Consulting

Results 1 to 4 of 4

Thread: how do I change file extension on VBA that I already use

  1. #1
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location

    how do I change file extension on VBA that I already use

    I have a code that SaveAs excel file as MS 2003 (i.e. ".xls" extension). I changed the extension from .xls to .xlsx to make it SaveAs MS 2010. It worked but I get the following message when I tried to open the file:

    "The file cannot open because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

    here is the colde I used:

    Private Sub CommandButton1_Click()
      Dim sFileName As String
        Dim sPath As String
        CommandButton1.Enabled = False
        sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & _
        Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")
        If Len(Dir("\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy"), vbDirectory)) = 0 Then
            MkDir "\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy")
        End If
        sPath = "\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy")
        sFileName = sFileName & "_" & ID.Value & "_" & console.Value & "_" & prod.Value & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=sPath & "\" & sFileName, FileFormat:=xlNormal, ReadOnlyRecommended:=False
         MsgBox "Your log has been saved"
        ThisWorkbook.Close SaveChanges:=False
        
    End Sub
    Last edited by megha; 05-08-2014 at 08:23 AM. Reason: wrong code

  2. #2
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Quote Originally Posted by megha View Post
    I have a code that SaveAs excel file as MS 2003 (i.e. ".xls" extension). I changed the extension from .xls to .xlsx to make it SaveAs MS 2010. It worked but I get the following message when I tried to open the file:

    "The file cannot open because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

    here is the colde I used:

    Private Sub CommandButton1_Click()
      Dim sFileName As String
        Dim sPath As String
        CommandButton1.Enabled = False
        sFileName = Format(DateValue(Now()), "mmm_dd_yyyy") & "_" & _
        Format(TimeSerial(Hour(Now()), Minute(Now()), Second(Now())), "hh_mm_ss_AM/PM")
        If Len(Dir("\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy"), vbDirectory)) = 0 Then
            MkDir "\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy")
        End If
        sPath = "\\sptd.sabert.net/sites/NavLibrary/Completed_Reports\" & Format(DateValue(Now()), "mmm_yyyy")
        sFileName = sFileName & "_" & ID.Value & "_" & console.Value & "_" & prod.Value & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=sPath & "\" & sFileName, FileFormat:=51, ReadOnlyRecommended:=False
         MsgBox "Your log has been saved"
        ThisWorkbook.Close SaveChanges:=False
        
    End Sub

    Try as above, Fileformat:=51

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    The macro recorder is your friend. After recording, view the FileFormat value. I doubt it will be xlNormal.

  4. #4
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location
    Thank you! I used xlsm for my extension string and 52 for fileformat. It is working fine. Thanks for the help!

Posting Permissions

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