简体   繁体   中英

MS Word vba to save .docm to .docx WITHOUT converting active document

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM