none
GetObject not picking the running excel files RRS feed

  • Question

  • I am trying to get opened instances of excel application and operate between them.

    Set xlApp = GetObject("", "Excel.Application")
    For wb = 1 To xlApp.Workbooks.Count
        xlApp.Workbooks(wb).Close
    Next
    

    I have 2 excel files in open and while running the code xlApp.Workbooks.Count always 0.
    But GetObject("", "Excel.Application").Application.Name returns Microsoft Excel and GetObject("", "Excel.Application").Application.hwnd returns a valid handle.

    Also I tried with Set xlApp = GetObject("", "XLMAIN") which throws error ActiveX can't create object.
    Why it is not picking the running excel files and give count? Any ideas?

    Wednesday, April 29, 2015 8:43 PM

Answers

  • Thanks for the reply.

    I am using VBScript and the syntax is right.

    The same code is working for me if I execute it from VBS file but only not executing if I execute from the tool UFT. Seems the problem is with the tool.

    Thanks...

    Thursday, April 30, 2015 1:52 PM

All replies

  • Re:  getting an object

    In the Line...  Set xlApp = GetObject("", "Excel.Application")
    Remove the quote marks before the comma.
    So the line looks like...  Set xlApp = GetObject(, "Excel.Application")

    For reference...
    If the path (1st argument) is a zero-length string (""), GetObject returns a new object instance of the specified type.
    If the path argument is omitted, GetObject returns a currently active object of the specified type.
    If no object of the specified type exists, an error occurs.

    '---
    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)

    • Edited by James Cone Monday, October 31, 2016 3:44 AM
    Thursday, April 30, 2015 3:07 AM
  • Thanks for the reply.

    I am using VBScript and the syntax is right.

    The same code is working for me if I execute it from VBS file but only not executing if I execute from the tool UFT. Seems the problem is with the tool.

    Thanks...

    Thursday, April 30, 2015 1:52 PM
  • Re:  code is correct, but code doesn't work

    Did you try my suggestion?

    '---
    Jim Cone

    Thursday, April 30, 2015 2:24 PM