The information in this article applies to:
- Professional and Enterprise Editions of Microsoft Visual Basic,
16-bit only, for Windows, version 4.0
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0, 3.0
- Microsoft Visual Basic for Windows, version 1.0
- Microsoft Word for Windows, versions 2.0, 6.0, 6.0a
SUMMARY
This article demonstrates how to send a LinkExecute command to Microsoft
Word for Windows from Visual Basic using dynamic data exchange (DDE) to run
a macro.
MORE INFORMATION
The following sample program demonstrates how to automatically start Word
for Windows and execute a Word macro called MyMacro.
Steps to Create Sample Program
- Start Word and start a new project in Visual Basic. Form1 is created by
default.
- Place the following controls on Form1, and give them these properties:
Object Name Caption
---------------------------------
TextBox Text1
Button Command1 Start Word
Button Command2 MyMacro
- 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
- Add the following code to the Command2 Click event:
Sub Command2_Click ()
Text1.LinkMode = 0 'Clear DDE link if it already exists.
Text1.LinkTopic = "Winword|document1" 'Set up WINWORD.EXE link.
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
'VB3Line: Enter the following lines as one line
Text1.LinkExecute _
"[ToolsMacro .Name ="+Chr$(34)+"MyMacro"+Chr$(34)+",.Run]"
' NOTE: the space is necessary as shown before .Name in the
' above LinkExecute statement.
End Sub
- Create a macro called MyMacro in Word that inserts "hello world" in the
document:
a. Switch to Word.
b. From the Tools menu, choose Macro.
c. Type "MyMacro" 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: MyMacro?" prompt, choose Yes. (This will save the
newly created MyMacro 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
MyMacro macro has been added to the Word NORMAL.DOT file.)
- Press the F5 key to run the Visual Basic program.
- Choose the Start Word button.
- Choose the MyMacro button. This will establish a DDE conversation with
Word Document1 and execute the MyMacro macro.
- Switch to Word to verify that the Document1 contains "Hello World,"
confirming that the MyMacro macro has been run (the CTRL+HOME key
combination will move the cursor back to the beginning of the document).
Keywords : IAPDDE vb416 VB4WIN vbwin GnrlVb kbinterop kbprg kbfasttip
Technology : kbvba
Version : WINDOWS:1.0 2.0 3.0 4.0
Platform : WINDOWS
Issue type : kbhowto
|