[英]MS Word vba to save .docm to .docx WITHOUT converting active document
我有一個帶有宏 (.docm) 的 MS Word 文檔
基於許多 StackOverflow 帖子,我編寫了一個宏以導出為 pdf 並另存為 .docx
我打開/編輯 .docm 文檔,它有一個 onSave 宏,可以將文檔保存為 .pdf 格式和 .docx 格式,我分發給其他人使用。 我將始終對 .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
我在“saveas”或“saveas2”中找不到任何參數來阻止活動文檔的這種轉換
此外,在“saveas”命令之后,原始宏中的任何附加行都不會執行,因為活動文檔不再包含宏。 我嘗試向宏添加行以重新打開原始 .docm,然后關閉 .docx,但這些推薦永遠不會執行。
我希望我只是錯過了一些簡單的東西?
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
上面的代碼創建並保存同名的 ActiveDocument 副本,但作為 .docx 格式的文檔(無宏)。 .Add 命令中的visible 屬性意味着它不會出現在屏幕上並被程序關閉。 新文檔將出現在最近的文檔中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.