+ Reply to Thread
Results 1 to 4 of 4

GetSaveAsFilename - change file type, runtime error 1004

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    GetSaveAsFilename - change file type, runtime error 1004

    Hello,

    I have a workbook that I'm trying to suggest a filename for when the user selects Save As. That's all I wanted to achieve but it's getting increasing complicated and now I have a runtime error I can't seem to find a way around.

    I want to allow the user the ability to select a .xlsx file type if they wish, which all works fine until it hits ThisWorkbook.SaveAs when I get a runtime 1004 error:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim sFullName As String, sFileName As String, sPathName As String, sCurDir As String
    Dim lOverwrite As Long
    Dim sExt As String
    Dim wsWork As Worksheet: Set wsWork = ThisWorkbook.Worksheets("Workings")
    
    Application.EnableEvents = True
    Application.ScreenUpdating = False
    
        'Store current directory in variable, restore it later
        sCurDir = CurDir
    
        'If the user clicks SAVE AS...
        If SaveAsUI = True Then
    
            'Switch to desired directory
            If Len(sPathName) > 0 Then
                ChDrive sPathName
                ChDir sPathName
            End If
    
            ' loop until unique name is entered
            Do
    
              'suggested filename for save as dialog box
              sFileName = Format(CVDate(Now), wsWork.Range("nrNameDateFormat").Value) & wsWork.Range("nrName").Value
              sFullName = Application.GetSaveAsFilename(sFileName, FileFilter:="Excel Macro-Enabled Workbook (*.xlsm),*.xlsm,Excel Workbook (*.xlsx),*.xlsx", _
              Title:="Save As")
                
              'exit if cancelled
              If Len(sFullName) = 0 Then GoTo Abort
              If sFullName = "False" Then GoTo Abort
    
              'if name is unique, exit loop and save file
              If Not FileExists(sFullName) Then Exit Do
    
              'Message user to confirm overwrite
              lOverwrite = MsgBox("A file named '" & GetFileName(sFullName) & "' already exists in the location chosen." & _
              vbNewLine & vbNewLine & "Do you want to overwrite the existing file?", vbYesNoCancel + vbQuestion, "File Exists")
    
              Select Case lOverwrite
                Case vbYes
                  'overwrite existing file
                  Exit Do
                Case vbNo
                  'do nothing, loop again to get new filename
                Case vbCancel
                  'bail out
                  GoTo Abort
              End Select
            Loop
            
            'finally, save the file using filename from above
            sExt = GetFileExt(sFullName)
            Application.EnableEvents = False
            Application.DisplayAlerts = False
            Debug.Print sFullName; sExt
            ThisWorkbook.SaveAs FileName:=sFullName, FileFormat:=sExt
            Application.DisplayAlerts = True
            Application.EnableEvents = True
    
            'do not resave the workbook
            Cancel = True
    
        End If
    
    Exit Sub
    
    Abort:
    
        'restore previous current directory (skip if error encountered)
        On Error Resume Next
        ChDrive sCurDir
        ChDir sCurDir
        On Error GoTo 0
    
        'do not resave the workbook
        Cancel = True
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    End Sub
    Originally I didn't have the FileFormat specified and it gave me a runtime 1004 error with text stating that it couldn't be saved as this file type.

    sFullName contains: \\...\Profile\Desktop\SaveAs TestMacro\2017-10-10RMSelfTestResultsSR.xlsx ("..." are to shorten the string for visibility in this posting, the full file path is actually correct)
    sExt contains: .xlsx

    The code works fine if saving as an .xlsm file.

    Anyone have any ideas what's wrong with this - or an alternative method of pre-populating the Save As filename?

    Any assistance appreciated

    Thanks, TC

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: GetSaveAsFilename - change file type, runtime error 1004

    ".xlsx" is not a valid value for the FileFormat argument. The correct value for that would be 51- the value of the xlOpenXMLWorkbook constant.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: GetSaveAsFilename - change file type, runtime error 1004

    Ah, I see. That simple!

    I've modified the code again, but it just feels really clunky - it works fine. I've adapted someone else code to start with so perhaps that's why it doesn't quite feel right. The structure feels more unwieldy that it need be:

     
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim sFullName As String, sFileName As String, sPathName As String, sCurDir As String, sExt As String
    Dim lOverwrite As Long
    Dim wsWork As Worksheet: Set wsWork = ThisWorkbook.Worksheets("Workings")
    Dim iExt As Integer
    Dim WarnMsg
    
        Application.EnableEvents = True
        Application.ScreenUpdating = False
        
        'Store current directory in variable, restore it later
        sCurDir = CurDir
        
        'If the user clicks SAVE AS...
        If SaveAsUI = True Then
            
            'Switch to desired directory
            If Len(sPathName) > 0 Then
                ChDrive sPathName
                ChDir sPathName
            End If
            
            ' loop until unique name is entered
            Do
            
                'suggested filename for save as dialog box
                sFileName = Format(CVDate(Now), wsWork.Range("nrNameDateFormat").Value) & wsWork.Range("nrName").Value
                sFullName = Application.GetSaveAsFilename(sFileName, FileFilter:="Excel Macro-Enabled Workbook (*.xlsm),*.xlsm,Excel Workbook (*.xlsx),*.xlsx", _
                Title:="Save As")
    
                'exit if cancelled
                If Len(sFullName) = 0 Then GoTo Abort
                If sFullName = "False" Then GoTo Abort
                
                'Check file type selected
                sExt = GetFileExt(sFullName)
                If sExt = ".xlsx" Then
                    iExt = 51 'no macros
                Else
                    iExt = 52 'macro enabled
                End If
                
                'if name is unique, exit loop and save file
                If Not FileExists(sFullName) Then
                    'If file type has been changed to .xlsx warn user that macros will be removed
                    If sExt = ".xlsx" Then
                        WarnMsg = MsgBox("The following features cannot be saved in macro-free workbooks:" & vbNewLine & vbNewLine & _
                        "- VB Project" & vbNewLine & vbNewLine & "To save a file with these features click No then choose a " & _
                        "macro-enabled file type in the file list." & vbNewLine & "To continue saving as a macro free workbook " & _
                        "click Yes.", vbInformation + vbYesNoCancel, "Microsoft Excel")
                        Select Case WarnMsg
                            Case vbYes
                                'overwrite existing file
                                Exit Do
                            Case vbNo
                                'do nothing - resume progress through loop
                            Case vbCancel
                                GoTo Abort
                        End Select
                    Else 'extension remains .xlsm
                        'overwrite existing file
                        Exit Do
                    End If
                Else 'filename NOT unique
                    'Message user to confirm overwrite
                    lOverwrite = MsgBox("A file named '" & GetFileName(sFullName) & "' already exists in the location chosen." & _
                    vbNewLine & vbNewLine & "Do you want to overwrite the existing file?", vbYesNoCancel + vbQuestion, "File Exists")
                    
                    Select Case lOverwrite
                        Case vbYes
                            'If file type has been changed to .xlsx warn user that macros will be removed
                            If sExt = ".xlsx" Then
                                WarnMsg = MsgBox("The following features cannot be saved in macro-free workbooks:" & vbNewLine & vbNewLine & _
                                "- VB Project" & vbNewLine & vbNewLine & "To save a file with these features click No then choose a " & _
                                "macro-enabled file type in the file list." & vbNewLine & "To continue saving as a macro free workbook " & _
                                "click Yes.", vbInformation + vbYesNoCancel, "Microsoft Excel")
                                Select Case WarnMsg
                                    Case vbYes
                                        'overwrite existing file
                                        Exit Do
                                    Case vbNo
                                        'do nothing - resume progress through loop
                                    Case vbCancel
                                        GoTo Abort
                                End Select
                            Else 'extension remains .xlsm
                                'overwrite existing file
                                Exit Do
                            End If
                        Case vbNo
                            'do nothing, loop again to get new filename
                        Case vbCancel
                            'bail out
                            GoTo Abort
                    End Select
                End If
            Loop
            
            'Save the file using filename provided
            Application.EnableEvents = False
            Application.DisplayAlerts = False
            ThisWorkbook.SaveAs FileName:=sFullName, FileFormat:=iExt
            Application.DisplayAlerts = True
            Application.EnableEvents = True
            
            'do not resave the workbook
            Cancel = True
            
        End If
        
    Exit Sub '-------------------------------------------------------
        
    Abort:
        
        'restore previous current directory (skip if error encountered)
        On Error Resume Next
        ChDrive sCurDir
        ChDir sCurDir
        On Error GoTo 0
        
        'do not resave the workbook
        Cancel = True
        
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    
    End Sub
    Am I missing something, or worrying about nothing?

  4. #4
    Forum Contributor
    Join Date
    10-22-2012
    Location
    London, UK
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    162

    Re: GetSaveAsFilename - change file type, runtime error 1004

    Marking post as solved because my initial problem has been - but would still appreciate any input regarding streamlining structure.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Runtime Error 1004 - File Format Invalid
    By NEU2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-15-2017, 02:32 PM
  2. [SOLVED] runtime error 1004 Getsaveasfilename of object application failed
    By michelle 1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-24-2017, 09:57 AM
  3. [SOLVED] Runtime Error 1004 cant find the file, but it knows it's name
    By PJC2013 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-25-2016, 06:43 AM
  4. Runtime error 1004 when trying to save file via VBA
    By avh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2015, 03:20 AM
  5. [SOLVED] Receiving Runtime Error 1004 -Cannot Access file
    By Dean Staples in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-30-2013, 09:27 PM
  6. Runtime Error 1004 : file could not be accessed
    By deepyogi in forum Excel General
    Replies: 1
    Last Post: 07-24-2009, 11:54 AM
  7. File Name Causes Runtime Error 1004
    By Robert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-10-2005, 10:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1