sam
sam

Reputation: 945

Access VBA Getobject()

I'm trying to set an instance of already opened excel to an object in access vba, but it is not working and when i'm running the code no error message is showing but control is coming out the sub.

here is the code

  Dim appXL As Object
  Dim wb As Object
  Dim txtcatpath As String
  txtcatpath = "C:\sample.xlsm"
  Set appXL = GetObject(,txtcatpath)

how do i set object as referernce to already opened excel?

Upvotes: 3

Views: 5732

Answers (1)

HansUp
HansUp

Reputation: 97101

Include the .Application property with GetObject.

Here's a simple example tested on my system.

Dim appXL As Object
Dim txtcatpath As String
txtcatpath = "C:\Users\hans\Documents\compensation.xlsx"
Set appXL = GetObject(txtcatpath).Application
Debug.Print appXL.activeworkbook.Name

With that workbook already opened in Excel, running the code gives me the workbook name, compensation.xlsx, in the Immediate window.

Upvotes: 1

Related Questions