GetObject... Old problem, new question.

I

imatter

Can anyone tell me why the following lines of code yield different results?

Set wordApp = GetObject(, "Word.Application")

Set wordApp = GetObject("", "Word.Application")

Example Code 1:

If WINWORD.EXE is open this line works as expected. It uses the exiisting
WINWORD.EXE and therefore does not cause the Normal.dot error** If
WINWORD.EXE is not open before execution of the script I get a Run-Time error
'429' when executing the script.

Example Code 2:

I doesn't matter whether or not WINWORD.EXE is open, this code opens a new
instance of WINWORD.EXE which causes the Normal.dot error. Writing the code
this way gets rid of the Run-Time error '429'.

Office 2003, Outlook to Word macro.

** Normal.dot error: when two instances of WINWORD.EXE are open, one is the
owner of the template file and locks it so when you close one of the
documents it wants you to save a copy of the Normal.dot
 
J

jaf

Hi,
This is normal behavior documented VBA in help.

Remarks

Use the GetObject function to access an ActiveX object from a file and assign the object to an object variable. Use the Set statement to assign the object returned by GetObject to the object variable. For example:

Dim CADObject As Object
Set CADObject = GetObject("C:\CAD\SCHEMA.CAD")

When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated.

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



John
 
I

imatter

Thank you Jaf. The help explanation makes perfect sense.

I guess it just doesn't seem logical. In order to actually use the
GetObject() function in a real world context, e.g. check to see if
WINWORD.EXE is running, you have to supress its error handling. I guess the
check is more of a hack anyways and at the time they weren't thinking about
the Normal.dot error.

Thanks again.
 
J

jaf

Hi,
You just need to handle the error.
This is the example from Excel 2003 VBA help.

' Declare necessary API routines:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName as String, _
ByVal lpWindowName As Long) As Long

Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hWnd as Long,ByVal wMsg as Long, _
ByVal wParam as Long, _
ByVal lParam As Long) As Long

Sub GetExcel()
Dim MyXL As Object ' Variable to hold reference
' to Microsoft Excel.
Dim ExcelWasNotRunning As Boolean ' Flag for final release.

' Test to see if there is a copy of Microsoft Excel already running.
On Error Resume Next ' Defer error trapping.
' Getobject function called without the first argument returns a
' reference to an instance of the application. If the application isn't
' running, an error occurs.
Set MyXL = Getobject(, "Excel.Application")
If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel

' Set the object variable to reference the file you want to see.
Set MyXL = Getobject("c:\vb4\MYTEST.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the MyXL object reference.
MyXL.Application.Visible = True
MyXL.Parent.Windows(1).Visible = True
Do manipulations of your file here.
' ...
' If this copy of Microsoft Excel was not running when you
' started, close it using the Application property's Quit method.
' Note that when you try to quit Microsoft Excel, the
' title bar blinks and a message is displayed asking if you
' want to save any loaded files.
If ExcelWasNotRunning = True Then
MyXL.Application.Quit
End IF

Set MyXL = Nothing ' Release reference to the
' application and spreadsheet.
End Sub

Sub DetectExcel()
' Procedure dectects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If
End Sub

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top