Results 1 to 5 of 5
  1. #1
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26

    Question Using Access to open and close Excel

    Hi all, i'm trying run a piece of code in Access to open up an Excel spreadsheet and make some updates. I've been working on this for an eternity and running out of hair. See below for a snippet of my code. I'm having a problem with the instance of Excel not closing after all the code runs(I see the Excel.exe in Task Manager). i know the problem w my code is when i actually do the update(Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False). i think it opens another instance of Excel bc it's not tied to an instantiated object and this instance does not close. I think i need to tie it to an instantiated object...something like WS.Cells.Replace but i'm not sure how to do that. I keep getting errors. i'm a little new to Excel VBA. Can someone help me w that piece of code?




    strFileName = txtFileToBeImported
    strTableName = "KISClaimsLossRun_Admin"




    'Open Loss Run in Excel, delete column and close, saving changes
    Dim app As New Excel.Application
    app.Visible = False 'Visible is False by default, so this isn't necessary
    Dim book As Excel.Workbook
    Set book = app.Workbooks.Open(txtFileToBeImported, , False)

    'Excel.Application.DisplayAlerts = False
    app.DisplayAlerts = False



    Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False



    book.Save


    Set book = Nothing
    app.Quit
    Set app = Nothing


    Any help would be greatly appreciated
    Ted

  2. #2
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    When I was using Excel to transfer data, I also had a hard time getting Excel to close (if it wasn't open) before I did the transfer. I found several sites that helped me. It's been several years - I don't recall the code I used......
    Look at these:

    Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files
    http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

    Sample Excel Automation
    http://access.mvps.org/access/modules/mdl0006.htm

    Find out if an application is currently running
    http://access.mvps.org/access/api/api0007.htm

    Automation object doesn't close automatically
    http://access.mvps.org/access/general/gen0017.htm

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    And another discussion about clearing Excel http://forums.aspfree.com/microsoft-...el-413629.html

    Need a worksheet object. Try this:

    Dim app As New Excel.Application
    Dim book As Excel.Workbook
    Dim wsht As Excel.Worksheet
    Set book = app.Workbooks.Open(txtFileToBeImported, False)
    Set wsht = book.Worksheets("Sheet1")
    wsht.Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    book.Save
    app.Quit
    Set app = Nothing
    Set book = Nothing
    Set wsht = Nothing

    If you don't know the worksheet name and just want to hit all in the workbook:
    For Each wsht In Worksheets
    wsht.Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next
    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.

  4. #4
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26

    Thumbs up Awesome! Thank you!

    Quote Originally Posted by June7 View Post
    And another discussion about clearing Excel http://forums.aspfree.com/microsoft-...el-413629.html

    Need a worksheet object. Try this:

    Dim app As New Excel.Application
    Dim book As Excel.Workbook
    Dim wsht As Excel.Worksheet
    Set book = app.Workbooks.Open(txtFileToBeImported, False)
    Set wsht = book.Worksheets("Sheet1")
    wsht.Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    book.Save
    app.Quit
    Set app = Nothing
    Set book = Nothing
    Set wsht = Nothing

    If you don't know the worksheet name and just want to hit all in the workbook:
    For Each wsht In Worksheets
    wsht.Cells.Replace What:="""", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next
    thank you Thank You THANK YOU!! THAT did the trick! i only had to disable the alerts so Excel didn't ask me if i wanted to save the changes. Other than that it's working perfectly. i owe my sanity to you!! I really appreciate your help!

  5. #5
    murfeezlaw is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2012
    Posts
    26
    Quote Originally Posted by ssanfu View Post
    When I was using Excel to transfer data, I also had a hard time getting Excel to close (if it wasn't open) before I did the transfer. I found several sites that helped me. It's been several years - I don't recall the code I used......
    Look at these:

    Ken's ACCESS Examples and Information for Working With EXCEL Workbook Files
    http://www.accessmvp.com/KDSnell/EXCEL_MainPage.htm

    Sample Excel Automation
    http://access.mvps.org/access/modules/mdl0006.htm

    Find out if an application is currently running
    http://access.mvps.org/access/api/api0007.htm

    Automation object doesn't close automatically
    http://access.mvps.org/access/general/gen0017.htm
    Thank you Steve. I've bookmarked these as references. I appreciate your help as well.

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

Similar Threads

  1. Open Excel spreadsheet in Access
    By carlyd in forum Forms
    Replies: 1
    Last Post: 02-17-2012, 01:09 PM
  2. Close and Save Excel file already opened in Access
    By DB2010MN26 in forum Programming
    Replies: 7
    Last Post: 01-19-2012, 06:50 PM
  3. Unable to close Excel from Access 2007
    By Phred in forum Programming
    Replies: 4
    Last Post: 01-14-2012, 01:58 PM
  4. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 PM
  5. Open Excel from within Access?
    By kjw in forum Access
    Replies: 2
    Last Post: 04-08-2008, 07:12 AM

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