| One of the problems when controlling Word from VB using the Word oject model, is that VB does not use a running copy of Word, and instead starts up a new instance of the application. These functions will check to see if Word is running, if it is, then grab it and turn it into an object, otherwise, create a new instance. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) 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 Public Sub DetectWord() ' Procedure detects a running Word and r ' egisters it. Const WM_USER = 1024 Dim hWnd As Long ' If Word is running this API call retur ' ns its handle. hWnd = FindWindow("OpusApp", vbNullString) If hWnd = 0 Then' 0 means Word Not running. Exit Sub Else ' Word 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 'Store these declarations in the form wh ' ich is making the calls to Word Dim WordApp As Word.Application Dim WordWasNotRunning As Boolean' Flag For final word unload 'The main sub which detects word, and cr ' eates the object, or starts a new Word Public Sub GetWord() ' Test to see if there is a copy of Micr ' osoft Word already running. 'on error resume next' Defer error trapping. ' Getobject function called without the ' first argument returns a ' reference to an instance of the applic ' ation. If the application isn't ' running, an error occurs. Set WordApp = GetObject(, "Word.Application") If Err.Number <> 0 Then WordWasNotRunning = True Else WordWasNotRunning = False End If Err.Clear' Clear Err object In Case Error occurred. ' Check for Microsoft Word. If Microsoft ' Word is running, ' enter it into the Running Object table ' . DetectWord If WordWasNotRunning = True Then 'Set the object variable to start a new ' instance of Word. Set WordApp = New Word.Application End If ' Show Microsoft Word through its Applic ' ation property. Then ' show the actual window containing the ' file using the Windows ' collection of the MyWord object refere ' nce. WordApp.Visible = True WordApp.Application.WindowState = wdWindowStateMaximize Screen.MousePointer = vbDefault End Sub 'Demo of how to call the above sub Private Sub cmdPreview_Click() 'Preview a word document Screen.MousePointer = vbHourglass 'Startup Word if not started, or switch to existing one GetWord 'Open the document, maximised, and switch to word WordApp.Visible = True WordApp.Application.WindowState = wdWindowStateMaximize WordApp.Documents.Open (myfilepath) Screen.MousePointer = vbDefault WordApp.Application.Activate Set WordApp = Nothing 'We can also use the 'WordWasNotRunning' flag to decide if we should quit word or not 'Users might get annoyed if docs they are currently editing aren't saved!! End Sub |
Use existing instance of Word as an object |
Freelance ASP PHP web development | Web developer India Web development India| Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design