title: vbscript to add macro to MS Word
body: <p>I have the following VBscript ( <a href="https://stackoverflow.com/questions/1537810/vbscript-to-write-a-macro-within-an-excel-file">based on this answer</a> ):</p>
  
  <pre><code>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()" &amp; vbCr &amp; _
      "   msgbox ""Inside the macro"" " &amp; vbCr &amp; _
      "end sub"
  xlmodule.CodeModule.AddFromString strCode
  wdDoc.Save
  wdDoc.Close
  wdApp.Quit
  </code></pre>
  
  <p>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.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>p.s: i've provide fixed strings here for anybody else faced with same issue:</p>
  
  <pre><code>wdDoc.SaveAs "c:\docs_to_process\document1.docm", 13
  wdDoc.Close
  </code></pre>
  
  <p><strong>important note:</strong>
  instead of just save, give document new name and point 
  document type as <em>wdFormatXMLDocumentMacroEnabled</em> it is 13 in enum list
  I don't know why, but pointing enum type as</p>
  
  <pre><code>wdDoc.SaveAs "c:\docs_to_process\document1.docm", wdFormatXMLDocumentMacroEnabled 
  </code></pre>
  
  <p>causes same error after opening file as if i just change extention</p>