Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word and DDE

Status
Not open for further replies.

SL100guy

IS-IT--Management
Sep 29, 2003
20
0
0
US
Hi All,

I am new to the use of DDE and am having a difficult time wrapping my head around its use.

I can open a new word 2007 document with success, but I can not write to it and save the file as a <filename>.docx with any chosen name. Please post some snipet examples for me to try.

Please no links because I am limited to my web access.

 
I've had mixed results bending DDE to my will.

If you just plan on opening Word, writting data, and saving word, take a look at the code below.

What it does is open notepad (change to word) and use alt-keys to copy and paste the data. What you would need to add would be the code to send that alt-keys to do a 'save as' and send the file name.

The example below was used to pull data out of a Nortel DMS, so you'd have to modify it for your own use.

Code:
;Generate a list of switch ports based on the trunk group number.
;Kevin Smith 4-28-2006
;	This version uses Notepad as a buffer.


string fName, sXMit, sCLLI, sQA, sTemp3
string sTextToCopy          ; Text to put into NOTEPAD.
string sOFFL[500]
integer iOFFLCnt

proc main
	iOFFLCnt = 1
	fName = "c:\grouplist.txt"
	sQA = "quit all^m"
	delfile fName
	get_CLLI()
   	run "notepad.exe"          ; Run Windows NOTEPAD.
   	pause 1                    ; Pause to wait for NOTEPAD.
	set capture path "c:\"
	set capture file "grouplist.txt"
	data_collector()
	list_builder()
	delfile fName
endproc

proc data_collector
	;sdlginput "Enter CLLI" "CLLI" sCLLI DEFAULT
	sXMit = "mapci nodisp;mtc;trks;stat;selgrp "
	strcat sXMit sCLLI
	strcat sXMit ";hcpytrk^m"
	clear
	capture on
	transmit sXMIT
	waitfor ">" forever
	capture off
	transmit sQA
endproc

proc list_builder
string sTemp, sType, sMajor, sMinor, sState, sJunk, sTCIC, sOutName, sTemp2
integer iIndex, iIndex2

	sOutName = "c:\tempout.txt"
	delfile sOutName
	
	fopen 1 sOutName write text
	fputs 1 sCLLI
	sTemp3 = sCLLI
	strtoclip sTemp3           ; Copy string to clipboard.
	paste_notepad()
	
	if fopen 0 fName read text
		for iIndex = 1 upto 10
			fgets 0 sTemp
		endfor

		while not feof 0
			strtok sType sTemp " " 1
			strtok sMajor sTemp " " 1
			strtok sMinor sTemp " " 1
			strtok sJunk sTemp " " 1
			strtok sTCIC sTemp " " 1
			strtok sState sTemp " " 1
			
			switch sState 
				case "CPB"
					sState = "INSV"
				endcase
				case "IDL"
					sState = "INSV"
				endcase
				case "INB"
					sState = "OFFL"

				endcase
				case "MB"
					sState = "OFFL"
				endcase
				default
					sState = "UNK?"
				endcase
			endswitch
			
			if strcmp sJunk "1" 
				strfmt sTemp "%s %6s %6s %6s %6s" sType sMajor sMinor sTCIC sState
				fputs 1 sTemp
				if strcmp sState "OFFL"
					strfmt sTemp2 "%s %s %s" sType sMajor sMinor
					sOFFL[iOFFLCnt] = sTemp2
					iOFFLCnt++
				endif
				strcat sTextToCopy sTemp
				;strcat sTextToCopy 
				sTemp3 = sTextToCopy
				paste_notepad()
   				sTextToCopy = ""
			endif
			fgets 0 sTemp
		endwhile
		
		iOFFLCnt = iOFFLCnt -1
		iIndex2 = 1
		
		if iOFFLCnt > 1
			extra_lf()
			sTemp3 = "Command to post OFFL'd T-1's:"
			paste_notepad()
		endif
		
		sTemp3 = "Post "
		if iOFFLCnt > 1 
			for iIndex = 1 upto iOFFLCnt
				if iIndex2 <= 5
					strcat sTemp3 sOFFL[iIndex] 
					strcat sTemp3 " "
					iIndex2++
				else
					iIndex2 = 2
					paste_notepad()
					sTemp3 = "Post "
					strcat sTemp3 sOFFL[iIndex]
					strcat sTemp3 " "
				endif
			endfor
			
			if iIndex2 >1
				paste_notepad()
			endif
		endif
	endif
	fclose 0
	fclose 1
	
	extra_lf()
	sTemp3 = "Command to post INB'd trunks:"
	paste_notepad()
	
	sTemp3 = "post g "
	strcat sTemp3 sCLLI
	strcat sTemp3 ";post b b"
	paste_notepad()
	
	extra_lf()
	sTemp3 = "Note:  This program looks at the first trunk of each T-1 to determine if a carrier is INSV or not."
	strtoclip sTemp3
	paste_notepad()
