|
Visual Basic (VB6 and VBA) |
|
Copyright 1999-2001 Christopher Greaves. All rights reserved. Home Page and email to [email protected] |
| If in doubt, record a macro and inspect the entrails! |
Visual Basic Library
This page was last updated on Saturday, November 24, 2001
Sub AutoExec() ' How to check whether Word is open ' Article contributed by Lutz Gentkow ' Some users are very talented at opening many instances of Word, by ' repeatedly clicking on the shortcut, until memory or resources run out. This ' is particularly so with Word 97, where clicking on Word's shortcut almost ' always starts a new instance of Word. With Word 2000, clicking on Word's ' shortcut generally creates a new document using an existing instance of ' Word, if there is one; but if a Word dialog box is active at the time, even ' Word 2000 creates a new instance of Word. ' ' So I have written the following macro to prevent users from doing this. Being ' an AutoExec macro, it fires automatically when you start a new instance of ' Word; and if it finds two open instances (the new one plus another one that ' was already open), then it activates the one that was already open and quits ' the new instance. ' ' However, if you are using Automation to open Word from another ' application (such as a VB application, or Excel) and want to check whether ' Word is already open before you start, then you can't use the Tasks ' collection. Instead, use the method described in the article: Control Word from Excel. Dim strTask As String strTask = "Microsoft Word" Dim Hits As Long Dim oTask As Task For Each oTask In Tasks Debug.Print oTask.Name Next oTask For Each oTask In Tasks If Left(oTask.Name, Len(strTask)) = strTask Then Hits = Hits + 1 If Hits >= 1 Then oTask.WindowState = wdWindowStateMaximize Application.Quit Else ' Not yet exceeded our limit End If Else ' not Microsoft Word End If Next oTask End Sub
| We all knew nothing when we started … |
|
Home Page and Contact Information Send email to [email protected]. This page was last updated Monday, November 19, 2001 |