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
824 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now