Word Macros: Drawing Objects?

R

rci

Hi all,

tried recording a macro of adding/modifying drawing objects.

The macro recorder simply failed to record anything, and in fact, prevented
me from performing many operations.

What the heck!

Excel does an excellent job of same.

Q: Are drawing objects even supported by Word VBA?

Specific issue: I need to add and change the text in a text box, as well as
paste/move an object.

Any help appreciated!

Regards...

SMS
 
J

Jean-Guy Marcil

rci was telling us:
rci nous racontait que :
Hi all,

tried recording a macro of adding/modifying drawing objects.

The macro recorder simply failed to record anything, and in fact,
prevented me from performing many operations.

What the heck!

My macro recorder recorded everything without any problems:
'_______________________________________
Sub Macro1()

ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 126#, _
108#, 342#, 198#).Select
Selection.ShapeRange.TextFrame.TextRange.Select
Selection.Collapse
Selection.TypeText Text:= _
"I have created this textbox and typed this text while the macro
recorder was on."
Selection.TypeParagraph
Selection.TypeText Text:= _
"Now I am going to select this box, cut it, type some text in the main
document and paste the box back in."
Selection.ShapeRange.Select
Selection.Cut
Selection.TypeText Text:="Text in the main document."
Selection.TypeParagraph
Selection.Paste

End Sub
'_______________________________________
Excel does an excellent job of same.

So does Word!
Q: Are drawing objects even supported by Word VBA?
Yes.

Specific issue: I need to add and change the text in a text box, as
well as paste/move an object.

Any help appreciated!

Here is the same macro, rewritten to remove the Selection object as much as
possible.

'_______________________________________
Sub MacroReWorked()

Dim MyBox As Shape
Dim BoxText As Range

With ActiveDocument
Set MyBox = .Shapes.AddTextbox(msoTextOrientationHorizontal, _
126#, 108#, 342#, 198#)
With MyBox
Set BoxText = .TextFrame.TextRange
BoxText.Text = "I have created this textbox and typed this text " _
& " while the macro recorder was on." & Chr(13) _
& "Now I am going to select this box, cut it, type some text " _
& "in the main document and paste the box back in."
.Select
End With
Selection.Cut
.Paragraphs(1).Range.Text = "Text in the main document." & Chr(13)
Selection.Paste
End With

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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