[RESOLVED] Resizing picture in Excel from VB.NET
In my last post, I was able to find out how to add a picture to an Excel document from VB.NET.
VB Code:
Dim pic As String = "filelocation/exactfilename.jpg"
oSheet.Range("B1:B1").Select() 'Where you want picture top left corner to be
oXL.ActiveSheet.Pictures.Insert(pic)
What if I have multiple Excel doc's I am creating and the picture needs to be a different size on each one?
In VB6, I did it this way - which does not work so well in .NET.
VB Code:
objexcel.ActiveSheet.Pictures.Insert(App.Path & logotext).Select
objexcel.Selection.ShapeRange.ScaleWidth 0.15, msoFalse, msoScaleFromTopLeft
objexcel.Selection.ShapeRange.ScaleHeight 0.15, msoFalse, msoScaleFromTopLeft
I am having a lot of trouble understanding how Excel works with the newer .NET language. Slowly buy surely I am figuring this out... with the help of everyone on VB Forums.
Thank you in advance for your time and assistance.
Re: Resizing picture in Excel from VB.NET
There is no "App.Path" in .net so use Application.StartupPath as its replacement.
msoFalse and msoScaleFromTopLeft should need to be qualified depending on your Imports statement.
VB Code:
Microsoft.Office.Core.MsoTriState.msoFalse
'And...
Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft
Re: Resizing picture in Excel from VB.NET
It took a lot of searching but here it is.
Also at the bottom is getting rid of GridLines in the display if anyone needs that code.
VB Code:
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
' Start Excel and get Application object.
oXL = CreateObject("Excel.Application")
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet
oSheet.Visible = True
Dim pic As String = System.Windows.Forms.Application.StartupPath & "/logos/logo1.bmp"
oSheet.Range("B1:B1").Select() 'This is where the top left corner of picture will be
Dim opicture As Object
opicture = oSheet.Pictures.Insert(pic)
opicture.ShapeRange.ScaleWidth(0.5, 0, 0)
opicture.ShapeRange.ScaleHeight(0.5, 0, 0)
GridLines not Visible
VB Code:
oXL.ActiveWindow.DisplayGridlines = False
Finally, the reason for using
VB Code:
System.Windows.Forms.Application.StartupPath
and not
as suggested is that 'Application' is ambiguous, imported from the namespaces or types 'CRAXDDRT, System.Windows.Forms'.
Because I am using Crystal Reports - the namespace was conflicting and I had to fully qualify it.
Hope this helps someone who may be struggling with this.
Re: [RESOLVED] Resizing picture in Excel from VB.NET
Yes, when importing other namespaces that have an Application object, like Word and CR, you need to qualify them to make each one distinctly different and recognizable by .net.
I never import all the way down to Word but just to the .Interop class. Then each will be identifiable.
VB Code:
Imports Microsoft.Office.Interop
'...
Word.Application....
Outlook.Application....
'And
'VS
Application.StartupPath
1 Attachment(s)
Re: [RESOLVED] Resizing picture in Excel from VB.NET
I am not quite sure how to do what you are suggesting. In VB.NET 2005, I goto the 'Project Properties', click on References, and select the Namespaces to import.
So, I think in my case, I had to fully qualify the Application.StartupPath to System.Windows.Forms.Application.StartupPath - I did not see a choice in the matter.
Thanks for any additional info you may have in helping my understand this.
Re: [RESOLVED] Resizing picture in Excel from VB.NET
Not sure either as I am running 03. Isnt there anything in the code behind where you have the declarations like Option Explicit On and the Imports?
1 Attachment(s)
Re: [RESOLVED] Resizing picture in Excel from VB.NET
Is is rather strange - a little change from the 03 days.
The Designer generated code is hidden by default. In the solution explorer you have to 'Show All Filed' then expand your form and double click on the Form.Designer.vb file.
Re: [RESOLVED] Resizing picture in Excel from VB.NET
I think there is still another part to it. There is supossed to be a partial class file that sontains all the code for creating the controls and events. It should be in there.
1 Attachment(s)
Re: [RESOLVED] Resizing picture in Excel from VB.NET
As I mentioned in my previous, you can show all files and see the .resx. I think the info is in here... But - playing in here can lead to some serious problems. I'd rather stay out. :)
Re: [RESOLVED] Resizing picture in Excel from VB.NET
Ok then, but thanks for the screenshots.
You will just have to fully qualify each as you refer to them then.