locked
how do I insert full page picture into word document via VBA? RRS feed

  • Question

  • Hi, I'm trying to work out the VBA to insert a picture into the first page of a word document AND the picture will fill the page, ie there will be no margin except the one the printer inserts when I print.

    I have tried recording a macro to see what happens but none of the position features are enabled as I'm recording.

    Can anyone point me inthe right direction to insert a full page picture please?

    All the best,

    Tom.

    Tuesday, February 14, 2012 3:50 PM

Answers

  • Hi tombola,

    Herewith the automated version, make sure there is at least 2 pages in your document (you might automate that as well, if necessary).

    I recorded a macro, which recorded the manual change of margins and I modified it a little (you can even strip it down more if you want).


    Sub test()
    
           Dim strPath As String
           Dim objShape As Shape
           
           Dim UsableWidth As Single
           Dim UsableHeight As Single
           
        ' set page margins first
    
        WordBasic.PageSetupMargins Tab:=0, PaperSize:=9, TopMargin:="0", _
            BottomMargin:="0", LeftMargin:="0", RightMargin:="0", Gutter:="0", _
            PageWidth:="21.59", PageHeight:="27.94", Orientation:=0, FirstPage:=0, _
            OtherPages:=0, VertAlign:=0, ApplyPropsTo:=4, FacingPages:=0, _
            HeaderDistance:="1.25", FooterDistance:="1.25", SectionStart:=2, _
            OddAndEvenPages:=0, DifferentFirstPage:=1, Endnotes:=0, LineNum:=0, _
            CountBy:=0, TwoOnOne:=0, GutterPosition:=0, LayoutMode:=0, DocFontName:= _
            "", FirstPageOnLeft:=0, SectionType:=1, FolioPrint:=0, ReverseFolio:=0, _
            FolioPages:=1
    
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    
        WordBasic.PageSetupMargins Tab:=0, PaperSize:=9, TopMargin:="2", _
            BottomMargin:="2", LeftMargin:="2", RightMargin:="2", Gutter:="0", _
            PageWidth:="21.59", PageHeight:="27.94", Orientation:=0, FirstPage:=0, _
            OtherPages:=0, VertAlign:=0, ApplyPropsTo:=1, FacingPages:=0, _
            HeaderDistance:="1.25", FooterDistance:="1.25", SectionStart:=2, _
            OddAndEvenPages:=0, DifferentFirstPage:=1, Endnotes:=0, LineNum:=0, _
            CountBy:=0, TwoOnOne:=0, GutterPosition:=0, LayoutMode:=0, DocFontName:= _
            "", FirstPageOnLeft:=0, SectionType:=1, FolioPrint:=0, ReverseFolio:=0, _
            FolioPages:=1
            
                 
           ' calculated usable width and height
           With ActiveDocument.PageSetup
              UsableWidth = .PageWidth '- .LeftMargin - .RightMargin
              UsableHeight = .PageHeight '- .TopMargin - .BottomMargin
           End With
           
    
           Set colShapes = ActiveDocument.Shapes
           
           ' change path and filename accordingly
           strPath = "C:\yourpathname\yourfilename.jpg"
      
            ' add picture in document
            Set objShape = colShapes.AddPicture(FileName:=strPath)
                        
            ' adjust the height and width of the picture to usable height and width
            With objShape
                 .Height = UsableHeight
                 .Width = UsableWidth
            End With
     
            
    End Sub

    Hope this helps,



    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"


    • Edited by danishani Wednesday, February 22, 2012 10:07 PM
    • Proposed as answer by danishani Thursday, February 23, 2012 3:30 PM
    • Marked as answer by danishani Saturday, February 25, 2012 7:01 PM
    Wednesday, February 22, 2012 10:06 PM

