简体   繁体   English

MS Word vba 将 .docm 保存为 .docx 而不转换活动文档

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

I have an MS Word document with macros (.docm)我有一个带有宏 (.docm) 的 MS Word 文档

Based on many StackOverflow posts, I've written a macro to export as a pdf and save as a .docx基于许多 StackOverflow 帖子,我编写了一个宏以导出为 pdf 并另存为 .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.我打开/编辑 .docm 文档,它有一个 onSave 宏,可以将文档保存为 .pdf 格式和 .docx 格式,我分发给其他人使用。 I will always be making my changes to the .docm document.我将始终对 .docm 文档进行更改。

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.我的问题是这样做会将活动(打开)文档从 .docm 转换为 .docx,这样我就不再对 .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"我在“saveas”或“saveas2”中找不到任何参数来阻止活动文档的这种转换

Furthermore, after the "saveas" command, any additional lines in the original macro are not executed because the active document no longer contains macros.此外,在“saveas”命令之后,原始宏中的任何附加行都不会执行,因为活动文档不再包含宏。 I tried adding lines to the macro to reopen the original .docm and then close the .docx but those commends never execute.我尝试向宏添加行以重新打开原始 .docm,然后关闭 .docx,但这些推荐永远不会执行。

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).上面的代码创建并保存同名的 ActiveDocument 副本,但作为 .docx 格式的文档(无宏)。 The visible property in the .Add command means that it will not appear on screen and it is closed by the procedure. .Add 命令中的visible 属性意味着它不会出现在屏幕上并被程序关闭。 The new document will appear in Recent documents.新文档将出现在最近的文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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