Using Excel VBA to create shape in Word

G

Guest

I am writing code in Excel that will create a report in Word. In one section,
I want to add a shape and send it to the back of a paragraph. The code works
fine when I run it in Word, but it doesn't work from my sub routine in Excel.
I obviously need to replace With "ActiveDocument" to something else but other
variables created earlier such as WdApp or WdDoc do not seem to work.

With ActiveDocument
.Shapes.AddShape Type:=msoShapeRectangle, _
Left:=60, Top:=120, Width:=500, Height:=150
.Shapes(1).Fill.ForeColor _
.RGB = RGB(Red:=205, Green:=216, Blue:=255)
.Shapes(1).Line.Visible = msoFalse
.Shapes(1).WrapFormat.AllowOverlap = True
.Shapes(1).WrapFormat.Side = wdWrapBoth
.Shapes(1).WrapFormat.Type = 3
.Shapes(1).ZOrder 5

End With
 
N

NickHK

Ariel,
Yes, you need the Word application object to precede Word's objects. e.g.
With wdApp.ActiveDocument
or if appropriated declared
With WdDoc

Also though, you have the Word constants (wdWrapBoth, etc), those that begin
with "wd". You need to qualify those also, otherwise Excel will not which
library they are from. Or change them to use their numeric value instead.
The other constant that start with "mso" should be OK as these are from the
Office library that Excel already knows about.

NickHK
 
T

Tom Ogilvy

Perhaps

With WdApp.ActiveDocument
.Shapes.AddShape Type:=msoShapeRectangle, _
Left:=60, Top:=120, Width:=500, Height:=150
.Shapes(1).Fill.ForeColor _
.RGB = RGB(Red:=205, Green:=216, Blue:=255)
.Shapes(1).Line.Visible = msoFalse
.Shapes(1).WrapFormat.AllowOverlap = True
.Shapes(1).WrapFormat.Side = wdWrapBoth
.Shapes(1).WrapFormat.Type = 3
.Shapes(1).ZOrder 5

End With

or

With WdApp.ActiveDocument.Shapes _
.AddShape( Type:=msoShapeRectangle, _
Left:=60, Top:=120, Width:=500, Height:=150)
.Fill.ForeColor.RGB = _
RGB(Red:=205, Green:=216, Blue:=255)
.Line.Visible = msoFalse
.WrapFormat.AllowOverlap = True
.WrapFormat.Side = wdWrapBoth
.WrapFormat.Type = 3
.ZOrder 5

End With
 
G

Guest

This worked perfectly! Thanks Nick.

NickHK said:
Ariel,
Yes, you need the Word application object to precede Word's objects. e.g.
With wdApp.ActiveDocument
or if appropriated declared
With WdDoc

Also though, you have the Word constants (wdWrapBoth, etc), those that begin
with "wd". You need to qualify those also, otherwise Excel will not which
library they are from. Or change them to use their numeric value instead.
The other constant that start with "mso" should be OK as these are from the
Office library that Excel already knows about.

NickHK
 
T

Tom Ogilvy

I missed the wdWrapBoth constant even though I looked for constants unique
to the Word Object model

If you have created a reference to the word object model, then you should
not need to qualify the constant. If you have not, then I would replace it
with its numeric value myself.

In this particular case, the value is zero, so it should work as written
(but this is coincidental).
 
Joined
Jul 15, 2014
Messages
2
Reaction score
0
Sorry May it is very old post but when I search I found your post then After may trials with other Posts I think this will help me or any one Who need like this

Function WriteToWordDoc()
Dim objWord
Dim objDoc
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
'add Text
Set objSelection = objWord.Selection
objSelection.TypeText Text:="First Name: Fahad"
'''''or
objSelection.TypeText Chr(10) & "Last Name Dossary"
' add shape
Dim Shp As Word.Shape 'should be like this word.shape
Set Shp = objDoc.Shapes.AddShape(msoShapeSmileyFace, 156, 236, 50, 50)
With Shp
.Fill.ForeColor.RGB = RGB(252, 236, 153)
.Line.ForeColor.RGB = RGB(0, 0, 0)
End With

End Function
 
Joined
Jul 15, 2014
Messages
2
Reaction score
0
Sorry May it is very old post but when I search I found your post then After may trials with other Posts I think this will help me or any one Who need like this
Write text or Add Shape to Word By Excel
Function WriteToWordDocfromExcel( )
Dim objWord
Dim objDoc
Dim objSelection
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
objWord.Visible = True
'add Text
Set objSelection = objWord.Selection
objSelection.TypeText Text:="First Name: Fahad"
'''''or
objSelection.TypeText Chr(10) & "Last Name Dossary"
' add shape
Dim Shp As Word.Shape 'should be like this word.shape
Set Shp = objDoc.Shapes.AddShape(msoShapeSmileyFace, 156, 236, 50, 50)
With Shp
.Fill.ForeColor.RGB = RGB(252, 236, 153)
.Line.ForeColor.RGB = RGB(0, 0, 0)
End With

End Function
 

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