All replies

  • Hi Tom,

    You can try the following to adjust the Picture to the usable height and with of your Page, see below code:

    Sub test()
    
           Dim strPath As String
           Dim objShape As Shape
           
           Dim UsableWidth As Single
           Dim UsableHeight As Single
           
           ' calculated usable width and height
           With ActiveDocument.PageSetup
              UsableWidth = .PageWidth - .LeftMargin - .RightMargin
              UsableHeight = .PageHeight - .TopMargin - .BottomMargin
           End With
           
    
           Set colShapes = ActiveDocument.Shapes
           
           ' change path and filename accordingly
           strPath = "C:\YourPathHere\YourPictureNameHere.jpg"
      
            ' add picture in document
            Set objShape = colShapes.AddPicture(FileName:=strPath)
                        
            ' adjust the height and width of the picture to usable height and width
            With objShape
                 .Height = UsableHeight
                 .Width = UsableWidth
            End With
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    • Proposed as answer by danishani Thursday, February 16, 2012 10:48 PM
    • Marked as answer by danishani Tuesday, February 21, 2012 4:17 PM
    • Unmarked as answer by tombola Wednesday, February 22, 2012 11:18 AM
    • Unproposed as answer by danishani Saturday, February 25, 2012 7:01 PM
    Tuesday, February 14, 2012 6:47 PM
  • Hi Daniel,

    APOLOGIES for the late response, I have been overloaded with work.

    This works great. Is there a way to get rid of the margins though, so the picture fills the whole page?

    Thanks again,

    Tom.
    Wednesday, February 22, 2012 11:19 AM
  • Hi Tom,

    You might change the margins accordingly, so if you set them to 0, the picture will be exposed to the whole page.

    So in other words, try below adjusted code:

    Sub test()
    
           Dim strPath As String
           Dim objShape As Shape
           
           Dim UsableWidth As Single
           Dim UsableHeight As Single
           
           ' calculated usable width and height
           With ActiveDocument.PageSetup
              .LeftMargin = 0
              .RightMargin = 0
              .TopMargin = 0
              .BottomMargin = 0
              UsableWidth = .PageWidth - .LeftMargin - .RightMargin
              UsableHeight = .PageHeight - .TopMargin - .BottomMargin
           End With
           
    
           Set colShapes = ActiveDocument.Shapes
           
           ' change path and filename accordingly
           strPath = "C:\yourpathname\yourfilename.jpg"
      
            ' add picture in document
            Set objShape = colShapes.AddPicture(FileName:=strPath)
                        
            ' adjust the height and width of the picture to usable height and width
            With objShape
                 .Height = UsableHeight
                 .Width = UsableWidth
            End With
    
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 22, 2012 3:24 PM
  • Hi, is there a way to make these changes just for the current page though please? So all other pages would have their default margin?
    Wednesday, February 22, 2012 3:27 PM
  • Ok, I got it to work.

    1. You first set manually the Margins (Page Setup > Customize setting) apply to whole document to 0.

    2. Then you go to page 2. You set the Margins (Page setup > Customize setting) apply from this point forward to the margins you want.

    This allows you to have the first page with margins 0, then from all other pages on forward, the margins are different.

    Now, the above steps are manually, but might be automated in some way, but did not figured that out yet.

    Then you change the code a little, using only the .PageWidth and .PageHeight to this:

    Sub test()
    
           Dim strPath As String
           Dim objShape As Shape
           
           Dim UsableWidth As Single
           Dim UsableHeight As Single
           
              
           ' calculated usable width and height
           With ActiveDocument.PageSetup
              UsableWidth = .PageWidth '- .LeftMargin - .RightMargin
              UsableHeight = .PageHeight '- .TopMargin - .BottomMargin
           End With
           
    
           Set colShapes = ActiveDocument.Shapes
           
           ' change path and filename accordingly
           strPath = "C:\yourpathname\yourfilenamejpg"
      
            ' add picture in document
            Set objShape = colShapes.AddPicture(FileName:=strPath)
                        
            ' adjust the height and width of the picture to usable height and width
            With objShape
                 .Height = UsableHeight
                 .Width = UsableWidth
            End With
     
    
            
    End Sub

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 22, 2012 4:10 PM
  • Hi tombola,

    Herewith the automated version, make sure there is at least 2 pages in your document (you might automate that as well, if necessary).

    I recorded a macro, which recorded the manual change of margins and I modified it a little (you can even strip it down more if you want).


    Sub test()
    
           Dim strPath As String
           Dim objShape As Shape
           
           Dim UsableWidth As Single
           Dim UsableHeight As Single
           
        ' set page margins first
    
        WordBasic.PageSetupMargins Tab:=0, PaperSize:=9, TopMargin:="0", _
            BottomMargin:="0", LeftMargin:="0", RightMargin:="0", Gutter:="0", _
            PageWidth:="21.59", PageHeight:="27.94", Orientation:=0, FirstPage:=0, _
            OtherPages:=0, VertAlign:=0, ApplyPropsTo:=4, FacingPages:=0, _
            HeaderDistance:="1.25", FooterDistance:="1.25", SectionStart:=2, _
            OddAndEvenPages:=0, DifferentFirstPage:=1, Endnotes:=0, LineNum:=0, _
            CountBy:=0, TwoOnOne:=0, GutterPosition:=0, LayoutMode:=0, DocFontName:= _
            "", FirstPageOnLeft:=0, SectionType:=1, FolioPrint:=0, ReverseFolio:=0, _
            FolioPages:=1
    
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    
        WordBasic.PageSetupMargins Tab:=0, PaperSize:=9, TopMargin:="2", _
            BottomMargin:="2", LeftMargin:="2", RightMargin:="2", Gutter:="0", _
            PageWidth:="21.59", PageHeight:="27.94", Orientation:=0, FirstPage:=0, _
            OtherPages:=0, VertAlign:=0, ApplyPropsTo:=1, FacingPages:=0, _
            HeaderDistance:="1.25", FooterDistance:="1.25", SectionStart:=2, _
            OddAndEvenPages:=0, DifferentFirstPage:=1, Endnotes:=0, LineNum:=0, _
            CountBy:=0, TwoOnOne:=0, GutterPosition:=0, LayoutMode:=0, DocFontName:= _
            "", FirstPageOnLeft:=0, SectionType:=1, FolioPrint:=0, ReverseFolio:=0, _
            FolioPages:=1
            
                 
           ' calculated usable width and height
           With ActiveDocument.PageSetup
              UsableWidth = .PageWidth '- .LeftMargin - .RightMargin
              UsableHeight = .PageHeight '- .TopMargin - .BottomMargin
           End With
           
    
           Set colShapes = ActiveDocument.Shapes
           
           ' change path and filename accordingly
           strPath = "C:\yourpathname\yourfilename.jpg"
      
            ' add picture in document
            Set objShape = colShapes.AddPicture(FileName:=strPath)
                        
            ' adjust the height and width of the picture to usable height and width
            With objShape
                 .Height = UsableHeight
                 .Width = UsableWidth
            End With
     
            
    End Sub

    Hope this helps,



    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"


    • Edited by danishani Wednesday, February 22, 2012 10:07 PM
    • Proposed as answer by danishani Thursday, February 23, 2012 3:30 PM
    • Marked as answer by danishani Saturday, February 25, 2012 7:01 PM
    Wednesday, February 22, 2012 10:06 PM