Solved

"Save in" in Excel

Posted on 2012-04-05
5
192 Views
Last Modified: 2012-06-12
We use a Private Sub Workbook_BeforeSave.  What is the code to include so that the file is automatically "saved in" a specified directory?
0
Comment
Question by:rutherfordcpa
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Shanan212
ID: 37812856
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    
    ActiveWorkbook.SaveAs Filename:= _
        "YOURDIRECTORY" & "YOURFILENAME" & ".xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False

    Application.EnableEvents = True
    Application.DisplayAlerts = True

Open in new window


The xlsx extension can be modified as well
0
 

Author Comment

by:rutherfordcpa
ID: 37812980
Thank you... perhaps I should have provided the exisiting code ...  can you incorporate your suggestion?


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Val(Application.Version) >= 12 Then
fname = Application.GetSaveAsFilename(filefilter:= _
"Excel Macro Enabled Workbook (*.xlsm), *.xlsm,Excel Macro Enabled Template (*.xltm), *.xltm,Excel 2003 Workbook (*.xls), *.xls,Excel 2003 Template (*.xlt), *.xlt", _
Title:="Save Workbook in Excel")
'Check if cancel was hit
If fname = False Then
Cancel = True
'ActiveWorkbook.SaveAs fname, FileFormatValue, CreateBackup:=False
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Else
End If
'Find the correct FileFormat that match the choice in the "Save as type" list
If fname <> False Then
Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
Case "xlt": FileFormatValue = 17
Case "xls": FileFormatValue = 56
Case "xlsm": FileFormatValue = 52
Case "xltm": FileFormatValue = 53
Case Else: FileFormatValue = 0
End Select
End If
If FileFormatValue = 0 Then
MsgBox "Sorry, unknown file extension"
Cancel = True
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
End If
Application.DisplayAlerts = False
Application.EnableEvents = False
Cancel = True
ActiveWorkbook.SaveAs fname, FileFormatValue, CreateBackup:=False
Application.DisplayAlerts = True
Application.EnableEvents = True
End If
End Sub
0
 
LVL 13

Expert Comment

by:Shanan212
ID: 37813150
Seems you already have the save as function implemented. Its just you are not saving it into a directory

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Val(Application.Version) >= 12 Then
fname = Application.GetSaveAsFilename(filefilter:= _
"Excel Macro Enabled Workbook (*.xlsm), *.xlsm,Excel Macro Enabled Template (*.xltm), *.xltm,Excel 2003 Workbook (*.xls), *.xls,Excel 2003 Template (*.xlt), *.xlt", _
Title:="Save Workbook in Excel")
'Check if cancel was hit
If fname = False Then
Cancel = True
'ActiveWorkbook.SaveAs fname, FileFormatValue, CreateBackup:=False
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
Else
End If
'Find the correct FileFormat that match the choice in the "Save as type" list
If fname <> False Then
Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
Case "xlt": FileFormatValue = 17
Case "xls": FileFormatValue = 56
Case "xlsm": FileFormatValue = 52
Case "xltm": FileFormatValue = 53
Case Else: FileFormatValue = 0
End Select
End If
If FileFormatValue = 0 Then
MsgBox "Sorry, unknown file extension"
Cancel = True
Application.EnableEvents = True
Application.ScreenUpdating = True
Exit Sub
End If
Application.DisplayAlerts = False
Application.EnableEvents = False
Cancel = True
fname = "DIRECTORYHERE" & fname
ActiveWorkbook.SaveAs fname, FileFormatValue, CreateBackup:=False
Application.DisplayAlerts = True
Application.EnableEvents = True
End If
End Sub

Open in new window


On this line (about 5 lines from bottom)

fname = "DIRECTORYHERE" & fname

Change whatever inside the quote to your directory. Eg: C:\Dir\

So that your new line would look like

fname = "C:\Dir\" & fname
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37818517
Here are a couple alternatives.  If you want them to not change the filename (and thus the type) and to save in a specific directory, there's no need for prompting, just use this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) 
dim strPathYouWantThemToSaveTo As String

    strPathYouWantThemToSaveTo = "C:\Dir\whatever\"
 
    Application.EnableEvents = False 
    Cancel = True 
    If CurDir <> strPathYouWantThemToSaveTo Then 
        ChDir strPathYouWantThemToSaveTo 
    End If 
    ThisWorkbook.Save 
    Application.EnableEvents = True 
     
End Sub 

Open in new window


Or, you can let them save it anywhere they choose, and any format they want, but also ensure a copy gets to the directory you want:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim strPathYouWantThemToSaveTo As String

    strPathYouWantThemToSaveTo = "C:\Dir\whatever\"
    
    ThisWorkbook.SaveCopyAs Filename:=strPathYouWantThemToSaveTo & ThisWorkbook.Name
    
End Sub

Open in new window


In either case, there's no need to be concerned with them changing format on the save.  And if they do, then open and re-edit and save, that new formatted copy would get to your directory as well.

This last, will catch their last save format, if you add this:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim strPathYouWantThemToSaveTo As String

    strPathYouWantThemToSaveTo = "C:\Dir\whatever\"
 
    Application.EnableEvents = False

    If CurDir <> strPathYouWantThemToSaveTo Then
        ChDir strPathYouWantThemToSaveTo
    End If
    ThisWorkbook.Save
    Application.EnableEvents = True
End Sub

Open in new window


However, you might want to prompt them to ensure they aren't closing because they want to scrap the file!

Just a couple options with less moving parts...perhaps :P

Dave
0
 

Author Comment

by:rutherfordcpa
ID: 37993846
Shanan212:

I am not able to get your code to work.

The user needs to be able to choose and change the file name but the default directory should to a folder in their My Documents... and depending on their version of Windows it may be Documents or My Documents.

If Mid(Path1, 4, 4) = "User" Then
fname = "\Documents\Folder1\"
Else
fname = "\My Documents\Folder1\"
End If
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Macro 6 48
macro for each dropdown 15 44
Fixing a embedded format 7 29
Excel - find text within text? 1 19
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now