PDA

View Full Version : Solved: GetObject not working



Ken Puls
10-13-2004, 11:21 AM
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)

CBrine
10-13-2004, 11:35 AM
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

Ken Puls
10-13-2004, 11:43 AM
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.

CBrine
10-13-2004, 11:56 AM
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

Ken Puls
10-13-2004, 01:45 PM
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.

Jacob Hilderbrand
10-14-2004, 03:02 PM
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")

Jacob Hilderbrand
10-14-2004, 03:09 PM
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.

Ken Puls
10-14-2004, 04:12 PM
Jacob, you rule! :thumb

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,

Jacob Hilderbrand
10-14-2004, 05:07 PM
Glad to help

Take Care