Results 1 to 10 of 10

Thread: [RESOLVED] Resizing picture in Excel from VB.NET

  1. #1

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

    Resolved [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:
    1. Dim pic As String = "filelocation/exactfilename.jpg"
    2. oSheet.Range("B1:B1").Select() 'Where you want picture top left corner to be
    3. 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:
    1. objexcel.ActiveSheet.Pictures.Insert(App.Path & logotext).Select
    2. objexcel.Selection.ShapeRange.ScaleWidth 0.15, msoFalse, msoScaleFromTopLeft
    3. 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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Microsoft.Office.Core.MsoTriState.msoFalse
    2. 'And...
    3. Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

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

    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:
    1. Dim oXL As Excel.Application
    2. Dim oWB As Excel.Workbook
    3. Dim oSheet As Excel.Worksheet
    4.  
    5. ' Start Excel and get Application object.
    6. oXL = CreateObject("Excel.Application")
    7. oWB = oXL.Workbooks.Add
    8. oSheet = oWB.ActiveSheet
    9.  
    10. oSheet.Visible = True
    11. Dim pic As String = System.Windows.Forms.Application.StartupPath & "/logos/logo1.bmp"
    12.  
    13. oSheet.Range("B1:B1").Select() 'This is where the top left corner of picture will be
    14.  
    15. Dim opicture As Object
    16. opicture = oSheet.Pictures.Insert(pic)
    17. opicture.ShapeRange.ScaleWidth(0.5, 0, 0)
    18. opicture.ShapeRange.ScaleHeight(0.5, 0, 0)

    GridLines not Visible
    VB Code:
    1. oXL.ActiveWindow.DisplayGridlines = False

    Finally, the reason for using
    VB Code:
    1. System.Windows.Forms.Application.StartupPath
    and not
    VB Code:
    1. Application.StartupPath
    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.
    Last edited by craigreilly; Jun 2nd, 2006 at 06:50 PM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Imports Microsoft.Office.Interop
    2.  
    3. '...
    4.  
    5. Word.Application....
    6.  
    7. Outlook.Application....
    8.  
    9. 'And
    10.  
    11. 'VS
    12. Application.StartupPath
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

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

    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.
    Attached Images Attached Images  
    Last edited by craigreilly; Jun 3rd, 2006 at 10:41 AM.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

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

    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.
    Attached Images Attached Images  
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

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

    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.
    Attached Images Attached Images  
    VB 6 / VB.NET 2003, 2005 / Crystal Reports 9-12

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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