Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-13-2016, 10:15 AM
dwirony dwirony is offline Changing file directory from a temp folder Windows 7 64bit Changing file directory from a temp folder Office 2003
Advanced Beginner
Changing file directory from a temp folder
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default Changing file directory from a temp folder

Hello all,

I am having trouble getting my macro to save my document into the desired location. The program I use attempts to save documents to a default temporary folder, and even after recording the process it still won't save my document to my desired location. Am I doing something wrong? How can I get my macro to override the default location for documents?

Code:
Sub Savetolocation
'
'
ChangeFileOpenDirectory "C:\Users\dwirony\My Documents\Overflow Folder\"
    ActiveDocument.SaveAs2 FileName:=ActiveDocument.Fullname, FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=14
End Sub

Reply With Quote
  #2  
Old 12-13-2016, 01:32 PM
Guessed's Avatar
Guessed Guessed is offline Changing file directory from a temp folder Windows 10 Changing file directory from a temp folder Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

ActiveDocument.FullName includes the entire path so you are simply saving the file over itself. Try
Code:
Sub Savetolocation()
  Dim sLocation As String
  sLocation = "C:\Users\dwirony\My Documents\Overflow Folder\" & ActiveDocument.Name
  ActiveDocument.SaveAs2 FileName:=sLocation, FileFormat:=wdFormatXMLDocument, CompatibilityMode:=14
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-13-2016, 03:03 PM
dwirony dwirony is offline Changing file directory from a temp folder Windows 7 64bit Changing file directory from a temp folder Office 2003
Advanced Beginner
Changing file directory from a temp folder
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

Quote:
Originally Posted by Guessed View Post
ActiveDocument.FullName includes the entire path so you are simply saving the file over itself. Try
Code:
Sub Savetolocation()
  Dim sLocation As String
  sLocation = "C:\Users\dwirony\My Documents\Overflow Folder\" & ActiveDocument.Name
  ActiveDocument.SaveAs2 FileName:=sLocation, FileFormat:=wdFormatXMLDocument, CompatibilityMode:=14
End Sub
Ahh, that makes sense. Your solution is getting it to save to the right file location, although now it's no longer changing the document to a .docx. I tried adding "& ".docx"" to the end of sLocation but the file name is retaining ".rtf" in the name now. How could I get it to change from rtf to docx?

Thank you for your assistance!
Reply With Quote
  #4  
Old 12-13-2016, 03:11 PM
Guessed's Avatar
Guessed Guessed is offline Changing file directory from a temp folder Windows 10 Changing file directory from a temp folder Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

That requires a bit more effort. This takes the current filename before the first period (assuming the first period is at the file extension), and appends the new file extension
Sub Savetolocation()
Dim sLocation As String, sFile as String
sFile = Split(ActiveDocument.Name,".")(0) & ".docx"
sLocation = "C:\Users\dwirony\My Documents\Overflow Folder\" & sFile
ActiveDocument.SaveAs2 FileName:=sLocation, FileFormat:=wdFormatXMLDocument, CompatibilityMode:=14
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 12-13-2016, 03:14 PM
Guessed's Avatar
Guessed Guessed is offline Changing file directory from a temp folder Windows 10 Changing file directory from a temp folder Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,159
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Whoops, I tried to edit and lost the message.

Try this
Code:
Sub Savetolocation()
  Dim sLocation As String, sFilename as String
  sFilename = Split(ActiveDocument.Name,".")(0) & ".docx"
  sLocation = "C:\Users\dwirony\My Documents\Overflow Folder\"
  ActiveDocument.SaveAs2 FileName:=sLocation & sFilename, FileFormat:=wdFormatXMLDocument, CompatibilityMode:=14
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 12-13-2016, 03:17 PM
dwirony dwirony is offline Changing file directory from a temp folder Windows 7 64bit Changing file directory from a temp folder Office 2003
Advanced Beginner
Changing file directory from a temp folder
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default

Quote:
Originally Posted by Guessed View Post
That requires a bit more effort. This takes the current filename before the first period (assuming the first period is at the file extension), and appends the new file extension
Sub Savetolocation()
Dim sLocation As String, sFile as String
sFile = Split(ActiveDocument.Name,".")(0) & ".docx"
sLocation = "C:\Users\dwirony\My Documents\Overflow Folder\" & sFile
ActiveDocument.SaveAs2 FileName:=sLocation, FileFormat:=wdFormatXMLDocument, CompatibilityMode:=14
End Sub
Luckily for me the first period is at the file extension. Genius solution, thanks for the help Andrew.
Reply With Quote
  #7  
Old 12-13-2016, 09:41 PM
gmayor's Avatar
gmayor gmayor is offline Changing file directory from a temp folder Windows 10 Changing file directory from a temp folder Office 2016
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

If there are more than one period characters, using split creates a few more problems. It would be better to use

Code:
    sFile = ActiveDocument.Name
    sFile = Left(sFile, InStrRev(sFile, Chr(46))) & "docx"
    'or, if you want to add some text to the end of the filename
    'sFile = Left(sFile, InStrRev(sFile, Chr(46)) - 1) & "-sometext.docx"
__________________
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
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing file directory from a temp folder Word 2010 Macro to rename Folder in Directory staicumihai Word VBA 2 10-28-2016 03:01 AM
Changing file directory from a temp folder vba code for folder name, subfolder directory, file name & revision times klpw Excel Programming 2 12-24-2015 12:31 AM
Changing file directory from a temp folder Command button - save in temp folder and send email with attachment bigbird69 Word VBA 13 11-18-2012 10:06 PM
outlook 2003 always not remove temp copy of opened attachments in temporary folder c.itech Outlook 0 06-20-2011 10:34 PM
Error 451 - Couldīt Open Temp file greenberet Outlook 1 09-06-2010 05:35 AM

Other Forums: Access Forums

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