VBA for SaveAs, limited options for filetype, fixed destination folder

DutchKevin

Board Regular
Joined
Apr 13, 2011
Messages
133
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I've been searching around a while now and found several "almost what I need" solutions.
The thing I'm working on is a VBA macro that:
- triggers a save-as dialog
- uses the current file name, allows a different name
- limits filetypes in the list to xlsx, xlsm
- allows changing current filetype e.g. from xlsx -> xlsm, or vice versa
- and that starts from a predefined folderlocation on my c-drive, from a variable

So far I've explored options that use Application.Dialogs(xlDialogSaveAs) and Application.FileDialog(msoFileDialogSaveAs) but it never really gets all the way.
Either the destination folder does not "stick", or the file extension filter is failing, or the file type changing is causing error. Each option I test seems to have at least one part from the wish-list missing.
Could anyone please point me in the right direction? I assume that what I attempt here is not impossible ???

Thanks in advance
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The custom function below uses the Application.GetSaveAsFilename method, which opens a file dialog, just for the user to determine a destination folder and a file name. Therefore the code has to provide a way to actually save the desired workbook. The code below takes this into account .

VBA Code:
Public Function LimitedSaveAs(ByVal argWb As Workbook, ByVal argFolder As String) As String

    Const FILTERS As String = "Excel Workbook (*.xlsx), *.xlsx, " & _
                              "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm"
    
    Dim fso         As Object
    Dim Result      As Variant
    Dim InitialPath As String
    Dim FileExt     As String
    Dim FileFormat  As XlFileFormat
    Dim FilterIndex As Long
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    InitialPath = IIf(Len(argFolder) = 0, argWb.Path & "\", IIf(Right(argFolder, 1) = "\", argFolder, argFolder & "\"))
    If fso.FolderExists(InitialPath) Then
        InitialPath = InitialPath & argWb.Name
    Else
        InitialPath = argWb.FullName
    End If

    If Len(argWb.Path) = 0 Then
        FilterIndex = 1
    Else
        FileExt = Right(InitialPath, Len(InitialPath) - InStrRev(InitialPath, "."))
        Select Case LCase(FileExt)
            Case "xlsx":    FilterIndex = 1
            Case "xlsm":    FilterIndex = 2
            Case Else:      FilterIndex = 1
        End Select
    End If

    Result = Application.GetSaveAsFilename(InitialPath, FILTERS, FilterIndex, "Save as")
    If Not VarType(Result) = vbBoolean Then

        FileExt = Right(Result, Len(Result) - InStrRev(Result, "."))
        Select Case LCase(FileExt)
            Case "xlsx":    FileFormat = xlOpenXMLWorkbook
            Case "xlsm":    FileFormat = xlOpenXMLWorkbookMacroEnabled
            Case Else:      FileFormat = xlOpenXMLWorkbook
        End Select
        Application.DisplayAlerts = False
        argWb.SaveAs FileName:=Result, FileFormat:=FileFormat
        Application.DisplayAlerts = True
        LimitedSaveAs = Result
    Else
        
        ' cancel was pressed
    End If
End Function


Usage example:
VBA Code:
Public Sub DutchKevin()

    Dim ResultFile  As String
    Dim FolderName  As String
    Dim wb          As Workbook

    FolderName = "C:\Users\DutchKevin\Documents"    ' <<<< change to suit
    Set wb = ActiveWorkbook                         ' <<<< change to suit

    ResultFile = LimitedSaveAs(wb, FolderName)
    
    If Len(ResultFile) > 0 Then
        MsgBox ResultFile & " has been saved", vbInformation
    Else
        MsgBox "Workbook " & wb.Name & " has not been saved!", vbExclamation
    End If
End Sub
 
Upvote 0
Solution
Hello GWteB
I did some quick testing. The day is at it's end here, and this looks to do exactly what I need!
How the VBA works, that I need to process a bit further, but it looks very elegant.
Thanks already a lot for helping me forward, and I'll post back any questions I may find tomorrow
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top