So, I have a Word-Template with Macros inside. What do I want to achieve?
When the File is saved I want to save the Word-File as a new docx-File with new name and new destination.
When I click save now the file is already saved correctly with the new name and new destination (also as a *docx file) but the macro is still inside so when I press save now the macro is still executed (this is what I want to remove).
So how can I remove when saving the File all the macros in the new file?
My current code is:
Sub FileSave()
'
' saveFile Macro
'
'
DocDate = ActiveDocument.Tables(1).Cell(4, 2)
GCCName = ActiveDocument.Tables(1).Cell(8, 1)
FileExtension = ".docx"
matchedStr = GCCName & " - " & DocDate & FileExtension
matchedStr = Replace(matchedStr, "", "")
matchedStr = Replace(matchedStr, Chr(13), "")
ActiveDocument.Save
ChangeFileOpenDirectory "########\"
ActiveDocument.SaveAs2 FileName:= _
matchedStr, FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", _
AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _
:=False, SaveAsAOCELetter:=False, CompatibilityMode:=15
End Sub
If you have a Word template (dotm) you should not open the template to create a document: you should just create a new document right from the start. If the new document is being generated by code then use Documents.Add
rather than Documents.New
. The new document will contain no macros and can be saved as docx with no problem.
These days, it is possible to save a template file to a file with the docx extension and to a "document without macros" file format - that will work. (In earlier versions of Word, with the *.dot file format, it did not.)
HOWEVER, the document will still have a link to the template and be able to "see" the macros via that link. This may be why you think the macros are being saved in the docx document. If such a file is sent "out of house" (to a location with no way to access the template in the folder where it's stored) then the macros will not show up or be available.
If you want to completely dissociate the document from its template, attach it to a different template (usually Normal.dotm because that's available on all Word installations). Example based on the code in the question:
ActiveDocument.AttachedTemplate = NormalTemplate
This needs to be at the end of the code since it will dissociate it from the code that's performing the action.
A tip for the code in the question: working with ActiveDocument
can be unreliable - it's not certain that it will always be the document intended (the user could activate a different one, for example). A more reliable approach is to work with a Document
object. The same applies to other things, such as tables. For example:
Dim doc as Word.Document
Dim tbl as Word.Table
Set doc = ActiveDocument
' do things with the document...
Set tbl = doc.Tables(1)
' do things with the table
DocDate = tbl.Cells(4,2).Range.Text
GCCName = tbl.Cell(8, 1).Range.Text
doc.Save
Alternative for docm files, taken from my project
NameNoExtension is the name of the new file. Put your path where SelectSaveDir() is in my code. The code opens a copy of the file where the the code is, deletes all the vba Modules and saves the copied file as docx.
Sub SaveCopyAsDocx(ByVal NameNoExtension As String)
Dim NewDocument As Object
Set NewDocument = Documents.Add(ThisDocument.FullName)
Dim Modul As Object
For Each Modul In NewDocument.VBProject.VBComponents
If Modul.Name <> "ThisDocument" Then
NewDocument.VBProject.VBComponents.Remove Modul
End If
Next
NewDocument.SaveAs SelectSaveDir() & "\" & NameNoExtension & ".docx"
End Sub
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.