Results 1 to 7 of 7
  1. #1
    jadown is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    11

    Access Opens Excel, Edit, SaveAs, then Close

    Hello,



    I am trying to open an Excel Workbook from a FileDialog where the
    user can select the file, make changes (in this case formatting), then SaveAs a
    different name in the same path as the original file from a command button on an
    Access form. Everything works except saving the file as a different name.




    Quote:

    Dim
    fd As Object, sFileName As String


    Set fd =
    Application.FileDialog(3)
    With fd
    .AllowMultiSelect = False
    .Title =
    "Browse to Select a File"
    If .Show = -1 Then sFileName =
    .SelectedItems(1)
    End With

    'Change the column headings on the
    spreadsheet
    If Len(sFileName) = 0 Then Exit Sub

    Dim xlObj As
    Object




    Set xlObj =
    CreateObject("Excel.Application")


    With xlObj
    .Workbooks.Open
    sFileName
    .Visible =
    True
    .Worksheets.Select
    .Cells.Select
    .Selection.Replace What:="=",
    Replacement:="'="
    .ActiveWorkbook.SaveAs FileName:="NewFile" &
    sFileName
    .Quit
    End With
    Set fd = Nothing
    Set xlObj = Nothing

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    What happens - error message, wrong results, nothing?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jadown is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    11
    Nothing happens

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    The SaveAs code is wrong.

    The value of sFileName is the full file path/name, like: C:\foldername\Book1.xlsx

    This means cannot just concatenate to more text.

    .ActiveWorkbook.SaveAs FileName:=Replace(sFileName, ".xlsx", "_NewFile.xlsx")
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    jadown is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    11
    Thanks for you help. I do have a question though. Since, I am opening the file from a dialog box how can I make the save as save to where ever the user opens the file? Because multiple users will be opening the file from their local computers.

  6. #6
    jadown is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    11
    Okay I tried to add a string for the select path but now I'm getting a run-time error '1004. Microsoft Excel cannot access the file C:\6D233000. The file name or path does not exist, The file is being used by another program...etx.

    Code:
    Dim intChoice As Integer
    Dim strPath As String
    Dim fd As Object, sFileName As String
    'Opens the Excel file
    Set fd = Application.FileDialog(3)
    With fd
    .AllowMultiSelect = False
    .Title = "Browse to Select a File"
    If .Show = -1 Then sFileName = .SelectedItems(1)
    End With
     
    'Change the column headings on the spreadsheet
    If Len(sFileName) = 0 Then Exit Sub
    Dim xlObj As Object
    Set xlObj = CreateObject("Excel.Application")
    'Collects current path
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(4).Show
    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = _
            Application.FileDialog(4).SelectedItems(1)
        'displays the result in a message box
    End If
    'This will
    With xlObj
    .Workbooks.Open sFileName
    .Visible = True
    .Worksheets.Select
    .Cells.Select
    .Selection.Replace What:="=", Replacement:="'="
    .Selection.Replace What:="=", Replacement:="'="
    .ActiveWorkbook.SaveAs FileName:=strPath & "NewFile.xlsx"
    End With
    Set fd = Nothing
    Set xlObj = Nothing

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    My suggested code should save the renamed file back to the same location originally opened from. Your latest code does not show implementing my suggestion.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Why does Excel close when I open Access.
    By Bmadigan2000 in forum Access
    Replies: 1
    Last Post: 12-23-2013, 11:11 AM
  2. Replies: 5
    Last Post: 07-22-2013, 01:11 PM
  3. XML file opens in excel 2010/2013 correctly but not in Access
    By flebber in forum Import/Export Data
    Replies: 1
    Last Post: 05-21-2013, 09:23 PM
  4. Replies: 1
    Last Post: 01-22-2013, 09:51 AM
  5. Access does not close Excel App
    By John_G in forum Programming
    Replies: 4
    Last Post: 09-04-2012, 03:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums