EXTREMELY URGENT: Problem with exlApp.Workbooks.Open() function in VBA

DimlyLitMind

New Member
Joined
Apr 24, 2014
Messages
4
Hi all,

I am trying to run a VBA code in Access to open an excel file and export some data into it. This has worked before with Excel 2003. I had to recently migrate to Excel 2007 and I am getting various errors one by one, but at this point I am stuck at this particular step of opening an Excel file. The file is present, the path is correct (I checked while debugging the VBA code) and I get an error. The problem is, the Err.Description field is blank. In fact, everything in Err object is either blank or 0. Can someone help me? This is EXTREMELY URGENT. Thanks!!!

Here's my code:

Code:
On Error Resume Next
    ' try to open Excel
    Set exlApp = CreateObject("Excel.Application")
    RunExcelBeforeDataTransfer = False
    If LoadExcel = False Then
        Exit Function
    End If
    Set wrkBook = exlApp.Workbooks(strFSFileName)
    If 0 = Err.Number Then 'a workbook with this name is OPEN. Close it first.
        exlApp.Workbooks(strFSFileName).Close SaveChanges:=False
    End If
    
    Err.Clear
    On Error GoTo ErrorHandler
    Call Log("Opening Excel file: " & strThisAppPath + strFSFileName)
    Set wrkBook = exlApp.Workbooks.Open(strThisAppPath + strFSFileName, 0, False)
    exlApp.Run (strFSFileName & "!GeraFormatBeforeDataTransfer")
    exlApp.Workbooks(strFSFileName).Close SaveChanges:=True
    UnLoadExcel
    
    RunExcelBeforeDataTransfer = True
    Exit Function
ErrorHandler:
    UnLoadExcel
    Call Log("Error preparing Excel for data transfer: " & CStr(Err.Number) & " - " & Err.Description & Err.Source)
    AppError "Error preparing Excel for data transfer: " & CStr(Err.Number) & " - " & Err.Description & Err.Source
    Err.Raise vbObjectError + 1, "", ""
End Function
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Have you tried commenting out the On Error GoTo ErrorHandler line and adding an On Error Goto 0 in its place for testing?
 
Upvote 0
I have tried your suggestion. I am getting an error message that "The macro "filename.xls!GeraFormatBeforeDataTransfer" is not running. Either it is not available in this workbook or the macros are disabled." However, it ran before, today AND yesterday. I think the macro is not getting enabled. How can I enable it? I have set "Enable all macro settings" in Excel options.
 
Upvote 0
Is the file still an .xls not an .xlsm or .xlsb file? Does the file name have spaces in it?
 
Upvote 0
You can test if the workbook opens successfully:

Code:
Set wrkBook = exlApp.Workbooks.Open(strThisAppPath + strFSFileName, 0, False)
Msgbox wrkBook.Name

It is possible you need a path separator:
Code:
Set wrkBook = exlApp.Workbooks.Open(strThisAppPath [B][COLOR="#FF0000"]& "\" &[/COLOR][/B] strFSFileName, 0, False)

Also now just noticing that + is not a vba concatenator so that is wrong:
Code:
Set wrkBook = exlApp.Workbooks.Open(strThisAppPath [B][COLOR="#FF0000"]+[/COLOR][/B] strFSFileName, 0, False)

Should be:
Code:
Set wrkBook = exlApp.Workbooks.Open(strThisAppPath [B][COLOR="#FF0000"]&[/COLOR][/B] strFSFileName, 0, False)
 
Upvote 0
It seems there was an issue with the file used. There were two files in two folders with the same name, but one with the macro not present (or not activated, I think). I used the newer files and it worked!! I don't know why there were two files like that to begin with, but the code worked without any changes made. I think the + and "\" are not involved.
 
Upvote 0
Thank you for your help Rory. It seems there were two files in two folders which have the same name but one was an older version which does not contain the macro I think. I used the newer file and it worked.
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,847
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top