Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Oct 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting Picture in VBA (Word 2003 (11.6113.6360))

    In an effort to reduce the file size of a plethora of corporate templates due to bandwidth issues, I am trying to insert logo graphics using a AutoNew. Depending on the template, the graphics are either placed with body text or in the header. I've got the insertion of the graphic down and I can resize it okay, but I'm having trouble with the positioning. For some reason I can't record the commands from the Format Picture / Layout dialog to a macro and I haven't been able to find the appropriate ShapeRange properties. I'm trying to set the following values:

    Wrapping style: In front of text
    Horizontal Position: Absolute position 0" to the right of Column
    Vertical Position: Absolute position .5" below Page

    Any suggestions?

    Thanks,
    -Steve

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.6360))

    Something like this:

    With Selection.ShapeRange
    .WrapFormat.Type = wdWrapNone
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .Left = InchesToPoints(0)
    .Top = InchesToPoints(0.5)
    End With

  3. #3
    New Lounger
    Join Date
    Oct 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.

    That code has a similar problem to something I tried earlier. It bombs as it enters the With statement. If I manually change the wrapping style to "in front of text" while execution of the macro is paused I can then continue the macro and it works. The problem seems to be that some ShapeRange properties can't be set via code without some previous settings already being set. I've tried doing it with the selection in the body text and in the header and end up with the same result. I've tried different graphics file formats (jpg & gif) with the same result.

    Using your suggestion, here's the code I have for inserting the graphic at the current selection point.

    <<<Start Code>>>
    Sub InsertPCLogo ()

    Dim myTemplate As Template

    Set myTemplate = ActiveDocument.AttachedTemplate

    Selection.InlineShapes.AddPicture FileName:= _
    myTemplate.Path & Application.PathSeparator & _
    "WJE PC Logo.jpg", _
    LinkToFile:=False, _
    SaveWithDocument:=True

    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

    With Selection.ShapeRange
    .WrapFormat.Type = wdWrapNone
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .Left = InchesToPoints(0)
    .Top = InchesToPoints(0.5)
    End With

    End Sub
    <<<End Code>>>

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.

    You're adding an inline shape, then try to treat it as a floating shape. Try this instead - it inserts directly as a floating shape:

    Sub InsertPCLogo()
    Dim myTemplate As Template
    Set myTemplate = ActiveDocument.AttachedTemplate
    With ActiveDocument.Shapes.AddPicture(FileName:= _
    myTemplate.Path & Application.PathSeparator & _
    "WJE PC Logo.jpg", _
    LinkToFile:=False, _
    SaveWithDocument:=True)
    .WrapFormat.Type = wdWrapNone
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .Left = InchesToPoints(0)
    .Top = InchesToPoints(0.5)
    End With
    End Sub

  5. #5
    New Lounger
    Join Date
    Oct 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.

    Excellent! That solves my problems for the graphics inserted in the body text. It doesn't seem to work when I try to put it in the header, though. I think I might need to play with the Anchor argument to AddPicture, but I haven't gotten it to work yet. I'm trying to anchor it to a bookmark (hrd1_line5) and must not have the syntax quit right yet.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.

    Here is a modified version of the code that inserts a picture in the header of the first section and anchors it to the paragraph containing the specified bookmark.

    Sub InsertPCLogo()
    Dim myTemplate As Template
    Set myTemplate = ActiveDocument.AttachedTemplate
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterP rimary).Shapes.AddPicture _
    (FileName:=myTemplate.Path & Application.PathSeparator & _
    "WJE PC Logo.jpg", _
    LinkToFile:=False, _
    SaveWithDocument:=True, _
    Anchor:=ActiveDocument.Bookmarks("hrd1_line5").Ran ge)
    .WrapFormat.Type = wdWrapNone
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
    .RelativeVerticalPosition = wdRelativeVerticalPositionPage
    .Left = InchesToPoints(0)
    .Top = InchesToPoints(0.5)
    End With
    End Sub

  7. #7
    New Lounger
    Join Date
    Oct 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Formatting Picture in VBA (Word 2003 (11.6113.

    WoW (all pun intended). Thanks a lot. I'll have to play with that over the weekend and see if I can wrap up this project.

    Thanks, again.
    -Steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •