VBA code no longer works in Office XP

B

Bob U

I am modifying a template previously developed (by someone
else)that inserts an enhanced metafile from the clipboard
and converts it into an inline image. When I use this in
Office XP I get a runtime error 424, object required in
the line marked with ">>>" below. The template worked in
Office 97 and 2000.


If replace = False Then
mySel.InsertParagraphAfter
End If

On Error GoTo ErrorHandler:
mySel.PasteSpecial Link:=False,
DataType:=wdPasteEnhancedMetafile
On Error GoTo 0

'Can't make a Metafile an InlineShape when inserting so do
it below.
With mySel.ShapeRange.Width = InchesToPoints(6.5)
.ConvertToInlineShape
End With

Commenting out affected line moves the error to the next
line.

Any insight, advice, or work around will be greatly
appreciated.

TIA

Bob

P.S. ErrorHandler traps non-enhancedmetafiles in the
clipboard...
 
J

Jay Freedman

Hi, Bob,

I can tell you what to do to fix the problem, but I can't tell you what
changed or why...

The metafile is being pasted before -- not in -- the range mySel. Running
your code in the debugger with F8 and with mySel.ShapeRange in the Watch
window, you can see that its .Count property is 0, meaning there are no
shapes in the ShapeRange.

To fix this, add this line before the With mySel.ShapeRange statement:

mySel.MoveStart Unit:=wdCharacter, Count:=-1

That will extend the range one character toward the beginning of the
document, so that it includes the pasted metafile. Now you'll see its .Count
increase to 1, and everything else will work.
 
B

Bob U

Jay,

Thanks for responding, however...

I'm still getting the same issue. What you say makes
sence, but I still get the runtime error. Any other ideas?

TIA again.

Bob
 
J

Jay Freedman

Hi, Bob,

I'm not really sure what's happening in your setup... I'm working with Word
2003, not XP.

It's apparent that the shape's anchor isn't where we expect it to be.
However, it *has to be* somewhere in the same paragraph as the range where
you're pasting it, so this should work: Remove the mySel.MoveStart
statement and change the With mySel.ShapeRange statement to this:

With mySel.Paragraphs(1).Range.ShapeRange
 
B

Bob U

Jay,

Thank you again for your prompt response.

This one worked. Like you said, it HAD to be somewhere...

BTW - I checked and it is Office XP and Word 2002 SP-1
(10.3416.3501).

Cheers!
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top