Consulting

Results 1 to 9 of 9

Thread: GetObject not working

  1. #1
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    GetObject not working

    Hello,

    I've tried both the the following pieces of code, and neither one seems to work for me.

    Sub test1()
        Dim newApp As Object
        Set newApp = GetObject("J:\My Documents\Quarter End.doc", "Word.Document")
    End Sub
    Sub test2()
        Dim newApp As Object
        Set newApp = GetObject("J:\My Documents\Quarter End.doc")
    End Sub
    The code runns without errors, but no new app is opened (nothing even flashes in the task manager, either). The document does exist, and opens just fine. The code isn't working with Word open or closed.

    Am I missing something here?
    (Running Office 2003 Pro on Windwoes XP Pro)
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  2. #2
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    If your goal is to open a word document, give this a try. You might want to try setting newApp.Visible = True in your code, since word opens with Visible = False when accessed this way.

    HTH.
    Private Sub CommandButton1_Click()
        Dim newApp As object
        Dim WordDoc As Object
        Set newApp = CreateObject("Word.application")
        newApp.Visible = True
        Set WordDoc = newApp.Documents.Open("J:\my Documents\Quarter End.doc")
    End Sub
    The most difficult errors to resolve are the one's you know you didn't make.


  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi CBrine,

    Thanks for that. It works beautifully, but I don't know if it will work easily for my application.

    My ultimate goal is to have a macro that can pull from a list of file names which may or may not all be Word docs. Could be some visio drawings, word docs, excel wb's, even html docs.

    According to MS's VBHelp on the getObject function "When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated." -- exactly what I'm after.

    I could set up the CreateObject with a case to evaluate each object before specifying which CreateObject code to open, but I ran into a problem with Publisher this way.... couldn't figure out how to open a file using VBA there.

    It may be the route to go, but I thought that the getObject might save me all of that work.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    VBAX Mentor CBrine's Avatar
    Joined
    Jun 2004
    Location
    Toronto, Canada
    Posts
    387
    Location
    It's what I thought. The application comes up with visible =False. If you set it to true, it seems to work fine. You don't need to define the type in the in the getobject, since it will use the windows associations for this.

    HTH
    Cal

    PS- Your right, this will save you a bunch of work, since it automatically recognizes the app you need to open with the file.

    Private Sub CommandButton1_Click() 
    Dim newApp As Object
    Set newApp = GetObject("C:\test.doc")
    newApp.Application.Visible = True
    End Sub
    The most difficult errors to resolve are the one's you know you didn't make.


  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hey Cal,

    Thanks! I was working with newapp.visible. Missed the application...

    Now I have something weird though.

    Access databases or Visio drawings will open, but disappear once the code ends. If you step through the code, it opens the object (and shows it), but as soon as you hit the line "end sub" the whole thing goes away.

    A Publisher file won't open at all. Come up with a runtime error '462'. "The remote server machine does not exist or is unavailable"

    A PDF file also won't open. It comes up with a runtime error '424'. "Object required"

    Excel files don't do anything at all, although I don't really care there, as I can just code it to hit a Workbook.open if it's an Excel file.

    Word works beautifully though...

    Have you ever run into any of these? The Access/Visio ones are the biggest concern to me, as those are most of what my user wants to open. I don't understand why they'd just disappear.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You might be getting errors because of the Document.Open part. Excel is not a document. If you changed that part to the correct name for the file then I think it should work.


    Dim newApp          As Object
    Dim ExcelWkb        As Object
    Set newApp = CreateObject("excel.application")
        newApp.Visible = True
        Set ExcelWkb = newApp.Workbooks.Open("C:\Temp.xls")

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Here is the code I use to open up any other program:


    Option Explicit
    
    Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hWnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    
    Sub OpenAnyFile(FilePath As String, FileName As String)
    Dim FileToOpen As String
    FileToOpen = FilePath & "" & FileName
        Call ShellExecute(0, "Open", FileToOpen & vbNullString, _
    vbNullString, vbNullString, 1)
    End Sub

    Just call it with the file name and path and it should open up just fine.

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location

    Cool

    Jacob, you rule!

    That is exactly what I was looking for. Somehow, though, I thought that it should be possible with VB's getObject method, as the MS help does make it appear that way.

    I solved my Access issue, after finding this with Access's OpenCurrentDatabase method:
    "When the variable pointing to the application object goes out of scope, the instance of Microsoft Access that it represents closes as well."
    The solution for that is to declare the appAccess variable at the module level to initialize, but that leaves a variable hanging around too.

    At any rate, your solution does exactly what I need, opening a new instance of any program with the file, no matter what it is.

    Thanks a ton, I really appreciate it. If that isn't a KB entry, I think it should be. Is there an API section? I haven't checked lately...

    Cheers,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •