Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Add Picture to Excel File

  1. #1

    Thread Starter
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Resolved [RESOLVED] [2005] Add Picture to Excel File

    Many sites mention Excel automation, but fail to inform how to insert a picture to the Excel worksheet.

    VB Code:
    1. Dim oXL As Excel.Application
    2. Dim oWB As Excel.Workbook
    3. Dim oSheet As Excel.Worksheet
    4. oXL = CreateObject("Excel.Application")
    5. oXL.Visible = True 'Move to Bottom after Debugging
    6.  
    7. oWB = oXL.Workbooks.Add
    8. oSheet = oWB.ActiveSheet
    9.  
    10. oSheet.Visible = True
    11. oSheet.Pictures.insert("logo.jpg").Select()
    does not seem to work...

    The rest of my code works to add and format text, so I know the reference is correct.

    Thank in advance if anyone can help me.

  2. #2
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: [2005] Add Picture to Excel File

    Hi craigreilly,

    try this code it's 100% working
    VB Code:
    1. Dim ObjExcel As New Microsoft.Office.Interop.Excel.Application
    2. Dim imglocation As String
    3.  
    4. ObjExcel.Visible = True
    5. ObjExcel.Workbooks.Add()
    6.  
    7. 'here you have to specify the image path...
    8. imglocation = PicBox.ImageLocation
    9.  
    10. ObjExcel.ActiveSheet.Pictures.Insert(imglocation)
    11.  
    12. ObjExcel = Nothing

    hope this helps...

    Regards,
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  3. #3
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: [2005] Add Picture to Excel File

    I tried that too, but it doesn't seems to work

    my code as
    VB Code:
    1. dim xlsApp as excel.application
    2. dim xlsWB as excel.Workbook
    3. dim xlsSheet as excel.Worksheet
    4. dim rng as excel.Range
    5. dim excelFiles as string = "c:\path"
    6. dim pic as object = "c:\pic.gif"
    7.  
    8. xlsWB = xlsApp.Workbooks.open(excelFile)
    9. rng = rng.Range("A26")
    10. xlsSheet.Sheets("Sheet1").Pictures.Insert(pic)
    11. xlsApp.WorkBooks.Close()
    12. xlsApp.Quit()
    13. xlsSheet = Nothing
    14. xlsWB = Nothing
    15. xlsApp = Nothing
    16. GC.Collect()
    17. GC.WaitForPendingFinalizers()
    ---------------------------------------------------
    noob coder
    ---------------------------------------------------

  4. #4
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: [2005] Add Picture to Excel File

    I’ve tested my code and it’s 100% working,

    I modified your code and now it’s working… You have to make Excel visible, plus the picture path should be string not object…

    VB Code:
    1. Dim xlsApp As New Microsoft.Office.Interop.Excel.Application
    2. 'Dim xlsWB As New Microsoft.Office.Interop.Excel.Workbook
    3. 'Dim xlsSheet As New Microsoft.Office.Interop.Excel.Worksheet
    4. 'Dim rng As Microsoft.Office.Interop.Excel.Range
    5. Dim excelFiles As String = "c:\software list.xls"
    6. Dim pic As String = "d:\Image(01).jpg"
    7.  
    8. xlsApp.Visible = True
    9.  
    10. xlsApp.Workbooks.Open(excelFiles)
    11. 'rng = rng.Range("A26")
    12.  
    13. 'xlsSheet.Sheets("Sheet1").Pictures.Insert(pic)
    14. xlsApp.ActiveSheet.Pictures.Insert(pic)
    15.  
    16. 'xlsApp.Workbooks.Close()
    17. 'xlsApp.Quit()
    18. 'xlsSheet = Nothing
    19. 'xlsWB = Nothing
    20. xlsApp = Nothing
    21. GC.Collect()
    22. GC.WaitForPendingFinalizers()
    regards,
    Last edited by phenom; May 11th, 2006 at 01:05 AM.
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  5. #5
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: [2005] Add Picture to Excel File

    You're the MAN~~!
    let me try it out now
    Thanks alot
    ---------------------------------------------------
    noob coder
    ---------------------------------------------------

  6. #6
    Lively Member heeroyu16's Avatar
    Join Date
    Nov 2005
    Posts
    123

    Re: [2005] Add Picture to Excel File

    *crap~~!
    *now I got a access denied again...
    *hmmm enabled the security,and access.. I used to get this error
    *thats how I got the above codes in the first place

    I got around it by getting rid of the "new microsoft~~"
    now i'm getting a object reference no set to an instant of an object...

    xlsApp.Workbooks.Open(excelFiles)

    Maybe i's using the wrong reference..
    Microsoft excel 9.0 libary.. becos i've only office 2000 on my pc.. and the test pc is a win2k
    Last edited by heeroyu16; May 11th, 2006 at 01:57 AM.
    ---------------------------------------------------
    noob coder
    ---------------------------------------------------

  7. #7

    Thread Starter
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188

    Re: [2005] Add Picture to Excel File

    Thanks for the help. Part of the problem I was running into was the path of the file.
    When running Crystal Reports, I can give it a path like this:
    VB Code:
    1. report = application.OpenReport("reports/reporta.rpt", 1)

    But trying to put a graphic into Excel, you need the full path, probably because it is Excel inserting the picture, and not VB.NET

    VB Code:
    1. Dim pic As String = System.AppDomain.CurrentDomain.BaseDirectory() & "/logos/logo.jpg"
    2. oXL.ActiveSheet.Pictures.Insert(pic)

    And, atleast with VB.NET 2005 and Office XP, I did not have to have the Excel App visible until the end.

    Thanks for the feedback. This was really bugging me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width