Solved

How can I insert and position a picture directly above a book mark in MS Word 2010 using VBA

Posted on 2013-01-16
5
832 Views
Last Modified: 2013-01-20
I had an amazing macro in MS word vba that inserted pictures right ontop of bookmarked words that worked with word 2003 but is erring the *@! out in 2010 Ive been googling away appears there is some bug in 2010 here is my code that used to work

Selection.GoTo what:=wdGoToBookmark, name:=bmname
With Selection.InlineShapes
Mypicture = .AddPicture(picfile, LinkToFile:=False, SaveWithDocument:=True)

Set myFloatPic = Mypicture.ConvertToShape
 
  With myFloatPic
.Anchor = ActiveDocument.BookMarks(bmname).Range
  .Top = .Top + mTop
  .Height = mHeight
  .Width = mWidth
0
Comment
Question by:Dov_B
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38785218
What  *@!     ?
0
 

Author Comment

by:Dov_B
ID: 38785244
Sorry that just replaces the expletives I wanted to use when saying that my trust macro erred out just because I shelled out some major bucks to buy office 2010
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 38785264
Then, I'm sorry. I don't really understand the question.

If you have some code that stops on an error, can you indicate the code line and give the error text  and number?
0
 

Author Comment

by:Dov_B
ID: 38785280
run-time error '-2147467259 (80004005)':
Method 'AddPicture' of object 'InlineShapes' failed
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 38786746
This works for me:
Set rng = ActiveDocument.Bookmarks(bmname).Range

With ActiveDocument.InlineShapes
    Set Mypicture = .AddPicture(picfile, LinkToFile:=False, SaveWithDocument:=True, Range:=rng)
End With

ActiveDocument.Bookmarks.Add bmname, rng 're-add overwritten bookmark
Set myFloatPic = Mypicture.ConvertToShape
With myFloatPic
    '.Anchor = ActiveDocument.Bookmarks(bmname).Range 'This is unnecessary. Also it deletes the picture
    .Width = mWidth
    .Top = .Top + mTop
    .Height = mHeight
End With

Open in new window


But you can add the picture as a Shape, so it doesn't have to be converted:
With ActiveDocument.Shapes
    Set myFloatPic = .AddPicture(picfile, LinkToFile:=False, SaveWithDocument:=True, Anchor:=bmname.Range)
End With
With myFloatPic
    .Width = mWidth
    .Top = .Top + mTop
    .Height = mHeight
End With

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

809 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