0
votes

I have the following VBscript ( based on this answer ):

Set wdApp = CreateObject( "Word.Application" )
Set wdDoc = wdApp.Documents.Open( "c:\docs_to_process\document1.docx" )
Set xlmodule = wdDoc.VBProject.VBComponents.Add( 1 )
strCode = _
    "sub test()" & vbCr & _
    "   msgbox ""Inside the macro"" " & vbCr & _
    "end sub"
xlmodule.CodeModule.AddFromString strCode
wdDoc.Save
wdDoc.Close
wdApp.Quit

The problem is: when I run it and stop the script before saving the document, I can see the macro in the opened Word file, but when I process futher, save the document and quit the script, after that if I open Word document, I see no macro in it.

What am I doing wrong?

p.s: i've provide fixed strings here for anybody else faced with same issue:

wdDoc.SaveAs "c:\docs_to_process\document1.docm", 13
wdDoc.Close

important note: instead of just save, give document new name and point document type as wdFormatXMLDocumentMacroEnabled it is 13 in enum list I don't know why, but pointing enum type as

wdDoc.SaveAs "c:\docs_to_process\document1.docm", wdFormatXMLDocumentMacroEnabled 

causes same error after opening file as if i just change extention

1

1 Answers

0
votes

For macro the file extension should be docm i.e. macro enabled. So change the file name

wdDoc.SaveAs "c:\docs_to_process\document1.docm", wdFormatXMLDocumentMacroEnabled