none
Adding picture to every page RRS feed

  • Question

  • Hi everyone!

    Some time ago i asked about adding picture. 
    Now i have another question.
    Dim pageNumbers As Integer
    
       pageNumbers = ActiveDocument.BuiltInDocumentProperties(14).Value
    
       For i = 1 To pageNumbers
    
            Set r = ActiveDocument.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, i)
    
            Set InlineShape = ActiveDocument.InlineShapes.AddPicture(FileName:="C:\1.jpg", LinkToFile:=False, _
    
                  SaveWithDocument:=True, range:=r)
    
            InlineShape.Select
    
            Set Shape = InlineShape.ConvertToShape()
    
            Shape.Top = 0
    
            Shape.Left = ActiveDocument.PageSetup.PageWidth - Shape.Width
    
       Next

     
     
    I have a problem with this code when document contains tables, covering several pages.
    As tables are inseparable we can't set insert position at the top left corner of the pages.
    For that reason images is inserted incorrectly.
    I was thinking about using headers or inserting into textboxes, but it doesn't suit.
    Any thoughts to rewrite this code, or find a way to locate cursor will be appreciated.

     



    Tuesday, November 8, 2011 2:42 PM

Answers

  • It turned out that Shape has LayoutInCell property.

    So final version:

    Dim pageNumbers As Integer
    
       pageNumbers = ActiveDocument.BuiltInDocumentProperties(14).Value
    
       For i = 1 To pageNumbers
    
            Set r = ActiveDocument.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, i)
    
            Set Shape = ActiveDocument.Shapes.AddPicture(FileName:="C:\1.jpg", LinkToFile:=False, SaveWithDocument:=True, Left:=0, Top:=0, Width:=40, Height:=40, Anchor:=r)
      
            Shape.LayoutInCell = False
    
            Shape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    
            Shape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    
            Shape.WrapFormat.Type = wdWrapBehind
    
            Shape.Top = 0
    
            Shape.Left = 0
    
       Next
    

     

    Wednesday, November 9, 2011 2:52 PM

All replies

  • Hi Andrey

    Why not use Shapes.AddPicture instead of InlineShapes.AddPicture?


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 8, 2011 7:03 PM
    Moderator
  • Initially i preffered InlineShapes.AddPicture because Shapes.AddPicture requires image size (so i have to figure it out).

    If i used Shapes.AddPicture i would have something like that:

    Dim pageNumbers As Integer
    
       pageNumbers = ActiveDocument.BuiltInDocumentProperties(14).Value
    
       For i = 1 To pageNumbers
    
            Set r = ActiveDocument.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, i)
    
            Set Shape = ActiveDocument.Shapes.AddPicture(FileName:="C:\1.jpg", LinkToFile:=False, SaveWithDocument:=True, Left:=0, Top:=0, Width:=40, Height:=40, Anchor:=r)
    
            Shape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    
            Shape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    
            Shape.WrapFormat.Type = wdWrapBehind
    
            Shape.Top = 0
    
            Shape.Left = 0
    
       Next
    

    Actually it doesn't make any difference.

    Cursor can't be positioned before table, it's being positioned in first cell. 

    I can simply draw textbox (which is also shape) anywhere. So there has to be a way to do the same with image.

     

    Tuesday, November 8, 2011 8:19 PM
  • It turned out that Shape has LayoutInCell property.

    So final version:

    Dim pageNumbers As Integer
    
       pageNumbers = ActiveDocument.BuiltInDocumentProperties(14).Value
    
       For i = 1 To pageNumbers
    
            Set r = ActiveDocument.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, i)
    
            Set Shape = ActiveDocument.Shapes.AddPicture(FileName:="C:\1.jpg", LinkToFile:=False, SaveWithDocument:=True, Left:=0, Top:=0, Width:=40, Height:=40, Anchor:=r)
      
            Shape.LayoutInCell = False
    
            Shape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    
            Shape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    
            Shape.WrapFormat.Type = wdWrapBehind
    
            Shape.Top = 0
    
            Shape.Left = 0
    
       Next
    

     

    Wednesday, November 9, 2011 2:52 PM
  • Hi Andrey

    Glad the suggestion worked for you :-)

    <<because Shapes.AddPicture requires image size (so i have to figure it out).>>

    Actually, these parameters are optional, so you could pass Type.Missing. But you can also change the size at any time after the Shape has been inserted. So either way it shouldn't be a problem :-)

    When you have a minute, please stop by and mark the message(s) in the discussion that you consider to be the "Answers" :-)

    Edit (additional note): If a table is the only thing on a page (it's a long table) then the "cursor" must be in the table - there's nothing on the page outside the table. And all Shape objects must be anchored to a Range position inside that table. But, as you've found out, they don't have to be positioned in the table.


    Cindy Meister, VSTO/Word MVP
    Wednesday, November 9, 2011 3:15 PM
    Moderator