none
Adding picture to every page RRS feed

  • Question

  • I wrote a VB script that adds a picture at the top right corner on every page.

    It works fine with .doc files, but with .docx files picture gets a wrong position.

    It's placed higher and to the rigth than it supposed to be. 

     

     

    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 see that this problem has something to do with margins.

    Any help on understanding why such a difference take place will be appreciated.

     

     

     

    Wednesday, August 3, 2011 7:38 AM

Answers

  • This is due to a change in defaults. If you set everything explicitly, both do the same.
     
       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.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
            shape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
     
            shape.Top = 0
     
            shape.Left = ActiveDocument.PageSetup.PageWidth - shape.Width
     
       Next
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Proposed as answer by Bruce Song Wednesday, August 10, 2011 8:02 AM
    • Marked as answer by Bruce Song Friday, August 19, 2011 3:02 AM
    Tuesday, August 9, 2011 10:05 AM

All replies

  • Hi Andrey,

    I can reproduce the problem on my side with the code snippet. I think it could be the issue of docx file, I will help you submit to our internal bug system and it will be highly evaluated by our engineer and then decided whether to fix. If there exist any update, I will notify you at the first time.

    Appreciate your patience and your understanding.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 9, 2011 3:15 AM
  • This is due to a change in defaults. If you set everything explicitly, both do the same.
     
       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.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
            shape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
     
            shape.Top = 0
     
            shape.Left = ActiveDocument.PageSetup.PageWidth - shape.Width
     
       Next
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Proposed as answer by Bruce Song Wednesday, August 10, 2011 8:02 AM
    • Marked as answer by Bruce Song Friday, August 19, 2011 3:02 AM
    Tuesday, August 9, 2011 10:05 AM
  • Hi Tony,

    Thank you very much for pointing out about this. Luckily, I haven't submit to the internal bug system.

    You are right and I agree with you. Thank you for your valuable contribute. I have test your code snippet and it works well for docx files. 

    @Andrey, here is the reference on MSDN about the top property of the Shape in Word 2010:

    http://msdn.microsoft.com/en-us/library/ff196323.aspx

    The sample also set the RelativeVerticalPosition .

    Hope this can help you and feel free to follow up after you have tried.

    Best Regards, 


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, August 10, 2011 8:01 AM