Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Word from Excel with VBA 1

Status
Not open for further replies.

vdivito

Technical User
Mar 23, 2007
4
US
I am in the process of creating a Excel macro that takes values from a spreadsheet and creates a word documnet. I am practically done with the project but I can not get around one error when creating a shape in Word. I have created a small macro for troubleshooting wich is pasted here. I get "Object variable or With block not set on the sh = appWD.ActiveDocument.... line. Any help would be appreciated. BTW - the same code minus "appWD." works fine in a Word macro.

Sub Test3()

Dim appWD As Word.Application

' Create a new instance of Word & make it visible
Set appWD = CreateObject("Word.Application.8")

With appWD
.Visible = True
.Activate
.WindowState = wdWindowStateNormal
End With

appWD.ScreenUpdating = True

' Tell Word to create a new document
appWD.Documents.Add

'Draw Rectangle
Dim sh As Shape

sh = appWD.ActiveDocument.Shapes.AddShape(msoShapeRectangle, Left:=90, Top:=70, Width:=72, Height:=72)

End Sub




 
And what about this ?
sh = appWD.ActiveDocument.Shapes.AddShape(1, Left:=90, Top:=70, Width:=72, Height:=72) ' 1=msoShapeRectangle

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 



Don't forget its an OBJECT
Code:
[b]
Set[/b] sh = appWD.ActiveDocument.Shapes.AddShape(msoShapeRectangle, Left:=90, Top:=70, Width:=72, Height:=72)

Skip,

[glasses] [red][/red]
[tongue]
 
Bingo.

You may also want to explicitly declare it as:
Code:
Dim sh As Word.Shape
The code may think of it as an Excel Shape. I know this often happens with people declaring:

Dim rng As Range

when they are controlling stuff in Word, from Excel. If not explicitly declared as a Word Range, an Excel macro will assume (correctly) that Range means an Excel range. They are very different beasties. Not sure if this would apply to Shape, but if you can declare it explicitly as a Word Shape, it may be better to do so.

Gerry
My paintings and sculpture
 
Thanks to SkipVought for your solution. It works.

Thanks also to the others here but I tried those methods and they did not solve my problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top