|
-
May 10th, 2006, 08:24 PM
#1
Thread Starter
Addicted Member
[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.
-
May 10th, 2006, 11:39 PM
#2
Addicted Member
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,
=======================================
If I helped you, Kindly Rate my post. Thanks
-----------
PHENOM 
-
May 11th, 2006, 12:28 AM
#3
Lively Member
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()
---------------------------------------------------
noob coder
---------------------------------------------------
-
May 11th, 2006, 01:01 AM
#4
Addicted Member
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,
Last edited by phenom; May 11th, 2006 at 01:05 AM.
=======================================
If I helped you, Kindly Rate my post. Thanks
-----------
PHENOM 
-
May 11th, 2006, 01:10 AM
#5
Lively Member
Re: [2005] Add Picture to Excel File
You're the MAN~~!
let me try it out now
Thanks alot
---------------------------------------------------
noob coder
---------------------------------------------------
-
May 11th, 2006, 01:12 AM
#6
Lively Member
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
---------------------------------------------------
-
May 11th, 2006, 09:42 AM
#7
Thread Starter
Addicted Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|