• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 920
  • Last Modified:

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

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
Dov_B
Asked:
Dov_B
  • 3
  • 2
1 Solution
 
GrahamSkanCommented:
What  *@!     ?
0
 
Dov_BAuthor Commented:
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
 
GrahamSkanCommented:
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
 
Dov_BAuthor Commented:
run-time error '-2147467259 (80004005)':
Method 'AddPicture' of object 'InlineShapes' failed
0
 
GrahamSkanCommented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now