Solved

how can I insert a floating shape at a bookmark with vba 2003 MS Word

Posted on 2012-03-14
7
944 Views
Last Modified: 2013-06-23
how can I insert a floating shape at a bookmark with vba 2003 MS Word
0
Comment
Question by:Dov_B
  • 4
  • 3
7 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37722710
Here is an example:
Dim sh As Shape
Dim rng As Range
Set rng = ActiveDocument.Bookmarks("bmk1").Range
Set sh = ActiveDocument.Shapes.AddShape(msoShapeOval, 10, 20, 150, 200, rng)

Open in new window

0
 

Author Comment

by:Dov_B
ID: 37722807
darn I meant a picutre I have the following and it dont work
Sub AddPictureAtBookMark3(bmname, picfile)
Selection.GoTo What:=wdGoToBookmark, Name:=bmname
Selection.InlineShapes.AddPicture FileName:= _
        picfile, LinkToFile:=False, _
        SaveWithDocument:=True
        ActiveDocument.InlineShapes(0).Select
  Selection.InlineShapes(Selection.InlineShapes.Count).ConvertToShape
  With ActiveDocument.Shapes(ActiveDocument.Shapes.Count)
  .Top = .Top + 40
  .Height = 50
  .Width = 50
  End With
End Sub

and nietther does this
Sub AddPictureAtBookMark(bmname, picfile)
With ActiveDocument.Shapes
Set myPicture = .AddPicture(picfile, _
LinkToFile = False, Savewithdcoument = True, _
Anchor:=ActiveDocument.Bookmarks(bmname).Range)
End With
End Sub
0
 

Author Comment

by:Dov_B
ID: 37722825
what I mean is that instaed of putting it at the bookmark it puts it at the top of the page
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37722918
Your first code refers to InLine shapes (i.e.not floating)

The shape will be displayed on the same page as the anchor. It's position on the page is determined by the top and left settings, so you would need to specify them, either in the AddPicture method or later:

Sub AddPictureAtBookMark(bmname, picfile)
    With ActiveDocument.Shapes
        Set myPicture = .AddPicture(picfile, _
                        LinkToFile = False, Savewithdcoument = True, _
                        Left:=100, Top:=100, _
                        Anchor:=ActiveDocument.Bookmarks(bmname).Range)
    End With
    myPicture.Left = 200
End Sub
0
 

Author Comment

by:Dov_B
ID: 37722934
Thank you so much for the explanation.
is it possible to get the left and top of the bookmark? you see I am trying to put the picutres very precisely under specific words. I do this by placing bookmarks at the words location
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 37724094
Thanks. It is possible to position a graphic relative to a range. However, it won't be fixed relative to the bookmark, so any change in pagination could move the bookmark, but not the graphic.
Sub AddShapeAtBookMark()
    Dim sh As Shape
    Dim rng As Range
    Set rng = ActiveDocument.Bookmarks("bmk1").Range

    Set sh = ActiveDocument.Shapes.AddShape(msoShapeRectangle, 0, 0, 30, 10, rng)
    
    sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    sh.Left = rng.Information(wdHorizontalPositionRelativeToPage)
    sh.Top = rng.Information(wdVerticalPositionRelativeToPage) + 12
End Sub

Open in new window

0
 

Author Comment

by:Dov_B
ID: 39270189
Dear GrahamSkan,
I used the above code many times with amazing results but then. Iwas busy with other things for a few months. When I tried using that macro code again it totaly did not put the picture next to the bookmark at all?!!
Do you have any ideas on what the issue might be?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question