![]() |
#1
|
|||
|
|||
![]()
I have word macro enabled template file that that users use to create a new document from.
Is there a way to have the file default to be saved as a docm file for just that template file when the initially save their new document? I still want all other files to be saved under a normal docx format. I starting to search google, but thinking/guessing maybe something with a BeforeSave Event? |
#2
|
|||
|
|||
![]()
Yes you should be able to use the BeforeSave event. See if this gets you started:
Code:
Option Explicit Private WithEvents m_oThisApp As Application Private Sub Class_Initialize() 'Syncronize class with application. Set m_oThisApp = Word.Application lbl_Exit: Exit Sub End Sub Private Sub m_oThisApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean) Dim oDialog As Dialog If ActiveDocument.AttachedTemplate = ThisDocument.AttachedTemplate Then Cancel = True Set oDialog = Dialogs(wdDialogFileSaveAs) With oDialog .Format = 13 .Show End With End If End Sub |
#3
|
|||
|
|||
![]()
I think the code you provide makes sense, but I couldn't seem to get it work last night. I think I need to place the beginning WithEvent in a Class Module, and the rest either in ThisDocument or normal module.
I need to get some real work here done this morning, but will give it a 2nd try again later today. And all of this does belong in the word macro template dotm file correct? |
#4
|
|||
|
|||
![]()
Put this code in a standard module of your template:
Code:
Option Explicit Private m_ThisApp As clsThisApp Sub AutoNew() InitiateAppClass lbl_Exit: Exit Sub End Sub Sub AutoOpen() InitiateAppClass lbl_Exit: Exit Sub End Sub Sub InitiateAppClass() Set m_ThisApp = New clsThisApp End Sub |
#5
|
|||
|
|||
![]()
A HUGE Thanks for helping with that.
I'm going to now try and modify that same information to work with an Excel file. Similar situation. Saving a new excel file off an an excel macro template to default to xlsm vs xlsx. |
#6
|
|||
|
|||
![]()
Think I hit a small snag?
I want the document to only force the saving as a .docm file on the initial save as. The orginal code seemed to want to force a SaveAs during each save of the document as.. So I modded the If Statement to check if the SaveAsUI is true, and that seems to work, but...... What is strange is that you edit the document, then attempt to close the file and chose not to save, vba throws an error; runtime error 4248. It appears to happen only if the document is open by itself as Word attempts to close the application and gives the error. If there are other word files open, it does not give the error. I think I just need to tweak the If Statement some Code:
Private Sub m_ThisApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean) Dim oDialog As Dialog If SaveAsUI = True And ActiveDocument.AttachedTemplate = ThisDocument.AttachedTemplate Then Cancel = True Set oDialog = Dialogs(wdDialogFileSaveAs) With oDialog .Format = 13 .Show End With End If End Sub |
#7
|
|||
|
|||
![]()
Try this. I also think the Cancel may have been in the wrong place:
Code:
Private Sub m_oThisApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean) Dim oDialog As Dialog If ActiveDocument.FullName = ThisDocument.FullName Then Exit Sub If ActiveDocument.Path <> vbNullString Then Exit Sub If ActiveDocument.AttachedTemplate = ThisDocument.AttachedTemplate Then Set oDialog = Dialogs(wdDialogFileSaveAs) With oDialog .Format = 13 .Show End With 'I had the cancel in the wrong place. Suppress the normal dialog. Cancel = True End If End Sub |
#8
|
|||
|
|||
![]()
Greg,
Thanks again!! I just to trying out the new code, and things work as they should. Problem solved! |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
flutterby1 | Word | 4 | 10-08-2014 10:55 AM |
Quick Parts entries in .docm template lost | kjworduser | Word | 7 | 07-31-2013 02:19 PM |
![]() |
Moz | Word | 1 | 12-20-2012 04:23 PM |
Docm content dissapearing when I open the file? | shabbaranks | Word | 2 | 07-18-2012 01:13 AM |
![]() |
tinfanide | Word VBA | 6 | 12-06-2011 03:02 PM |