Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-30-2022, 07:32 AM
grNadpa grNadpa is offline Set (temporary) SaveAs path Windows 10 Set (temporary) SaveAs path Office 2016
Advanced Beginner
Set (temporary) SaveAs path
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default Set (temporary) SaveAs path


Cannot seem to set the path for Word 2016 SaveAs screen. The statement
Code:
MsgBox Options.DefaultFilePath(wdDocumentsPath)
displays the directory I want.
But, after executing
Code:
'source: based on https://wordmvp.com/FAQs/MacrosVBA/SetDefFilename.htm
'change properties
Dim dtToday As Date
  dtToday = Date
    If Documents.Count > 0 Then
         Set dlgProp = Dialogs(wdDialogFileSummaryInfo)
         ' Establish title
         dlgProp.Title =  "Waiver_" & dtToday & ".docx"
         ' Set the values
         dlgProp.Execute
         ' Show the dialog for testing purposes
         dlgProp.Show
    End If
The SaveAs dialog box shows up but with an entirely different path.

And with the bogus message
Quote:
You don't currently have permission to access this folder
(It's the the path I last used to save a .docm document).

The keywords I use in this forum's search bring up how to change file names, but not directories.

I guess this ability qualifies merely as a "nice to have". But it really would be a nice-to-have.
Reply With Quote
  #2  
Old 04-01-2022, 08:49 PM
gmayor's Avatar
gmayor gmayor is offline Set (temporary) SaveAs path Windows 10 Set (temporary) SaveAs path Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

Your code simply sets a document property. It does not set the file path. To change the path, use
Code:
ChDir "path"
However it is not necessary to do that to save a document in a specific location. If you want to save a document in a specific folder you must set that folder first e.g.
Code:
Dim dtToday As String
Dim sPath As String
    sPath = Options.DefaultFilePath(wdDocumentsPath) & "\"
    dtToday = Format(Date, "ddmmyyyy")
    MsgBox sPath & "Waiver_" & dtToday & ".docx"
    ActiveDocument.SaveAs2 sPath & "Waiver_" & dtToday & ".docx"
Note that
Code:
Dim dtToday As Date
  dtToday = Date
produces the system short date format and that may result in illegal filename characters e.g. 02/04/2022 so set dtDate as a string variable and format the date to produce legal filename characters e.g 02042022 as in the previous example.
__________________
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 04-02-2022, 06:12 AM
grNadpa grNadpa is offline Set (temporary) SaveAs path Windows 10 Set (temporary) SaveAs path Office 2016
Advanced Beginner
Set (temporary) SaveAs path
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default

Thank you @gmayor
Please confirm that I modified your example correctly. Specifically I added
Code:
Options.DefaultFilePath(wdDocumentsPath) = "C:\Users\Brian\Documents"
such that the entire test sub reads
Code:
Private Sub CommandButton3_Click()
Dim dtToday As String
Dim sPath As String
    Options.DefaultFilePath(wdDocumentsPath) = "C:\Users\Brian\Documents" 
    sPath = Options.DefaultFilePath(wdDocumentsPath) & "\"
    dtToday = Format(Date, "ddmmyyyy")
    MsgBox sPath & "Waiver_" & dtToday & ".docx"
    ActiveDocument.SaveAs2 sPath & "Waiver_" & dtToday & ".docx"
End Sub
Final question: Does capturing the path before changing it, then restoring it after the SaveAs2 serve as 'Best Practices' ?
Reply With Quote
  #4  
Old 04-02-2022, 08:11 PM
gmayor's Avatar
gmayor gmayor is offline Set (temporary) SaveAs path Windows 10 Set (temporary) SaveAs path Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
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 ofgmayor has much to be proud of
Default

There is no need to change the path. Simply tell the macro where to save the document. The following will save the file in the appropriate place regardless of the current save location.

Code:
Private Sub CommandButton3_Click()
Dim dtToday As String
Dim sPath As String
    sPath = Options.DefaultFilePath(wdDocumentsPath) & "\"
    dtToday = Format(Date, "ddmmyyyy")
    ActiveDocument.SaveAs2 sPath & "Waiver_" & dtToday & ".docx"
End Sub
__________________
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
  #5  
Old 04-03-2022, 06:40 AM
grNadpa grNadpa is offline Set (temporary) SaveAs path Windows 10 Set (temporary) SaveAs path Office 2016
Advanced Beginner
Set (temporary) SaveAs path
 
Join Date: Mar 2022
Posts: 46
grNadpa is on a distinguished road
Default

@gmayor wrote
Quote:
There is no need to change the path.
I don't understand. When I insert the code
Code:
MsgBox Options.DefaultFilePath(wdDocumentsPath) & "\"
The the mesgbox displays
Quote:
c:\users\brian\documents\integro\waiver\
The word (.dotm) document that will contain this code will be invoked from a template folder. So I want to save the completed document in another folder (as .docx)
Does that not require resetting the the path through Options.Default(wdDocumentsPath)?

Last edited by grNadpa; 04-03-2022 at 06:41 AM. Reason: typos
Reply With Quote
Reply

Tags
path, save as docx, vba code

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Temporary work file reglilly Word 4 05-15-2018 12:13 PM
How to delete a temporary ~$ Word file? Belloc Word 3 08-16-2017 06:24 AM
Set (temporary) SaveAs path Change word temporary location mkhorrami Word 1 01-11-2016 04:09 AM
Where does Word 2010 save temporary files? JonahSnow Word 1 06-03-2015 12:31 AM
Set (temporary) SaveAs path VBS saveas API behaviour is different from Menu saveas Ahmed AbuZekry Word VBA 2 03-23-2015 02:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:36 PM.


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