1
votes

I have an MS Word document with macros (.docm)

Based on many StackOverflow posts, I've written a macro to export as a pdf and save as a .docx

I open/edit the .docm document, that has an onSave macro that saves the document in .pdf format and .docx format which I distribute for other people to use. I will always be making my changes to the .docm document.

My issue is that doing so converts the active(open) document from .docm to .docx such that I'm no longer making my changes to the .docm.

Sub SaveActiveDocumentAsDocx()

    On Error GoTo Errhandler

    If InStrRev(ActiveDocument.FullName, ".") <> 0 Then

        Dim strPath As String
        strPath = Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".") - 1) & ".docx"
        ActiveDocument.SaveAs2 FileName:=strPath, FileFormat:=wdFormatDocumentDefault
        
    End If

    On Error GoTo 0

    Exit Sub

Errhandler:

    MsgBox "There was an error saving a copy of this document as DOCX. " & _
    "Ensure that the DOCX is not open for viewing and that the destination path is writable. Error code: " & Err

End Sub

I can find no parameter to prevent this conversion of the active document in either "saveas" or "saveas2"

Furthermore, after the "saveas" command, any additional lines in the original macro are not executed because the active document no longer contains macros. I tried adding lines to the macro to reopen the original .docm and then close the .docx but those commends never execute.

I'm hoping I'm just missing something simple?

1
Hey your code seems to be VBA, not VBScriptprogrammer365
Save your .docm as a .dotm, produce your new documents from that as .dotx.John Korchok
I specifically don't want to use a template. I just want to save a macro-containing document (.dotm) as a non-macro-containing format (.dotx)ZacWolf
A macro contaning document must be in .dotm format. Period. I think @John Korchock meant create a .docx from the .dotm template. Why do you specifically not want to use a template, even as an interim step?Charles Kenyon
There are two types of macro containing documents: .docm and .dotm. (document vs template). As your code below programmatically uses the "active document" (.docm) as the template for the new .docx I don't need a separate .dotm file, and when I open the new .docx it shows the template as Normal.dotm. This is what I wanted. THANKS!ZacWolf

1 Answers

2
votes
Sub SaveAMacrolessCopyOfActiveDocument()
    ' Charles Kenyon 2 October 2020
    ' Save a copy of active document as a macrofree document
    '
    Dim oDocument As Document
    Dim oNewDocument As Document
    Dim iLength As Long
    Dim strName As String
    Set oDocument = ActiveDocument '  - saves a copy of the active document
    ' Set oDocument = ThisDocument '- saves copy of code container rather than ActiveDocument
    Let iLength = Len(oDocument.Name) - 5
    Let strName = Left(oDocument.Name, iLength)
    Set oNewDocument = Documents.Add(Template:=oDocument.FullName, DocumentType:=wdNewBlankDocument, Visible:=False)
    oNewDocument.SaveAs2 FileName:=strName & ".docx", FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=15
    oNewDocument.Close SaveChanges:=False
    ' Clean up
    Set oDocument = Nothing
    Set oNewDocument = Nothing
End Sub

The above code creates and saves a copy of the ActiveDocument with the same name but as a .docx formatted document (macro-free). The visible property in the .Add command means that it will not appear on screen and it is closed by the procedure. The new document will appear in Recent documents.