Results 1 to 2 of 2
  1. #1
    WhatTheFrick is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2013
    Posts
    5

    Controlling NEW xlapp when Excel was already open

    Using Access 2007, Excel 2007, database and Excel files are 2003 versions (compatibility mode).



    Everything I'm doing works great as long as Excel was not previously open when running this code. For some reason, if Excel is open before I run my code, and even though I set xlapp to be a NEW Excel application (which works: if I set xlapp to visible, I can see BOTH Excel windows), I get two versions of the workbook I opened, one is Read Only, and the ImportRange is correct in one but offset by one row in the other, and Access is importing the named range from the workbook where the range is off by one.

    I create a new Excel object, open a workbook, find a value, use that location to set my named range, then I import the named range into an Access table:
    Code:
    public xlapp as excel.application
    Public wbCurrent As excel.Workbook
    Public shtData As worksheet
    Set xlApp = CreateObject("Excel.Application") 'I've also tried using  = New excel.Application - they both work
    With xlApp
        Set wbCurrent = .Workbooks.Open(ExcelPathAndFile, , False, , pw)
        'wbCurrent.Activate - tried this, didn't work
        Set shtData = wbCurrent.Sheets(DataSheet)
        
        Set ACell = shtData.Cells.Find("StartValue", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0)
        Set BCell = ACell.End(xlToRight)
        Set CCell = shtData.Cells(65000, ACell.Column).End(xlUp)
        
        wbCurrent.Names.Add Name:="ImportRange", RefersTo:=shtData.Range(ACell.Address, shtData.Cells(CCell.row, BCell.Column).Address)
    End With
        DoCmd.TransferSpreadsheet acImport, 8, "NewTable", ExcelPathAndFile, True, "ImportRange"
    Like I said, this works perfectly if I don't have Excel open before I run the code (not the file itself; if I just have Excel open period). If Excel is open, I don't get any errors or anything, but my workbook is somehow open in BOTH instances of Excel, as Read Only in the already-open instance, and the "ImportRange" was set in both versions, but is one row off in one, and it's THIS one that Access importing.

    How can I make this work if Excel was open before running code? Why is the workbook getting opened twice? Why is Access importing from the wrong one? Why would the named range be offset by one row in one version of the opened workbook?

    I don't need answers to ALL these questions, I just need to make this thing work if Excel happened to be open already. I'm really confused as to why what's happening is happening.

    I appreciate any help, and thanks for reading.

  2. #2
    dr91075 is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2013
    Posts
    22
    I had the same issue. What worked for me was setting the workbook variable (wbCurrent) AFTER opening the workbook.

    Code:
    .Workbooks.Open(ExcelPathAndFile, , False, , pw)
    set wbCurrent=xlApp.activeworkbook

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

Similar Threads

  1. Controlling forms from more than one table
    By jaarons in forum Forms
    Replies: 4
    Last Post: 01-04-2013, 02:05 AM
  2. Excel code not working with Excel open
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 07-11-2011, 12:12 PM
  3. Replies: 3
    Last Post: 10-01-2010, 07:04 PM
  4. Controlling autonumber
    By Patience in forum Access
    Replies: 3
    Last Post: 06-22-2010, 04:11 AM
  5. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 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