I have posted this previously, but I don't think I explained the situation accurately. Let me try to do a better job:
In Word, I have an existing macro (Macro1)that is stored in Normal.dot. I'm running the following code from Excel which builds and executes a macro (Macro2) in a new Word document.
Sub Macro3()
Range("A1:K48".Select
Selection.Copy
Call CreateMacro2
End Sub
Sub CreateMacro2()
Dim app As Object
Dim doc As Object
Dim module As Object
Dim strCode As String
Set app = CreateObject("Word.Application"
app.Visible = True
Set doc = app.Documents.Add
Set module = doc.VBProject.VBComponents.Add(1)
strCode = "Sub Macro2()" & vbCr & _
"Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _" & vbCr & _
"Placement:=wdFloatOverText, DisplayAsIcon:=False" & vbCr & _
"Application.Run MacroName:=""Macro1""" & vbCr & _
"End Sub"
module.CodeModule.AddFromString strCode
app.Run "Macro2"
End Sub
Macro2 that was built on the fly, looks like this:
Sub Macro2()
Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdFloatOverText, DisplayAsIcon:=False
Application.Run MacroName:="Macro1"
End Sub
When the last line of Macro2 processes calling Macro1, my Word document unexpectedly closes. The Word document is critical to the process I'm trying to complete. I can't seem to get the original Word document to remain the active window when I call Macro1. When I step through the code, the window closes as soon as that line is executed (Application.Run MacroName:="Macro1".
I can manually call Macro1 without any problem.
I can't help thinking that it has something to do with Macro1 being stored in Normal.dot and Macro2 is in the current document.
I'm confused. Any help is appreciated.
Thanks,
Brian
Brian
In Word, I have an existing macro (Macro1)that is stored in Normal.dot. I'm running the following code from Excel which builds and executes a macro (Macro2) in a new Word document.
Sub Macro3()
Range("A1:K48".Select
Selection.Copy
Call CreateMacro2
End Sub
Sub CreateMacro2()
Dim app As Object
Dim doc As Object
Dim module As Object
Dim strCode As String
Set app = CreateObject("Word.Application"
app.Visible = True
Set doc = app.Documents.Add
Set module = doc.VBProject.VBComponents.Add(1)
strCode = "Sub Macro2()" & vbCr & _
"Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _" & vbCr & _
"Placement:=wdFloatOverText, DisplayAsIcon:=False" & vbCr & _
"Application.Run MacroName:=""Macro1""" & vbCr & _
"End Sub"
module.CodeModule.AddFromString strCode
app.Run "Macro2"
End Sub
Macro2 that was built on the fly, looks like this:
Sub Macro2()
Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=wdFloatOverText, DisplayAsIcon:=False
Application.Run MacroName:="Macro1"
End Sub
When the last line of Macro2 processes calling Macro1, my Word document unexpectedly closes. The Word document is critical to the process I'm trying to complete. I can't seem to get the original Word document to remain the active window when I call Macro1. When I step through the code, the window closes as soon as that line is executed (Application.Run MacroName:="Macro1".
I can manually call Macro1 without any problem.
I can't help thinking that it has something to do with Macro1 being stored in Normal.dot and Macro2 is in the current document.
I'm confused. Any help is appreciated.
Thanks,
Brian
Brian