?
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
Medium Priority
?
909 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Suggested Courses

650 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