Schultz’s PowerBuilder Notes

DDE MS Word

Word bookmarks

The target Word document must have Bookmarks set up to receive the data inserted by the PowerBuilder app. To view existing:

Tools>Options>select View Bookmarks

Example:Form letter

The following uses MS Word to generate a form letter. The style is not so hot, but it has a examples of many functions to open MS Word, select the correct document, and populate several fields.

// n_cst_dde_word1:of_Seed_letter_info(datastore ads_seed_letter_info)

boolean lb_set_remote_ok, lb_execremote_fileopen
integer li_value, li_msgbox, li_fileopen, li_rc, li_run, li_ctr
long    ll_rowcount, ll_len, ll_ddehandleword, ll_ddehandledoc, i
string  ls_sd_id, ls_fname, ls_lname, ls_street, ls_street1, ls_street2, ls_city, ls_state, ls_zip, &
        ls_word_exe_arr[], ls_word_exe, ls_named, ls_docname 

// determine what Word exe to call 4/20/2006 dlmuench
ls_word_exe_arr[1] =  "C:\Program Files\Microsoft Office\Office10\WINWORD.EXE"
ls_word_exe_arr[2] =  "C:\Program Files\Microsoft Office\Office11\WINWORD.EXE"

FOR li_ctr = 1 TO UpperBound(ls_word_exe_arr)
   IF FileExists(ls_word_exe_arr[li_ctr] ) THEN
      ls_word_exe = ls_word_exe_arr[li_ctr]
      EXIT
   END IF
NEXT

lb_set_remote_ok = TRUE
lb_execremote_fileopen = FALSE

li_value = GetFileOpenName("Select Seed Letter Document to Print", + ls_docname, ls_named, "DOC", + "Doc Files (*.DOC),*.DOC" )
IF li_value > 0 THEN //user opens a file

   //try to open a channel to Word
   ll_ddehandleword = OpenChannel("WinWord", "System" )
   IF ll_ddehandleword < 0 THEN //if open channel fails, run the Word application
      li_run = Run(ls_word_exe, Minimized! )
      IF li_run < 0 THEN
         RETURN -1
      END IF
   END IF

   IF li_value = 1 THEN
      li_msgbox = MessageBox("Attention", &
                  "The file that you have chosen is" + " " + ls_docname + " " + &
                  ".~nDo you want to print the contents of this file?", &
                  Information!, OkCancel!, 2)
   END IF

  //if the previous open channel failed
  IF ll_ddehandleword < 0 THEN
     ll_ddehandleword = OpenChannel("WinWord", "System" )
     IF ll_ddehandleword < 0 THEN
        RETURN -1
     END IF
  END IF

  IF li_msgbox = 1 THEN//if OK to print
    //try to open a channel to the word document first
    ll_ddehandledoc = OpenChannel("WinWord", ls_docname)
    IF ll_ddehandledoc < 0 THEN//if the open channel fails then the word document has to be opened
       lb_execremote_fileopen = TRUE

       //Send a FileOpen command to the Word application to open a document
       li_rc = ExecRemote("[FileOpen.Name =" + "~"" + ls_docname + "~"" + "]", ll_ddehandleword)
       IF li_rc < 0 THEN
          RETURN -1
       END IF

      //Open a channel to Word document and establish a warm link
      ll_ddehandledoc = OpenChannel("WinWord", ls_docname)
      IF ll_ddehandledoc  ll_rowcount
         ls_sd_id = Trim(ads_seed_letter_info.GetItemString(i, "sd_id" ))
         ls_fname = ads_seed_letter_info.GetItemString(i, "fst_nm" )
         ls_lname = ads_seed_letter_info.GetItemString(i, "last_nm" )
         ll_len = len(ls_lname)
         ls_lname = Left(ls_lname, 1) + Lower(Right(ls_lname, ll_len - 1))
         ls_street = ""
         ls_street1 = ads_seed_letter_info.GetItemString(i, "addr_1_txt" )
         IF IsNull(ls_street1) = TRUE THEN
            ls_street1 = ""
         END IF
         ls_street2 = ads_seed_letter_info.GetItemString(i, "addr_2_txt" )
         IF IsNull(ls_street2) = TRUE THEN
            ls_street2 = ""
         END IF
         ls_street = ls_street1 + ls_street2
         ls_city = ads_seed_letter_info.GetItemString(i, "cty_nm" )
         ls_state = ads_seed_letter_info.GetItemString(i, "st_cd" )
         ls_zip = ads_seed_letter_info.GetItemString(i, "zip_cd" )
         ll_len = len(ls_zip)
         IF ll_len <= 6 THEN
            ls_zip = left(ls_zip, 5)
         ELSE
            ls_zip = left(ls_zip, 5) + '-' + right(ls_zip, ll_len - 5)
         END IF

         //Ask the DDE server application to accept data to be stored in the specified location
         li_rc = SetRemote("fname", ls_fname, ll_ddehandledoc)
         IF li_rc < 0 THEN
            lb_set_remote_ok = FALSE
         END IF

         IF lb_set_remote_ok = TRUE THEN
            li_rc = SetRemote("lname", ls_lname, ll_ddehandledoc)
            IF li_rc < 0 THEN
               lb_set_remote_ok = FALSE
            END IF
         END IF

         IF lb_set_remote_ok = TRUE THEN
            li_rc = SetRemote("street", ls_street, ll_ddehandledoc)
            IF li_rc < 0 THEN
               lb_set_remote_ok = FALSE
            END IF
         END IF

         IF lb_set_remote_ok = TRUE THEN
            li_rc = SetRemote("city", ls_city, ll_ddehandledoc)
            IF li_rc < 0 THEN
               lb_set_remote_ok = FALSE
            END IF
         END IF

         IF lb_set_remote_ok = TRUE THEN
           li_rc = SetRemote("state", ls_state, ll_ddehandledoc)
           IF li_rc < 0 THEN
              lb_set_remote_ok = FALSE
           END IF
        END IF

        IF lb_set_remote_ok = TRUE THEN
           li_rc = SetRemote("zip", ls_zip, ll_ddehandledoc)
           IF li_rc < 0 THEN
              lb_set_remote_ok = FALSE
           END IF
        END IF

        IF lb_set_remote_ok = TRUE THEN
           li_rc = SetRemote("fname2", ls_fname, ll_ddehandledoc)
           IF li_rc < 0 THEN
              lb_set_remote_ok = FALSE
           END IF
        END IF

        IF lb_set_remote_ok = TRUE THEN
           IF ls_sd_id  "" THEN
              li_rc = SetRemote("sd_id", ls_sd_id, ll_ddehandledoc)
           END IF
        END IF

        //if setremotes were successful then only do we want to print the document and save it
        IF lb_set_remote_ok = TRUE THEN
           //Send a FilePrint command to the Word document to print the document
           li_rc = ExecRemote("[FilePrint]", ll_ddehandledoc)
           IF li_rc < 0 THEN
              RETURN -1
           END IF
        END IF
        i++
     LOOP

     //Send a FileSave command to the Word document to save the changes
     li_rc = ExecRemote("[FileSave]", ll_ddehandledoc)
     IF li_rc < 0 THEN
        RETURN -1
     END IF

     //closes the channel to the word document
     li_rc = CloseChannel(ll_ddehandledoc)
     IF li_rc < 0 THEN
        RETURN -1
     END IF

     IF lb_execremote_fileopen = TRUE THEN //if execremote  used to open the file then we have to close it
        //Send a single  FileClose command to the word DDE server application (cold link)
        li_rc = ExecRemote("[FileClose()]", "WinWord", ls_docname)
        IF li_rc < 0 THEN
           RETURN -1
        END IF
     END IF

 END IF//if OK to print

 //closes the channel to the Word application
 li_rc = CloseChannel(ll_ddehandleword)
 IF li_rc  0 THEN//if 'Run' was used to start Word, we want to exit the Word application
    //Send a single  FileExit command to the word DDE server application (cold link)
     li_rc = ExecRemote("[FileExit()]", "WinWord", "System" )
     IF li_rc < 0 THEN
         RETURN -1
      END IF
   END IF
