Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2023, 11:45 AM
David Peck David Peck is offline instance a new dotm file from a Word dotm file and save with new file name Windows 10 instance a new dotm file from a Word dotm file and save with new file name Office 2021
Novice
instance a new dotm file from a Word dotm file and save with new file name
 
Join Date: Jun 2023
Posts: 3
David Peck is on a distinguished road
Default instance a new dotm file from a Word dotm file and save with new file name

I'm using this code to test if a file is a new instance just created from a dotm file and if so, saving it to a file with a docm extension.

When I attempt to reopen the file after closing it, Word reports the file as being corrupt.

The second parameter of ActiveDocument.SaveAs is WdSaveFormat - I'm looking at the MS page for WdSaveFormat
SaveAs Method | Microsoft Learn

I do not see a symbolic constant for the docm file type. if I try to set WdSaveFormat to the default (wdFormatDocument) I get a different error saying the file extension does not match.

Question: Seeking a method to instance a new file from a macro enabled template (dotm) and save it as a macro enabled file (docm)



If IsNull(ActiveDocument.Path) Or ActiveDocument.Path = "" Then
'GET WORKING FOLDER FROM USER
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr

.Title = "Select a Folder for this Word File and all of the associated Images to be stored"
.AllowMultiSelect = False
If .Show <> -1 Then
'NOTHING SELECTED NEED MSG
MsgBox "You must specify a Folder for this Word file to be stored", vbOKOnly + vbExclamation, "FOLDER NOT SELECTED"
Exit Sub
Else
'PATH IS SELECTED
selectedTargetPath = .SelectedItems(1)
End If
End With
Set fldr = Nothing

saveDate = Now

'SAVE FILE TO FIX ACTIVE DOCUMENT PATH SO FILES CAN BE COPIED TO THE CORRECT LOCATION
WordDocFilePathAndName = selectedTargetPath & "" & _
"8_5x11 Photos " & Year(saveDate) & "." & Month(saveDate) & "." & Day(saveDate) & _
" " & Hour(saveDate) & "." & Minute(saveDate) & "." & Second(saveDate) & ".docm"
ActiveDocument.SaveAs WordDocFilePathAndName
End If
Reply With Quote
  #2  
Old 08-18-2023, 02:28 AM
gmayor's Avatar
gmayor gmayor is offline instance a new dotm file from a Word dotm file and save with new file name Windows 10 instance a new dotm file from a Word dotm file and save with new file name Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The available fileformats are listed at WdSaveFormat enumeration (Word) | Microsoft Learn. Your macro has three issues. One is attributable to not observing good practice i.e. you have not declared the variables used, the others relate to the file naming.
Your variable selectedTargetPath does not include the path separator, so the probability is that the chosen path does not exist, when included in your WordDocFilePathAndName.
By default Word will save an unsaved document as docx format. If you want another format you have to tell it what format you require. In the case of DOCM format that would be wdFormatXMLDocumentMacroEnabled, or its numeric equivalent 13.

NOTE: documents saved as macro enabled will not contain the macros from the template on which they are based. If you want macros, you would have to add them before saving.

Code:
Dim fldr As FileDialog
Dim selectedTargetPath As String
Dim WordDocFilePathAndName As String
Dim saveDate As Date

    If IsNull(ActiveDocument.path) Or ActiveDocument.path = "" Then
        'GET WORKING FOLDER FROM USER
        Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
        With fldr

            .Title = "Select a Folder for this Word File and all of the associated Images to be stored"
            .AllowMultiSelect = False
            If .Show <> -1 Then
                'NOTHING SELECTED NEED MSG
                MsgBox "You must specify a Folder for this Word file to be stored", vbOKOnly + vbExclamation, "FOLDER NOT SELECTED"
                Exit Sub
            Else
                'PATH IS SELECTED
                selectedTargetPath = .SelectedItems(1) & "\"
            End If
        End With
        Set fldr = Nothing

        saveDate = Now

        'SAVE FILE TO FIX ACTIVE DOCUMENT PATH SO FILES CAN BE COPIED TO THE CORRECT LOCATION
        WordDocFilePathAndName = selectedTargetPath & "" & _
                                 "8_5x11 Photos " & Year(saveDate) & "." & Month(saveDate) & "." & Day(saveDate) & _
                                 " " & Hour(saveDate) & "." & Minute(saveDate) & "." & Second(saveDate) & ".docm"
        ActiveDocument.SaveAs FileName:=WordDocFilePathAndName, FileFormat:=wdFormatXMLDocumentMacroEnabled
    End If
    Set fldr = Nothing
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 08-18-2023, 06:24 AM
David Peck David Peck is offline instance a new dotm file from a Word dotm file and save with new file name Windows 10 instance a new dotm file from a Word dotm file and save with new file name Office 2021
Novice
instance a new dotm file from a Word dotm file and save with new file name
 
Join Date: Jun 2023
Posts: 3
David Peck is on a distinguished road
Default thanks, that did it

Thanks very much...

The MS reference page you pointed me to has a lot more of the format constants than the MS reference page I was looking at which did not have the right one...
Reply With Quote
  #4  
Old 08-18-2023, 06:47 AM
Italophile Italophile is online now instance a new dotm file from a Word dotm file and save with new file name Windows 11 instance a new dotm file from a Word dotm file and save with new file name Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by David Peck View Post
Thanks very much...

The MS reference page you pointed me to has a lot more of the format constants than the MS reference page I was looking at which did not have the right one...
That's because you were looking at a page for Word 2003. Getting the correct information is just a key press away given that you have both the Object Browser (F2), and online help (F1), built into the VBE.
Reply With Quote
Reply

Tags
macro enabled file, macro enabled template



Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a dotm file and can't add bookmarks because it reverts to normal doc Dpucher Word VBA 1 07-21-2019 08:15 PM
instance a new dotm file from a Word dotm file and save with new file name View/edit macro in DOTM file. eduzs Word VBA 5 04-14-2018 04:33 AM
Which .dotm file used for replies in Outlook 2013? kungfauxn00b Outlook 0 04-17-2014 01:41 AM
instance a new dotm file from a Word dotm file and save with new file name Editing .dotm/.dotx/dot file martinlest Word 9 10-09-2013 01:57 PM
instance a new dotm file from a Word dotm file and save with new file name Macros being stripped from dotm file Brian Muth Word 1 10-09-2012 10:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:23 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft