GetObject xxx Application not working recognizing open files

0 votes

Here's a code snipped exemplifying the issue I'm having:

Private Sub document_open()

    Dim excelObj As Excel.Application

    Err.Clear 'just pro forma, in case there's any previous junk left

    On Error Resume Next

    Set excelObj = GetObject("Book1").Application
    excelObj.Visible = True 'not necessary, I guess but I left it there anyway, tried removing it, tried changing it to false - no difference
    MsgBox excelObj Is Nothing 'for testing purposes

End Sub

I entered and executed that code on an un-saved, newly generated file (Book1). It gave a FALSE response, indicating that it has found the operating Excel instance with the specified file. I pressed Ctrl+N to create Book2, ran the code for it, and once more received a FALSE.

When I ran the code again after saving Book2 but leaving it open, it returned a TRUE, indicating that the script had failed to identify it as an instance of Excel. I ran it with Book1 and it returned FALSE. I then ran the script with "Book1" and "Book1.xlsm," saved Book1 (of course as an.xlsm), and received a TRUE.

I closed everything, checked the Task Manager to be sure there were no active Excel instances, opened both files, ran the script, and TRUE.

The consequence is that a saved open file cannot be used by script to identify an Excel instance; only an unsaved open file can.

Feb 24, 2023 in Others by Kithuzzz
• 38,000 points
1 flag 1,442 views

1 answer to this question.

0 votes

You can reference open Excel files in a different way to get around this problem. One choice is to loop over all open workbooks in the Application.Workbooks collection and see whether the name of any workbooks matches the filename you're looking for. Here's an example:

Sub Example()
    Dim wb As Workbook
    Dim filename As String
    filename = "Book1.xlsx" ' replace with the filename you are looking for
    For Each wb In Application.Workbooks
        If wb.Name = filename Then
            MsgBox "Found " & filename & " in Excel instance " & wb.Application.Caption
            Exit For
        End If
    Next wb
End Sub
answered Feb 24, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
0 answers

Self Created Laravel Link Not Working

I installed laravel 6.0 through composer and ...READ MORE

Dec 3, 2019 in Others by anonymous
• 120 points
808 views
0 votes
0 answers

PhpMyAdmin not working on localhost

I had recently installed the Xampp version:1.7.7 ...READ MORE

Feb 8, 2022 in Others by Soham
• 9,710 points
1,472 views
0 votes
1 answer

PhpMyAdmin not working on localhost

In order to answer your question, you ...READ MORE

answered Feb 8, 2022 in Others by Rahul
• 9,680 points
5,285 views
0 votes
1 answer

How to open the Google Play Store directly from my Android application?

By using developer.andriod.com, one can solve this ...READ MORE

answered Feb 8, 2022 in Others by Rahul
• 9,680 points
857 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,211 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,662 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
906 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,053 views
0 votes
1 answer

Opencart 3 .x SEO Url not working for route=information/contact, route=account/login

now i m going to see you ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
2,881 views
0 votes
1 answer

Nokia Lumia 920 Excel not open

The issue is with your code, not ...READ MORE

answered Sep 27, 2022 in Others by narikkadan
• 63,600 points
471 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP