Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetObject Function Example

57 views
Skip to first unread message

john bruin

unread,
Sep 21, 1998, 3:00:00 AM9/21/98
to
Below is an example from the Excel97 visual basic help file. It is a
procedure to detect whether Excel is already running. I don't understand
it very well but assume there must be some different values for
detecting whether Word97 is running. Does anyone know these values or
can point me in the right direction to find them? Thanks in advance.


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


Regards
John Bruin

Stephan Kassanke

unread,
Sep 21, 1998, 3:00:00 AM9/21/98
to jbr...@tssc.co.nz
Hi John,

you don't necessarily need to use the API call. In the following sub, an
error will occur if Word is not running. So err.number will be <> 0 and you
can test for it. If err.number is <> 0 Word was not running, if not you have
a ref to the instance in the variable WordObj.

For Classnames have a look at KB article Q169240. The classname for word
windows is "OpusApp". Replace XLMAIN with OpusApp in your code, and
Findwindow will look for running Word instances.

Window Class Names for the Office 97 Applications

The window class names used by each Microsoft Office 97 program are
listed in the following table.

Program name Window class name
----------------------------------------
Microsoft Access OMain
Microsoft Excel XLMAIN
Microsoft Outlook rctrl_renwnd32
Microsoft PowerPoint PP97FrameClass
Microsoft Word OpusApp

hope this helps,
Stephan


Sub wordstarten()
Dim WordObj As Word.Application
Dim WordDoc As Word.Document
Dim myblock As Range


On Error Resume Next
' If Word is not running, err.number <> 0
Set WordObj = GetObject(class:="Word.Application")
If Err.Number <> 0 Then
'Open an instance of Word
Set WordObj = CreateObject("Word.Application")
Err.Clear
End If
' Fehlerbehandlung wieder einschalten
On Error GoTo 0

' Object reference on source data b2 or whatever
Set myblock = Worksheets("Daten").Range("b2").CurrentRegion

' copy source
myblock.Copy

' new document object reference
Set WordDoc = WordObj.Documents.Add
' and paste
WordDoc.Content.Paste
' some formatting
WordDoc.Content.Font.Size = 15
' show the results
WordObj.Visible = True
' and activate window
WordObj.Activate

' release Object variables
Set WordObj = Nothing
Set WordDoc = Nothing
End Sub


john bruin wrote:

--
--------------------------------------------------------------------------
Stephan Kassanke
Uni GH Paderborn
Decision Support & Operations Research
Warburger Straße 100
33098 Paderborn
05251 / 603721

e-Mail: ka...@uni-paderborn.de
ICQ: 16285877
--------------------------------------------------------------------------

0 new messages