[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:
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
oXL = CreateObject("Excel.Application")
oXL.Visible = True 'Move to Bottom after Debugging
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oSheet.Visible = True
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.
Re: [2005] Add Picture to Excel File
Hi craigreilly,
try this code it's 100% working
VB Code:
Dim ObjExcel As New Microsoft.Office.Interop.Excel.Application
Dim imglocation As String
ObjExcel.Visible = True
ObjExcel.Workbooks.Add()
'here you have to specify the image path...
imglocation = PicBox.ImageLocation
ObjExcel.ActiveSheet.Pictures.Insert(imglocation)
ObjExcel = Nothing
hope this helps...
Regards,
Re: [2005] Add Picture to Excel File
I tried that too, but it doesn't seems to work
my code as
VB Code:
dim xlsApp as excel.application
dim xlsWB as excel.Workbook
dim xlsSheet as excel.Worksheet
dim rng as excel.Range
dim excelFiles as string = "c:\path"
dim pic as object = "c:\pic.gif"
xlsWB = xlsApp.Workbooks.open(excelFile)
rng = rng.Range("A26")
xlsSheet.Sheets("Sheet1").Pictures.Insert(pic)
xlsApp.WorkBooks.Close()
xlsApp.Quit()
xlsSheet = Nothing
xlsWB = Nothing
xlsApp = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
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:
Dim xlsApp As New Microsoft.Office.Interop.Excel.Application
'Dim xlsWB As New Microsoft.Office.Interop.Excel.Workbook
'Dim xlsSheet As New Microsoft.Office.Interop.Excel.Worksheet
'Dim rng As Microsoft.Office.Interop.Excel.Range
Dim excelFiles As String = "c:\software list.xls"
Dim pic As String = "d:\Image(01).jpg"
xlsApp.Visible = True
xlsApp.Workbooks.Open(excelFiles)
'rng = rng.Range("A26")
'xlsSheet.Sheets("Sheet1").Pictures.Insert(pic)
xlsApp.ActiveSheet.Pictures.Insert(pic)
'xlsApp.Workbooks.Close()
'xlsApp.Quit()
'xlsSheet = Nothing
'xlsWB = Nothing
xlsApp = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
regards,
Re: [2005] Add Picture to Excel File
You're the MAN~~!
let me try it out now
Thanks alot
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)
:mad:
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
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:
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:
Dim pic As String = System.AppDomain.CurrentDomain.BaseDirectory() & "/logos/logo.jpg"
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.