首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MS Word vba在不转换活动文档的情况下将.docm保存为.docx

MS Word vba在不转换活动文档的情况下将.docm保存为.docx
EN

Stack Overflow用户
提问于 2020-10-02 01:12:57
回答 1查看 593关注 0票数 1

我有一个带有宏的MS Word文档(.docm)

基于许多StackOverflow帖子,我已经编写了一个宏导出为pdf并保存为.docx

我打开/编辑.docm文档,其中有一个onSave宏,它将文档保存为.pdf格式和.docx格式,我将其分发给其他人使用。我将始终对.docm文档进行更改。

我的问题是,这样做会将活动(打开的)文档从.docm转换为.docx,这样我就不再对.docm进行更改。

代码语言:javascript
运行
复制
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

我在"saveas“或"saveas2”中找不到任何参数来阻止活动文档的这种转换。

此外,在"saveas“命令之后,原始宏中的任何附加行都不会执行,因为活动文档不再包含宏。我尝试在宏中添加行以重新打开原始的.docm,然后关闭.docx,但这些建议从未执行过。

我希望我只是错过了一些简单的东西?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-03 04:05:11

代码语言:javascript
运行
复制
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

上面的代码以.docx格式的文档(无宏)创建并保存具有相同名称的ActiveDocument副本。.Add命令中的visible属性表示它不会显示在屏幕上,并且被过程关闭。新文档将出现在最近使用的文档中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64160234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档