The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for
Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0
- Microsoft Word for Windows, version 2.0
- Microsoft Word for Windows, version 6.0
SUMMARY
When performing DDE between Microsoft Visual Basic and Microsoft Word for
Windows, you need to supply a LinkTopic and a LinkItem. The LinkItem for
much of the DDE to Word will be the name of a Bookmark in Word. This can be
an obstacle if the Bookmark is not known in advance. This article shows how
to get a listing of available Word Bookmarks from Visual Basic with the
help of a Word Macro.
MORE INFORMATION
Step-by-Step Example to Find All Open Documents
When Word for Windows is running, all open Documents can be found by
the System LinkTopic.
- Start a new project in Visual Basic. Form1 is created by default.
- Add a text box (Text1) and a command button (Command1) to the form.
- Add the following code to the Command1_Click event:
Sub Command1_Click ()
Text1.LinkTopic = "Winword|System"
Text1.LinkMode = 2
Text1.LinkItem = "Topics"
Text1.LinkRequest
Text1.LinkMode = 0
End Sub
- Start Word for Windows.
- Start the Visual Basic program or press the F5 key.
- To display the available Documents currently open in Word for Windows in
the Text1 box, click the Command1 button. If you open additional
documents or close documents that are open, you refresh the list by
again clicking the Command1 button.
Step-by-Step Example to Find Available Bookmarks
Once you identify the document you want, you can find the available
Bookmarks that can be used as the LinkItem in DDE conversations.
- To the above example program, add another text box (Text2) and another
command button (Command2).
- Create a new document in Word by choosing New from the File menu.
- Save the document as TEST.DOC by choosing Save As from the File menu.
- Add the following Word for Windows macro naming it EnumBookmarks. To
create a new macro, choose Tools Macro from the Word menu. Name the
macro EnumBookmarks and select Edit in Word version 2.0 or Create in
Word version 6.0. Paste in the following code. Then close the macro
window, and save your changes.
'For Word for Windows 2.0, the code is as follows:
Sub Main
EndOfDocument
InsertBookmark.Name = "Start"
For Count = 1 to CountBookmarks()
If BookmarkName$(Count) <> "Start" then
InsertPara
Insert BookmarkName$(Count)
Endif
Next Count
ExtendSelection
EditGoTo.Destination = "Start"
InsertBookmark.Name="Start",.Delete
InsertBookmark .Name = "BookmarkList"
End Sub
'For Word for Windows 6.0, the InsertBookmark command was replaced with
'the EditBookmark command. Your macro code will need to reflect this
'as follows:
Sub MAIN
EndOfDocument
EditBookmark .Name = "Start"
For Count = 1 To CountBookmarks()
If BookmarkName$(Count) <> "Start" Then
InsertPara
Insert BookmarkName$(Count)
EndIf
Next Count
ExtendSelection
EditGoTo .Destination = "Start"
EditBookmark .Name = "Start", .Delete
EditBookmark .Name = "BookmarkList"
End Sub
- Add the following code to the Command2_Click event:
Sub Command2_Click ()
Text2.LinkTopic = "Winword|System"
Text2.LinkMode = 2
Text2.LinkItem = "Topic"
Text2.LinkExecute "[EnumBookmarks]"
Text2.LinkMode = 0
Text2.LinkTopic = "Winword|Test.Doc"
Text2.LinkMode = 2
Text2.LinkItem = "BookMarkList"
Text2.LinkRequest
Text2.LinkMode = 0
End Sub
NOTE: The second LinkTopic definition above assumes that Word's current
directory is the same directory where TEST.DOC is located. If this
is not the case, or if you are not certain whether or not this is the
case, replace TEST.DOC with a full path such as C:\WORD\TEST.DOC.
- If it is not already running, start Word for Windows.
- Start the Visual Basic program (or press the F5 key).
- To see the available Bookmarks for the document TEST.DOC listed in the
Text2 box, click the Command2 button. If you create or delete
additional Bookmarks, you can refresh the list in the Text2 box by
again clicking the Command2 button.
When you click the Command2 button, the EnumBookmarks Word macro runs.
EnumBookmarks creates a new Bookmark called BookmarkList where it copies
the names of all of the other bookmarks in the document. When the bookmark
BookmarkList is retrieved. It will contain all the bookmarks in the
document.
Once you identify the Documents and Bookmarks within Word for Windows, you
can establish DDE sessions using the Document and Bookmark as
LinkTopic and LinkItem values.
Text1.LinkTopic = "WinWord|MyDoc.Doc" 'Sets up link with WINWORD.EXE.
Text1.LinkItem = "MyBookmark" 'Set link to bookmark on document.
REFERENCES
For additional information, please see the following article in the
Microsoft Knowledge Base:
ARTICLE-ID: Q74862
TITLE : DDE Example Between Visual Basic and Word for Windows
Keywords : IAPDDE vbwin GnrlVb kbinterop kbprg
Technology : kbvba
Version : WINDOWS:2.0 3.0
Platform : WINDOWS
Issue type : kbhowto