VBA PasteSpecial AutoShape with text can't be inline?

A

Ancient Brit

OK. Another weird one.

I have a need to create a special graphic for users based on limited text
that they type, select, and then click on a button to create. The text is
apparently never more than two characters, usually numeric, occasionally
punctuation.

The graphic is basically a triangle (users call it a "delta") with the text
inside (hopefully fitting neatly).

I did an initial quick n' dirty version that uses the symbol Delta and the
EQ() field, but apparently the unequally thick sides made it unacceptable so
I'm back to the drawing board.

It seemed logical to make use of the AutoShapes' isosceles triangle and add
the text, formatting it appropriately (bold, centered, color-red), but I hit
a snag.

What you can do manually can't it seems be done in VBA, not even if you
record a macro.

Here's the code as it stands (very rough). The user types one or two
characters and then simply selects them, then clicks on a buton to run the
macro. There's a check to see whether anything was actually selected.
....
'Cut the selection to the clipboard for re-insertion later. Test for no
selection:
On Error Resume Next
Selection.Cut
'An error may have been triggered - don't know the details and don't need to
yet
If Err.Number <> 0 Then
MsgBox "DEV: No selection was made. Please try again"
Exit Sub
End If

ActiveDocument.Shapes.AddShape(msoShapeIsoscelesTriangle, 0, 0, _
36, 36).Select
Selection.ShapeRange.TextFrame.TextRange.Select

With Selection
.Collapse
.Paste 'We cut the text above
.WholeStory
.Font.Size = 10
.Font.Bold = True
.Font.Color = wdColorRed
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.ShapeRange.Select
.Cut
End With

Selection.PasteSpecial link:=False, placement:=wdInLine,
dataType:=wdPasteMetafilePicture
....
The idea was to cut the finished graphic with text and then repaste it as a
picture (Windows metafile) formatted as inline so that the user can place it
wherever they need it - in a table, whatever - and it can be resized if
needed to match its surroundings. Manually it works like a charm. (As it
stands, the graphic cannot be formatted as inline unless you cut it and then
repaste as another format).

There's another issue about how to locate the graphic at the current
insertion point when it insists on "floating" but I'll cope with that later.

The PasteSpecial fails - you cannot specify successfully that the graphic
should be pasted inline. Yet when the macro completes, you can select the
graphic and format it as inline - just not in VBA.

Any thoughts, anyone?

Peter
 
J

Jay Freedman

This probably isn't going to help, but... it works for me. I just
copied your code, pasted it into a module, selected a couple of
characters in the text, and ran the macro. It did what's on the label.

This is Word 2003 on Windows 7, not that the operating system should
have anything to do with it.

When the macro fails for you, what's the claimed error? You might have
to put On Error GoTo 0 after the existing error trap to be able to
catch the message.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
A

Ancient Brit

Hi Jay

It's very odd - there's no error thrown as such - the resulting object just
doesn't get formatted as wdInline. It's left floating.

Your timing is just right :) I solved the problem (for me at least) in the
last few minutes. For whatever reason I had placed the Selection.PasteSpecial
outside the With Selection (probably because of the way I wrote the macro -
the With Selection was created after the Selection.PasteSpecial).

When I inserted the PasteSpecial so that it was part of the With Selection
(after the .Cut, as .PasteSpecial link:=False, placement:=wdInLine,
dataType:=wdPasteMetafilePicture), the problem went away.

I sorted out the resistance to creating the final graphic at the starting
insertion point by retaining that position as a range and returning to it -
still within the With Selection - before performing the .PasteSpecial.

I'm not sure why there should be a difference in behavior for a Selection
process. I'll have to explore that further.

But thanks for your feedback. Since the clipboard is involved and that's a
Windows function, the OS may have a role to play, IMHO.

I may be back here again before long - in the not too distant future we'll
be moving to a later version of SAP and Office 2007 so no doubt all of my
code will lurch to a halt :)

Best,

Peter
 

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