Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-17-2016, 10:41 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default 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
But this saves as a docx (I don't trust the users to select docm).

Suggestions on how to delete this code after running are welcome.
Reply With Quote
  #2  
Old 03-17-2016, 05:14 PM
macropod's Avatar
macropod macropod is offline Force save after opening a template based file Windows 7 64bit Force save after opening a template based file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 03-18-2016, 08:17 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 03-18-2016, 02:16 PM
macropod's Avatar
macropod macropod is offline Force save after opening a template based file Windows 7 64bit Force save after opening a template based file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 03-19-2016, 12:10 AM
gmayor's Avatar
gmayor gmayor is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

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
saves as docx and the document can certainly be opened without error.
__________________
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
  #6  
Old 03-19-2016, 12:27 AM
macropod's Avatar
macropod macropod is offline Force save after opening a template based file Windows 7 64bit Force save after opening a template based file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #7  
Old 03-21-2016, 10:17 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 03-21-2016, 10:47 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default

Oh, and the error message tells me that Word can't open the file and cannot produce a reason why. Useless.
Reply With Quote
  #9  
Old 03-21-2016, 02:50 PM
macropod's Avatar
macropod macropod is offline Force save after opening a template based file Windows 7 64bit Force save after opening a template based file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by JonFleming View Post
Oh, and the error message tells me that Word can't open the file and cannot produce a reason why. Useless.
The problem is entirely of your own making, because you're getting the filename with one variable, then saving it with another, empty, one. Try:
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
Why you think saving as a docm file is important is unclear. A docx file has access to all the macros in its template (while the template remains accessible), exactly as a docm file does. Conversely, merely saving a file in the docm format doesn't add the template's macros to it.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 03-22-2016, 08:39 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default

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!!

Reply With Quote
  #11  
Old 03-22-2016, 02:08 PM
macropod's Avatar
macropod macropod is offline Force save after opening a template based file Windows 7 64bit Force save after opening a template based file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by JonFleming View Post
WORD WILL NOT OPEN THE DOCUMENT WHEN IT HAS A DOCX EXTENSION!!
Hardly surprising when you tell it to save the file with:
FileFormat:=wdFormatXMLDocumentMacroEnabled
If you want to use docx, you would have to use:
FileFormat:=wdFormatXMLDocument
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 03-23-2016, 08:45 AM
JonFleming JonFleming is offline Force save after opening a template based file Windows 10 Force save after opening a template based file Office 2016
Novice
Force save after opening a template based file
 
Join Date: Feb 2016
Posts: 15
JonFleming is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
The problem is entirely of your own making, because you're getting the filename with one variable, then saving it with another, empty, one. Try:
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
Why you think saving as a docm file is important is unclear. A docx file has access to all the macros in its template (while the template remains accessible), exactly as a docm file does. Conversely, merely saving a file in the docm format doesn't add the template's macros to it.
Your code does exactly what I want it to do. Thank you.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Force save after opening a template based file How to Force a "Save As" for a fillable form cbalogh Word 6 01-27-2014 05:50 PM
Force save after opening a template based file save as filtered HTML – force graphics format to .png eNGiNe Word 2 01-23-2014 06:27 AM
Force save after opening a template based file Force User to Save As lgillespie Word 6 09-09-2013 03:13 PM
Force save after opening a template based file 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

Other Forums: Access Forums

All times are GMT -7. The time now is 01:32 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