endproc

proc get_CLLI
string sTKGNum, sXMit, sTemp, sTemp2, fName2
integer iIndex
	
	fName2 = "c:\tkgtemp.txt"
	set capture path "c:\"
	set capture file "tkgtemp.txt"	
	sdlginput "Enter TKG#" "Trunk Group Number" sTKGNum DEFAULT
	transmit "quit all;table trkname;format pack^m"
	waitfor "output is column 1." forever
	waitfor ">" forever
	sXMit = "pos "
	strcat sXMit sTKGNum
	strcat sXMit "^m"
	clear
	capture on
	transmit sXMit
	waitfor ">" forever
	capture off
	transmit sQA
	waitfor ">" forever
	fopen 0 fName2 read text
	
	for iIndex = 1 upto 2
		fgets 0 sTemp
	endfor
	
	strtok sTemp2 sTemp " " 1
	strtok sCLLI sTemp " " 1
	
	fclose 0
	delfile fName2
endproc

proc paste_notepad
	strtoclip sTemp3	;Copy text to windows clipboard
	sendkey ALT 'E'		;Enter Edit Menu
	mspause 250		;Wait 1/4 second
	sendkey 'P'		;Paste
	sendkey 0x0d		;linefeed
	sTemp3 = ""		;Blank variable
endproc

proc extra_lf
	sendkey 0x0d		;linefeed
endproc
 
KODR,

Thanks for your solution. This is going to come in very useful.
 
KNOB,

"ddeinit ChanNum" and a "ddepoke ChanNum" doesn't seem to work in this script, but I got a "ddeinit SysChan" to work. The only problem is an error is gennerated (Ambiguous name detected: TmpDDE) and once "OK" is selected the statement is printed to the Word document. Any ideas to why the error is generated?

NOTE: I am not a native programmer so sorry if this looks bad
========================================
; Purpose: Establish a DDE system channel with Microsoft Word 2007
; Procomm Pluss 4.8
; Windows OS XP
; 29 July 2009
; Creator: RAK

integer iTaskID = 0

proc main

long SysChan = 0

string Appl = "c:\Program Files\Microsoft Office\Office12\Winword.exe /x /t c:\DDE_word-tst.docx"

Run Appl iTaskId
taskactivate iTaskId

pause 5

ddeinit SysChan "Winword" "system" ; Try to initiate DDE link with MS Word

if FAILURE ; If no link established,
usermsg "Unable to Establish SysChan" ; warn the user.
endif

if SUCCESS ; If link is established,
usermsg "SysChan Was Established" ; notify user
endif

ddeexecute SysChan "Hello World!"

endproc
=========================================
 
Could you show your code with how you placed the "ddeinit ChanNum" and "ddepoke ChanNum"?
 
Kodr,

I modeled this pretty closely to the documentation but I may be missing something, or be wrong all together" with the "bkmrkpoke1" bookmark in word. According to a few online articles I have read, like the one knob sites above, it seems to be necessary. When I run the script I get the "FAILURE" notification. I have also tried it without including "!BkmrkPoke1" as part of the ddeinit statement as shown below and also recieved the "FAILURE" notification.

Thanks for looking!

; Purpose: Establish a DDE system channel with Microsoft Word 2007
; Procomm Pluss 4.8
; Windows OS XP
; 29 July 2009
; Creator: RAK

integer iTaskID = 0

proc main

long SysChan = 0
long DDEChan = 0

string Appl = "c:\Program Files\Microsoft Office\Office12\Winword.exe /x /t c:\DDE_word-tst.docx"

Run Appl iTaskId
taskactivate iTaskId

pause 5

ddeinit DDEChan "Winword" "DDE_word-tst.docx!BkmrkPoke1" ; Try to initiate DDE link with MS Word

if FAILURE ; If no link established,
usermsg "Unable to Establish DDEChan" ; warn the user.
endif

if SUCCESS ; If link is established,
usermsg "ChanNum Was Established" ; notify user
endif
ddeinit SysChan "Winword" "system" ; Initiate DDE link with MS Word
ddeexecute SysChan "[SELECT(BkmrkPoke1)]"

ddepoke DDEChan "BkmrkPoke1" "Hello World!"

endproc
 
Did you create the word doc ahead of time and create a bookmark in the file called "BkmrkPoke1"?



 
I can't test this here, but I can give it a try on a computer I have set up at home over the weekend. By the way your code looks, it should be working for you.
 
I'm not having any luck getting this to work, I tried a few variations of your code so far. I'll keep looking at it as I get time. My DDE experience has all been with Excel so far.
 
I have played with it more but no new findings. It would be nice to be able to see what is going on in the background.

Does anyone have any info on MS Word 2007 DDE server commands? I can't find hardly anything but a few KB articles mostly referring to usage with Visual Basic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top