Microsoft KB Archive/85857

{|
 * width="100%"|

-

The information in this article applies to:


 * Microsoft Visual Basic Professional and Enterprise Editions, 16-bit only, for Windows, version 4.0
 * Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
 * Microsoft Visual Basic programming system 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(&quot;winword.exe&quot;, 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 = &quot;Winword|document1&quot; '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 _ &quot;[ToolsMacro .Name =&quot;+Chr$(34)+&quot;MyMacro&quot;+Chr$(34)+&quot;,.Run]&quot; ' 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 &quot;hello world&quot; in the document:

 Switch to Word. From the Tools menu, choose Macro.</li> Type MyMacro in the Macro Name field. Choose the Edit button.</li>  Type the following: "Insert &quot;Hello World&quot;"</li> From the File menu, choose Close. At the &quot;Do you want to keep the changes to Global: MyMacro?&quot; prompt, choose Yes. (This will save the newly created MyMacro macro.)</li> From the File menu, choose Exit. At the &quot;Do you want to save the global glossary and command changes?&quot; prompt, choose Yes. (The MyMacro macro has been added to the Word NORMAL.DOT file.)</li></ol> </li> Press the F5 key to run the Visual Basic program.</li> Choose the Start Word button.</li> Choose the MyMacro button. This will establish a DDE conversation with Word Document1 and execute the MyMacro macro.</li> Switch to Word to verify that the Document1 contains &quot;Hello World,&quot; confirming that the MyMacro macro has been run (the CTRL+HOME key combination will move the cursor back to the beginning of the document).</li></ol>

Additional query words:

Keywords : kbinterop kbprg kb16bitonly kbVBp400 IAPDDE VB4WIN vbwin

Version : WINDOWS:1.0,2.0,3.0,4.0,6.0,6.0a

Platform : WINDOWS

Issue type : kbhowto
 * }