#1
|
|||
|
|||
Force save after opening a template based file
My boss wants to set up our internal templates so that the user is forced to save the new document immediately upon opening it. In some of them the only VBA will be the save-forcer, in others there are macros that should be preserved. Here's what I have:
Code:
Private Sub Document_New() Dim intChoice As Integer Dim strPathName As String intChoice = 0 While intChoice = 0 intChoice = Application.FileDialog(msoFileDialogSaveAs).Show If intChoice <> 0 Then strPathName = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1) If Right$(strPathName, 1) = "x" Then strPathName = Left$(strPathName, Len(strPathName) - 1) & "m" End If ActiveDocument.SaveAs2 FileName:=strFileName, FileFormat:=wdFormatXMLDocumentMacroEnabled End If Wend End Sub Suggestions on how to delete this code after running are welcome. |
#2
|
||||
|
||||
Since the code is in the template, there is nothing to delete from the document. In any event, docx files cannot contain macros.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Erk? If the document based on the template is saved as a docx, Word will not open it and gives a useless error message. If I rename the docx to docm, Word opens it just fine and the code is still there.
I see that the autorun macro, being in the template, does not run when opening the docm. That helps, although I'd prefer to wipe it from documents that don't need it. But I still need to get it to save as a docm. |
#4
|
||||
|
||||
What is the supposedly "useless error message"? That changing the extension from docx to docm gets it working suggests you did not save it in the docx format in the first place.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
||||
|
||||
Your document also saves as strFilename, which doesn't exist in your code. As Paul indicates the document should be docx format and the code is in the template. The code when edited to
Code:
Option Explicit Private Sub Document_New() Dim intChoice As Integer Dim strPathName As String intChoice = Application.FileDialog(msoFileDialogSaveAs).Show If intChoice <> 0 Then strPathName = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1) ActiveDocument.SaveAs2 Filename:=strPathName, FileFormat:=wdFormatXMLDocument End If End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
||||
|
||||
Well spotted. Admittedly, I didn't study the code - I focussed on the question of the relationship between templates, macros and docx files.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Gosh, nobody seems to understand. The code I had did the save but I kept messing with it to try to get it to do what I wanted.
I WANT WORD TO SAVE IT AS DOCM AND I CANNOT GET WORD TO DO THAT!!1!1!!eleventy-one |
#8
|
|||
|
|||
Oh, and the error message tells me that Word can't open the file and cannot produce a reason why. Useless.
|
#9
|
||||
|
||||
Quote:
Code:
Private Sub Document_New() Dim i As Long, strFileName As String While i = 0 i = Application.FileDialog(msoFileDialogSaveAs).Show If i <> 0 Then strFileName = Application.FileDialog(msoFileDialogSaveAs).SelectedItems(1) strFileName = Split(strFileName, ".doc")(0) & ".docm" ActiveDocument.SaveAs2 FileName:=strFileName, FileFormat:=wdFormatXMLDocumentMacroEnabled End If Wend End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
As I already said, I made a mistake and posted non-working code. The working code saves as docx. I haven't had a chance yet to test your version. However, whether or not docx has access to macros is moot because WORD WILL NOT OPEN THE DOCUMENT WHEN IT HAS A DOCX EXTENSION!!
|
#11
|
||||
|
||||
Quote:
FileFormat:=wdFormatXMLDocumentMacroEnabled If you want to use docx, you would have to use: FileFormat:=wdFormatXMLDocument
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
Quote:
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to Force a "Save As" for a fillable form | cbalogh | Word | 6 | 01-27-2014 05:50 PM |
save as filtered HTML – force graphics format to .png | eNGiNe | Word | 2 | 01-23-2014 06:27 AM |
Force User to Save As | lgillespie | Word | 6 | 09-09-2013 03:13 PM |
VBA Code to force 'Save As' | rossi45 | Excel Programming | 1 | 05-11-2012 03:05 PM |
Template "File In Use" when opening 2 documents based on the same template | wendt | Word | 5 | 12-15-2009 12:37 AM |