Sub main
Dim channel as Integer
Dim appname as String
Dim topic as String
Dim testtext as String
Dim item as String
Dim pcommand as String
Dim msgtext as String
Dim answer as String
Dim x as Integer
Dim path as String
appname="WinWord"
path="c:\msoffice\winword\"
topic="System"
item="Page1"
testtext="Hello, world."
On Error Goto Errhandler
x=Shell(path & appname & ".EXE")
channel = DDEInitiate(appname, topic)
If channel=0 then
MsgBox "Unable to open Word."
Exit Sub
End If
DDEPoke channel, item, testtext
pcommand="[FileSaveAs .Name = " & Chr$(34) & "C:\TEMP001" & Chr$(34) & "]"
DDEExecute channel, pcommand
pcommand="[FileClose]"
DDEExecute channel, pcommand
msgtext="The text: " & testtext & " saved to C:\TEMP001." & Chr$(13)
msgtext=msgtext & Chr$(13) & "Delete? (Y/N)"
answer=InputBox(msgtext)
If answer="Y" or answer="y" then
Kill "C:\TEMP001.doc"
End If
DDETerminate channel
Exit Sub
Errhandler:
If Err<>0 then
MsgBox "DDE Access failed."
End If
End Sub
[ 本帖最后由 ilovejolly 于 2005-12-20 09:36 编辑 ]作者: yangjingxiao 时间: 2006-4-25 14:06
Microsoft Word does not support using the system topic to poke data. To enable Word to receive data, use the DDEPoke command, and send the data to a bookmark. You can either use a predefined bookmark or create your own.
以下代码测试通过
Sub main
Dim channel,x as Integer
Dim appname as String
Dim topic as String
Dim testtext as String
Dim item as String
Dim pcommand as String
Dim msgtext as String
Dim answer as String
Dim path as String
appname="WINWORD"
path="D:\setupfiles\Office\OFFICE11\"
topic="C:\\doc1.doc"
item="\StartOfDoc"
testtext="Hello, world."
On Error Goto Errhandler
x=Shell(path & appname & ".EXE C:\\doc1.doc")
channel = DDEInitiate(appname, topic)
If channel=0 then
MsgBox "Unable to open Word."
Exit Sub
End If
msgtext="The text: " & testtext & " saved to C:\TEMP001." & Chr$(13)
msgtext=msgtext & Chr$(13) & "Delete? (Y/N)"
answer=InputBox(msgtext)
If answer="Y" or answer="y" then
Kill "C:\TEMP001.doc"
End If
DDETerminate channel
Exit Sub
Errhandler:
MsgBox Err & ": " & Error$
End Sub