Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172

    Trouble having Access to save an Excel spreadsheet


    I have Access open an Excel spreadsheet, then Access makes some changes to the spreadsheet, and then I need Access to save the spreadsheet. I can do this, but when it saves the file, I get the message "A file named 'P&L 2024.xls' already exists in this location. Do you want to replace it?". I just want to save the file - YES, but not as a new file. I hope this makes sense. The code is as follows:

    Code:
    Dim objXLApp As Object    Dim objXLBook As Object
        Dim objWorkSheet As Object
        pubStrYrSpecial = "2024"
        
        '   open p&l
        Set objXLApp = CreateObject("Excel.Application")
        Set objXLBook = objXLApp.Workbooks.Open("L:\aaTS\Data\P&L 2024.xls")
        Set objWorkSheet = objXLBook.Worksheets(pubStrYrSpecial)
            
        '   Access makes some changes to the workbook
        
        '   save and close the workbook
        objXLBook.Save
        objXLBook.Close
        objXLApp.Quit
        Set objXLApp = Nothing
    I appreciate any help.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Works for me without prompt. Must be something in the changes causing issue. Post all the code or provide files.
    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
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172
    That is the whole code. I have not yet added the changes Access is to make except that I have it in a function as below.
    Code:
    Public Function modPnL_sumRevenue1()   '   sum the revenue values putting it in 'TOT REVENUE'    Dim objXLApp As Object
        Dim objXLBook As Object
        Dim objWorkSheet As Object
        pubStrYrSpecial = "2024"
        
        '   open p&l
        Set objXLApp = CreateObject("Excel.Application")
        Set objXLBook = objXLApp.Workbooks.Open("L:\aaTS\Data\P&L 2024.xls")
        Set objWorkSheet = objXLBook.Worksheets(pubStrYrSpecial)
            
        '   Access makes some changes to the workbook
        
        '   save and close the workbook
        objXLBook.Save
        objXLBook.Close
        objXLApp.Quit
        Set objXLApp = Nothing
        
    End Function
    I get the dialog box everytime.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I used your code with my Excel file and do not get prompt.

    What kind of changes do you want to make? It is possible to set a link to Excel sheet and edit data (but not delete rows).
    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
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    May depend on the windows or office version. Not tested but try commenting out the save line and modifying the close line to

    objXLBook.Close SaveChanges:=True

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Works for me also in 2007. However, only after dimming missing variables?

    Why are you not using Option Explicit at the top of every module?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172
    Ok, I got several responses and I am trying to answer all of them. I do have 'Option Compare Database Option Explicit' on top of the module. I just did not copy that part. I also tried objXLBook.Close SaveChanges:=True. I still get the prompt. As far as the changes, I will only change the data. I guess I will have to live withe the prompt. Thanks for trying.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I do have 'Option Compare Database Option Explicit' on top of the module.
    So why did mine complain about
    Code:
    pubStrYrSpecial
    objXLApp
    each of which were not declared in the procedure.?

    Are they Globals?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    @lawdy, what is your Excel version? Not sure if it would matter but I'd declare the objects as application, workbook and worksheet - not Object, unless you have a reason for doing so. I also imagine that you're doing this over a network and might need a pause between Save and Close. Try stepping through the code and wait couple of seconds before closing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Do you want to explore the link option?
    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.

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    unless you have a reason for doing so
    OP is probably late binding.

    I note the file is a .xls, not .xlsx which might make a difference?

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by CJ_London View Post
    OP is probably late binding.

    I note the file is a .xls, not .xlsx which might make a difference?
    I tried both extensions, first xlsx then xls. Each worked fine.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    172
    What is the link option?

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I think it was the linked sheet suggestion in post 4.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Here is example of query statement that makes connection to a simple worksheet. Data can be edited and records added but not deleted.

    SELECT * FROM [Units$] IN 'C:\Users\Administrator\June\MyStuff\Condos.xlsx'[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes];

    When Access External Data wizard sets up a link to worksheet, it will probably set IMEX property = 2 and cannot edit in the linked table.

    Create a link by typing in SQLView of query object and change IMEX to = 0 and edit is possible.
    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.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 03-19-2019, 08:35 AM
  2. Replies: 6
    Last Post: 09-01-2018, 04:40 PM
  3. Replies: 1
    Last Post: 05-23-2013, 10:00 AM
  4. Creating excel spreadsheet from access vba
    By nyneave in forum Programming
    Replies: 1
    Last Post: 10-12-2012, 09:59 AM
  5. Email Excel Spreadsheet from Access
    By Nancy in forum Access
    Replies: 2
    Last Post: 11-09-2010, 02:37 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