END IF//user opens a file

IF li_msgbox = 2 THEN
   RETURN 2
END IF

IF lb_set_remote_ok = FALSE THEN
   RETURN -1
END IF

RETURN 1

April 8, 2008 Posted by rick130 | OLE and DDE | , , , | No Comments Yet

DDE Functions

OpenChannel

Use OpenChannel to open a channel to a DDE server application and leave it open so you can efficiently execute more than one DDE request. This type of DDE conversation is called a warm link. Because you open a channel, the operating system does not have to poll all open applications every time you send or ask for data.

OpenChannel ( applname, topicname {, windowhandle } )

Argument

Description

applname

A string specifying the DDE name of the DDE server application

topicname

A string identifying the data or the instance of the application you want to use (for example, in Microsoft Excel, the topic name could be System or the name of an open spreadsheet)

windowhandle (optional)

The handle of the window that you want to act as the DDE client. Specify this parameter to control which window is acting as the DDE client when you have more than one open window

Return value: Returns the handle to the channel (a positive integer) if it succeeds, a negative if it fails.

Execute Remote

Sends a single command to a DDE server application, called a cold link..

ExecRemote ( command, applname, topicname )

SetRemote

Asks a DDE server application to accept data to be stored in the specified location without requiring an open channel. This syntax is appropriate when you will make only one or two requests of the server.

SetRemote ( location, value, applname, topicname )
<!–[if !supportLineBreakNewLine]–>
<!–[endif]–>

Argument

Description

Location

A string whose value is the location of the data in the server application that will accept the data. The format of location depends on the application that will receive the request. In Excel it is a column or cell, in Word it is a bookmark.

Value

A string whose value you want to send to the remote application

applname

A string whose value is the DDE name of the server application

topicname

A string identifying the data or the instance of the application that will accept the data (for example, in Microsoft Excel, the topic name could be the name of an open spreadsheet)

li_rc = SetRemote(“state”, ls_state, ll_ddehandledoc)

Return value

1

Success

-1

Link not started

-2

Request Denied

Null

Null argument sent

April 8, 2008 Posted by rick130 | OLE and DDE | , | No Comments Yet