Run macro in another doc

J

Jim Andersen

Hi,

This should work... but it doesn't.
Who can spot the error ?

I open a document, add a macro, and try to run that macro, but I get error
80020003 (macro can not be found)

Sub ThisFails()

Dim Doc As Document
Dim MyRange As Range

Set Doc = Documents.Open(FileName:="c:\temp\tst2.RTF",
ConfirmConversions:=False, ReadOnly:= _
True, Format:=wdOpenFormatRTF, Visible:=False)

Dim VBComp As VBComponent
Dim VBCodeMod As CodeModule
Dim Code As String
Set VBComp = Doc.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "tstModule"
Set VBCodeMod = VBComp.CodeModule

Code = _
"Sub Hello()" & Chr(13) & _
" msgbox ""Hello World""" & Chr(13) & _
"End Sub"

VBCodeMod.InsertLines 1, Code
Application.Run "Hello"

End Sub

The macro is in there, and it works ok. But it won't run it.

tia
/jim
 
C

Cindy M -WordMVP-

Hi Jim,
This should work... but it doesn't.
Who can spot the error ?
Try saving the project (macro container = document) before calling the macro?
I open a document, add a macro, and try to run that macro, but I get error
80020003 (macro can not be found)

Sub ThisFails()

Dim Doc As Document
Dim MyRange As Range

Set Doc = Documents.Open(FileName:="c:\temp\tst2.RTF",
ConfirmConversions:=False, ReadOnly:= _
True, Format:=wdOpenFormatRTF, Visible:=False)

Dim VBComp As VBComponent
Dim VBCodeMod As CodeModule
Dim Code As String
Set VBComp = Doc.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "tstModule"
Set VBCodeMod = VBComp.CodeModule

Code = _
"Sub Hello()" & Chr(13) & _
" msgbox ""Hello World""" & Chr(13) & _
"End Sub"

VBCodeMod.InsertLines 1, Code
Application.Run "Hello"

End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
J

Jim Andersen

Cindy M -WordMVP- said:
Hi Jim,

Try saving the project (macro container = document) before calling the
macro?

I did. And it works, but I would like to know why. Perhaps some container I
need to do a "refresh" on ?

thx
/jim
 

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