ID Number: Q85857
1.00
WINDOWS
Summary:
This article demonstrates how to send a LinkExecute command to Word
for Windows version 2.0 from Visual Basic using dynamic data exchange
(DDE) to run a macro.
This information applies to Microsoft Visual Basic programming system
version 1.0 for Windows and to version 2.0 of Word for Windows.
More Information:
The following example program demonstrates how to automatically start
Word for Windows and execute a WinWord macro called Foo.
Steps to Create Example Program
-------------------------------
1. Run Visual Basic, or from the File menu, choose New Project (ALT,
F, N) if Visual Basic is already running. Form1 is created by
default.
2. Create the following controls on Form1 with the following
properties:
Object CtrlName Caption
------ -------- -------
TextBox Text1
Button Command1 Start Word
Button Command2 Foo
3. Add the following code to the Command1 Click event:
Sub Command1_Click ()
x = Shell("winword.exe", 7) 'Start Word for Windows
'minimized without the focus
End Sub
4. Add the following code to the Command2 Click event:
Sub Command2_Click ()
Text1.LinkMode = 0 'Clears DDE link if it already exists.
Text1.LinkTopic = "Winword|document1" 'Sets up link with
'WINWORD.EXE.
Text1.LinkMode = 2 'Establish a cold DDE link.
Text1.LinkTimeout = 60 'Set the time for a response to 6 seconds;
'if a DDETIMEOUT occurs, increase the
'Text1.LinkTimeout
Text1.LinkExecute "[ToolsMacro .Name ="+Chr$(34)+"Foo"+Chr$(34)+",.Run]"
'(Note that the space is necessary as shown before .Name in the
' above LinkExecute statement.)
End Sub
5. Create a macro called Foo in WinWord that inserts "hello world" in
the document:
a. Switch to WinWord.
b. From the Tools menu, choose Macro.
c. Type "Foo" in the Macro Name field. Choose the Edit button.
d. Type the following:
Insert "Hello World"
e. From the File menu, choose Close. At the "Do you want to keep
the changes to Global: Foo?" prompt, choose Yes (this will save
the newly created Foo macro).
f. From the File menu, choose Exit. At the "Do you want to save the
global glossary and command changes?" prompt, choose Yes. (The
Foo macro has been added to the WinWord NORMAL.DOT file.)
6. Press F5 to run the program.
7. Choose the Start Word button.
8. Choose the Foo button. This will establish a DDE conversation with
Word Document1 and execute the Foo macro.
9. Switch to WinWord to verify that the Document1 contains "Hello
World," confirming that the Foo macro has been run (the CTRL+HOME
key combination will move the cursor back to the beginning of
the document).
Additional reference words: 1.00