mail merge

harj

New Member
Dear All,
I am the new boy in the forum world and am looking for some info. I want to use WORD to merge some documents and havent got a clue. I know I have to use mailemerge ole but thats all. Any help on what the template must look like and the data file plus the code to do the merging in Progress would be greatly appreciated. I know its a lot to ask but one of you clever guys must have done it already and are able to help a poor guy like me.

Harj
 

Chris Kelleher

Administrator
Staff member
Here's a program that I wrote that pulls information from our Progress database, exports the data to a comma delimted
file, and does a mail merge with MS Word, and then sends the finished Word document via Outlook. This should be
able to get you started.

-Chris

Code:
&ANALYZE-SUSPEND _VERSION-NUMBER AB_v9r12 GUI ADM2
&ANALYZE-RESUME
&Scoped-define WINDOW-NAME wWin
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _DEFINITIONS wWin 
/*------------------------------------------------------------------------

  File: 

  Description: from cntnrwin.w - ADM SmartWindow Template

  Input Parameters:
      <none>

  Output Parameters:
      <none>

  History: New V9 Version - January 15, 1998
          
------------------------------------------------------------------------*/
/*          This .W file was created with the Progress AB.              */
/*----------------------------------------------------------------------*/

/* Create an unnamed pool to store all the widgets created 
     by this procedure. This is a good default which assures
     that this procedure's triggers and internal procedures 
     will execute in this procedure's storage, and that proper
     cleanup will occur on deletion of the procedure. */

CREATE WIDGET-POOL.

/* ***************************  Definitions  ************************** */
&GLOBAL-DEFINE NewLine  CHR(13)
&GLOBAL-DEFINE Delim    ","
&GLOBAL-DEFINE csvfile  "rtufax.csv"
&GLOBAL-DEFINE docfile  "rtufax.doc"
&GLOBAL-DEFINE sendfile "sendfax.doc"

/* Parameters Definitions ---                                           */

/* Local Variable Definitions ---                                       */
DEFINE VARIABLE lEndFaxProcess AS LOGICAL NO-UNDO INITIAL TRUE.

DEFINE VARIABLE cDir AS CHARACTER NO-UNDO.

cDir = "c:\rtufax\".

DEFINE VARIABLE cCSVFile AS CHARACTER NO-UNDO.

cCSVFile = cDir + {&csvfile}.

DEFINE VARIABLE cDOCFile AS CHARACTER NO-UNDO.

cDOCFile = cDir + {&docfile}.

DEFINE VARIABLE cSendFile AS CHARACTER  NO-UNDO.

cSendFile = cDir + {&sendfile}.

/* Buffer for locked fax record */
DEFINE BUFFER buffer-rt-fax FOR rt-fax.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&ANALYZE-SUSPEND _UIB-PREPROCESSOR-BLOCK 

/* ********************  Preprocessor Definitions  ******************** */

&Scoped-define PROCEDURE-TYPE SmartWindow
&Scoped-define DB-AWARE no

&Scoped-define ADM-CONTAINER WINDOW

&Scoped-define ADM-SUPPORTED-LINKS Data-Target,Data-Source,Page-Target,Update-Source,Update-Target,Filter-target,Filter-Source

/* Name of first Frame and/or Browse and/or first Query                 */
&Scoped-define FRAME-NAME fMain

/* Standard List Definitions                                            */
&Scoped-Define ENABLED-OBJECTS EDITOR-Message BUTTON-Start 
&Scoped-Define DISPLAYED-OBJECTS EDITOR-Message 

/* Custom List Definitions                                              */
/* List-1,List-2,List-3,List-4,List-5,List-6                            */

/* _UIB-PREPROCESSOR-BLOCK-END */
&ANALYZE-RESUME


/* ************************  Function Prototypes ********************** */

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD DisplayMessage wWin 
FUNCTION DisplayMessage RETURNS CHARACTER
  ( cMessage AS CHARACTER )  FORWARD.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD getRegistryValue wWin 
FUNCTION getRegistryValue RETURNS CHARACTER
    ( pBaseKey as char,   /* i.e. "HKEY_..."  */
      pKeyName as char,   /* main key, i.e. "software\ACME..." */
      pSecName as char,   /* section */
      pItem    as char    /* item identifier, "" = return list, ? = default */
     )  FORWARD.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION-FORWARD SwitchButtons wWin 
FUNCTION SwitchButtons RETURNS CHARACTER
  ( /* parameter-definitions */ )  FORWARD.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


/* ***********************  Control Definitions  ********************** */

/* Define the widget handle for the window                              */
DEFINE VAR wWin AS WIDGET-HANDLE NO-UNDO.

/* Definitions of the field level widgets                               */
DEFINE BUTTON BUTTON-Start 
     LABEL "Start Process" 
     SIZE 15 BY 1.24.

DEFINE BUTTON BUTTON-Stop 
     LABEL "Stop Process" 
     SIZE 15 BY 1.24.

DEFINE VARIABLE EDITOR-Message AS CHARACTER 
     VIEW-AS EDITOR SCROLLBAR-VERTICAL
     SIZE 70 BY 13.52 NO-UNDO.


/* ************************  Frame Definitions  *********************** */

DEFINE FRAME fMain
     EDITOR-Message AT ROW 2 COL 5 NO-LABEL
     BUTTON-Start AT ROW 16 COL 16
     BUTTON-Stop AT ROW 16 COL 40
    WITH 1 DOWN NO-BOX KEEP-TAB-ORDER OVERLAY 
         SIDE-LABELS NO-UNDERLINE THREE-D 
         AT COL 1 ROW 1
         SIZE 80 BY 17.


/* *********************** Procedure Settings ************************ */

&ANALYZE-SUSPEND _PROCEDURE-SETTINGS
/* Settings for THIS-PROCEDURE
   Type: SmartWindow
   Allow: Basic,Browse,DB-Fields,Query,Smart,Window
   Container Links: Data-Target,Data-Source,Page-Target,Update-Source,Update-Target,Filter-target,Filter-Source
 */
&ANALYZE-RESUME _END-PROCEDURE-SETTINGS

/* *************************  Create Window  ************************** */

&ANALYZE-SUSPEND _CREATE-WINDOW
IF SESSION:DISPLAY-TYPE = "GUI":U THEN
  CREATE WINDOW wWin ASSIGN
         HIDDEN             = YES
         TITLE              = "Restorative Services Fax Status"
         HEIGHT             = 17
         WIDTH              = 80
         MAX-HEIGHT         = 28.81
         MAX-WIDTH          = 146.2
         VIRTUAL-HEIGHT     = 28.81
         VIRTUAL-WIDTH      = 146.2
         RESIZE             = no
         SCROLL-BARS        = no
         STATUS-AREA        = no
         BGCOLOR            = ?
         FGCOLOR            = ?
         THREE-D            = yes
         MESSAGE-AREA       = no
         SENSITIVE          = yes.
ELSE {&WINDOW-NAME} = CURRENT-WINDOW.
/* END WINDOW DEFINITION                                                */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _INCLUDED-LIB wWin 
/* ************************* Included-Libraries *********************** */

{src/adm2/containr.i}

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME




/* ***********  Runtime Attributes and AppBuilder Settings  *********** */

&ANALYZE-SUSPEND _RUN-TIME-ATTRIBUTES
/* SETTINGS FOR WINDOW wWin
  VISIBLE,,RUN-PERSISTENT                                               */
/* SETTINGS FOR FRAME fMain
                                                                        */
/* SETTINGS FOR BUTTON BUTTON-Stop IN FRAME fMain
   NO-ENABLE                                                            */
IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
THEN wWin:HIDDEN = yes.

/* _RUN-TIME-ATTRIBUTES-END */
&ANALYZE-RESUME

 



/* ************************  Control Triggers  ************************ */

&Scoped-define SELF-NAME wWin
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CONTROL wWin wWin
ON END-ERROR OF wWin /* Restorative Services Fax Status */
OR ENDKEY OF {&WINDOW-NAME} ANYWHERE DO:
  /* This case occurs when the user presses the "Esc" key.
     In a persistently run window, just ignore this.  If we did not, the
     application would exit. */
  IF THIS-PROCEDURE:PERSISTENT THEN RETURN NO-APPLY.
END.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CONTROL wWin wWin
ON WINDOW-CLOSE OF wWin /* Restorative Services Fax Status */
DO:
  /* This ADM code must be left here in order for the SmartWindow
     and its descendents to terminate properly on exit. */
  APPLY "CLOSE":U TO THIS-PROCEDURE.
  RETURN NO-APPLY.
END.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&Scoped-define SELF-NAME BUTTON-Start
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CONTROL BUTTON-Start wWin
ON CHOOSE OF BUTTON-Start IN FRAME fMain /* Start Process */
DO:
  RUN StartFaxing IN THIS-PROCEDURE.
END.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&Scoped-define SELF-NAME BUTTON-Stop
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CONTROL BUTTON-Stop wWin
ON CHOOSE OF BUTTON-Stop IN FRAME fMain /* Stop Process */
DO:
  RUN StopFaxing IN THIS-PROCEDURE.
END.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&Scoped-define SELF-NAME EDITOR-Message
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CONTROL EDITOR-Message wWin
ON ANY-PRINTABLE OF EDITOR-Message IN FRAME fMain
DO:
  RETURN NO-APPLY.
END.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&UNDEFINE SELF-NAME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _CUSTOM _MAIN-BLOCK wWin 


/* ***************************  Main Block  *************************** */

/* Include custom  Main Block code for SmartWindows. */
{src/adm2/windowmn.i}

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


/* **********************  Internal Procedures  *********************** */

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE adm-create-objects wWin  _ADM-CREATE-OBJECTS
PROCEDURE adm-create-objects :
/*------------------------------------------------------------------------------
  Purpose:     Create handles for all SmartObjects used in this procedure.
               After SmartObjects are initialized, then SmartLinks are added.
  Parameters:  <none>
------------------------------------------------------------------------------*/

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE disable_UI wWin  _DEFAULT-DISABLE
PROCEDURE disable_UI :
/*------------------------------------------------------------------------------
  Purpose:     DISABLE the User Interface
  Parameters:  <none>
  Notes:       Here we clean-up the user-interface by deleting
               dynamic widgets we have created and/or hide 
               frames.  This procedure is usually called when
               we are ready to "clean-up" after running.
------------------------------------------------------------------------------*/
  /* Delete the WINDOW we created */
  IF SESSION:DISPLAY-TYPE = "GUI":U AND VALID-HANDLE(wWin)
  THEN DELETE WIDGET wWin.
  IF THIS-PROCEDURE:PERSISTENT THEN DELETE PROCEDURE THIS-PROCEDURE.
END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE enable_UI wWin  _DEFAULT-ENABLE
PROCEDURE enable_UI :
/*------------------------------------------------------------------------------
  Purpose:     ENABLE the User Interface
  Parameters:  <none>
  Notes:       Here we display/view/enable the widgets in the
               user-interface.  In addition, OPEN all queries
               associated with each FRAME and BROWSE.
               These statements here are based on the "Other 
               Settings" section of the widget Property Sheets.
------------------------------------------------------------------------------*/
  DISPLAY EDITOR-Message 
      WITH FRAME fMain IN WINDOW wWin.
  ENABLE EDITOR-Message BUTTON-Start 
      WITH FRAME fMain IN WINDOW wWin.
  {&OPEN-BROWSERS-IN-QUERY-fMain}
  VIEW wWin.
END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE exitObject wWin 
PROCEDURE exitObject :
/*------------------------------------------------------------------------------
  Purpose:  Window-specific override of this procedure which destroys 
            its contents and itself.
    Notes:  
------------------------------------------------------------------------------*/

  APPLY "CLOSE":U TO THIS-PROCEDURE.
  RETURN.

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE ExportFaxData wWin 
PROCEDURE ExportFaxData :
/*------------------------------------------------------------------------------
  Purpose:     
  Parameters:  <none>
  Notes:       
------------------------------------------------------------------------------*/

DisplayMessage("Exporting Fax Data for a new fax...").

OUTPUT TO VALUE(cCSVFile).

EXPORT DELIMITER {&Delim}
    "to-name"
    "from-name"
    "provider-phone"
    "fax-number"
    "phone-number"
    "fax-date"
    "subject"
    "patient-name"
    "id-number"
    "cos-trans"
    "req-visits"
    "req-weeks"
    "appr-visits"
    "appr-weeks"
    "start-date"
    "end-date".

EXPORT DELIMITER {&Delim}
     buffer-rt-fax.to-name
     buffer-rt-fax.from-name
     buffer-rt-fax.provider-phone
     buffer-rt-fax.fax-number
     buffer-rt-fax.phone-number
     buffer-rt-fax.fax-date
     buffer-rt-fax.subject
     buffer-rt-fax.patient-name
     buffer-rt-fax.id-number
     buffer-rt-fax.cos-trans
     buffer-rt-fax.req-visits
     buffer-rt-fax.req-weeks
     buffer-rt-fax.appr-visits
     buffer-rt-fax.appr-weeks
     buffer-rt-fax.start-date
     buffer-rt-fax.end-date.

OUTPUT CLOSE.

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE FaxMailMerge wWin 
PROCEDURE FaxMailMerge :
/*------------------------------------------------------------------------------
Purpose      : This procedure will run with windows 32 bit.  It is intended
               to run MS Word Office '97 and merge a Data Source text file 
               with a Mail Merge Document, without user intervention.
Create Date  : 10/26/98

Reference Only: 
    Below, is the the original Macro in MSWord/Office '97 
    which autoexecuted the mail merge within Word.  Following is the 
    translation into the 4GL which executes the Mail Merge from
    outside of Word.
       
    Private Sub Document_Open()
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .Execute
            Application.Options.PrintBackground = True
            Application.ActiveDocument.PrintOut False
        End With
  
        ActiveDocument.Close wdDoNotSaveChanges
            Application.Quit wdDoNotSaveChanges

    End Sub       

History:

------------------------------------------------------------------------------*/

/*** Input Parameters ***/
DEF INPUT PARAMETER cMMLetterPath  AS CHAR NO-UNDO. /* Mail Merge Letter */
DEF INPUT PARAMETER cMMDataSource  AS CHAR NO-UNDO. /* Name of data source file */

/*** Local Variable Definitions ***/
DEF VAR hWordApp    AS COM-HANDLE NO-UNDO.
DEF VAR hMMLetter   AS COM-HANDLE NO-UNDO.
DEF VAR hNewDoc     AS COM-HANDLE NO-UNDO.
DEF VAR lLoop       AS LOGICAL NO-UNDO INIT true.

DEF VAR cErrMsg     AS CHAR NO-UNDO INIT 
                    "Error printing mail merge letter from procedure 'FaxMailMerge'.  ".
DEF VAR cWordError  AS CHAR NO-UNDO.
DEF VAR i           AS INT  NO-UNDO.
DEF VAR iMTI        AS INT  NO-UNDO.

/****************************** Main Block ************************************/
DisplayMessage("Starting Mail Merge with Fax Document...").

/**--------------------------------------------------------------------
   Create automation object.   (Chapt 8.2.3)
     
   This option creates a connection to a new or existing instance of 
   the Automation object implicitly defined by the specified file. 
   This option identifies the Automation object and its Server from 
   the specified filename extension, as defined in the registry. If 
   the file is not already open in the Automation Object, it will be 
   opened. If the file is already open in another Server instance, 
   this connection option fails with a "File in Use" error. 
   
   Microsoft Object Library must be selected for COM objects to work.
     Go to Tools, Macro, Visual Basic Editor, Tools, & References.  
     Select 'Microsoft Word 8.0 Object Library'.
--------------------------------------------------------------------**/
CREATE "Word.Application" hWordApp NO-ERROR.
if error-status:error or not valid-handle(hWordApp) then 
do:
    cWordError = cErrMsg + "Word could not be found on this machine, or you" +
                 "do not have Word for Office '97.  Please verify that Word" +
                 "is installed on your PC and that it is the correct version" +
                 "or call your system administrator.". 
    return error cWordError. 
end.

Merge-Data:
DO:
    /*------------------------------------------------------------------------- 
      Access COM object properties and methods using hWordApp. (Chapt 7.2) 
    -------------------------------------------------------------------------*/
    
    /**-- Make MS Word Visible on the desktop --**/
    hWordApp:Visible = FALSE.
    
    /**-- Close any other Open documents --**/
    hWordApp:Documents:Close(no) NO-ERROR.
    if error-status:error then 
    do:
        cWordError = cErrMsg + "Could not close previously opened documents before executing mail merge.".
        leave Merge-Data.
    end.            
    
    /*-----------------------------------------------------------------
      Do not display the built-in warning message whenever you open a 
        document or template that might contain Macro Viruses.
        Macro Virus Protection under the Tools/Options/General tab. 
    -----------------------------------------------------------------*/
    hWordApp:Options:VirusProtection(False) NO-ERROR.
    
    /**-- Open the Mail Merge Letter and assign it to a com handle --**/
    ASSIGN hMMLetter = hWordApp:Documents:Open(cMMLetterPath,no,yes,yes,,,no,,) NO-ERROR.
    if not valid-handle(hMMLetter) or error-status:error then 
    do:
        cWordError = cErrMsg + "Invalid COM-handle for Mail Merge letter or, could not open" + 
                     cMMLetterPath + ".  " + "Check for the existence of the document and that" +
                     "the path was entered correctly.".
        leave Merge-Data.
    end.
   
    /**-- Open the DataSource and link it to the Active Document, which is the Mail Merge Letter --**/
    hMMLetter:MailMerge:OpenDataSource(cMMDataSource) NO-ERROR.
    if error-status:error then 
    do:
        release object hMMLetter.
        cWordError = cErrMsg + "Could not open Data Source:" + cMMDataSource + ".".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.
    end.
    
    /**-- Send Letter and each merged data record to a new document (during execution) --**/
    assign hMMLetter:MailMerge:Destination = 0.
            
    /**-- Execute the Mail Merge --**/
    hMMLetter:MailMerge:Execute() NO-ERROR.
    if error-status:error then 
    do:
        cWordError = cErrMsg + "MailMerge:Execute() command failed.".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.
    end.
    
    /**-- Close the Original Mail Merge Document and release the object --**/
    hMMLetter:Close(no).
    RELEASE OBJECT hMMLetter.
    
    /**-- Assign the new document to a COM handle Item(1) refers to the first doc object.  Each page for that
          document represents a record in the data source. Copy of your template as a page. --**/
    ASSIGN hNewDoc = hWordApp:Documents:Item(1) NO-ERROR.
    if not valid-handle(hNewDoc) then
    do:
        cWordError = cErrMsg + "hNewDoc handle invalid.".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.        
    end.
    if error-status:error then 
    do:
        cWordError = cErrMsg + "Invalid COM-handle for Item(1) in the document collection.".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.
    end.
    
    /**-- Print any Background Documents --**/
    hWordApp:Options:PrintBackground(True) NO-ERROR.
    if error-status:error then 
    do:
        cWordError = cErrMsg + "PrintBackground error.".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.
    end.
   
    hNewDoc:SaveAs(cSendFile).
    if error-status:error then 
    do:
        cWordError = cErrMsg + "SaveAs error.".
        do i = 1 to error-status:num-messages:
            message error-status:get-message(i).
        end.
        leave Merge-Data.
    end.
    RELEASE OBJECT hNewDoc NO-ERROR.
    
    /**-- Close any open documents --**/
    hWordApp:Documents:Close(no) NO-ERROR.
      
end.

hWordApp:Quit(). 

/**-- (Chapt 8.3) Release the Automation object --**/ 
/**--------------------------------------------------------------
To avoid misleading errors while developing your application, 
set all equivalent component handles to the unknown value (?) 
after releasing Automation objects on any one of them.
--------------------------------------------------------------**/
RELEASE OBJECT hWordApp.
assign hWordApp = ?
       hMMLetter = ?
       hNewDoc = ?.

if cWordError <> "" then
DO:
    MESSAGE "ERROR:" cWordError VIEW-AS ALERT-BOX ERROR.
    RETURN cWordError.
END.
else return "".

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE SendEmail wWin 
PROCEDURE SendEmail :
/*------------------------------------------------------------------------------
  Purpose:     
  Parameters:  <none>
  Notes:       
------------------------------------------------------------------------------*/
DEFINE INPUT PARAMETER ipMessageCompany AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipMessageSendTo  AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipMessageSendNum AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipMessageSubject AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipMessageText    AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipMessageAttach  AS CHARACTER NO-UNDO.

DEFINE VARIABLE iCounter  AS INTEGER   NO-UNDO.
DEFINE VARIABLE cSendNum  AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSendTo   AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFileName AS CHARACTER NO-UNDO.

define variable chSession   as com-handle no-undo.
define variable chMessage   as com-handle no-undo.
define variable chRecipient as com-handle no-undo.
define variable chAttach    as com-handle no-undo.

DisplayMessage("Sending Email Fax from Outlook...").

create "MAPI.session" chSession.


chSession:logon( 
    getRegistryValue("HKEY_CURRENT_USER", 
                   "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem", 
                   "Profiles", 
                   "DefaultProfile")
                ).

chMessage = chSession:outbox:messages:add.

chMessage:Type    = "IPM.Note".
chMessage:Subject = ipMessageSubject.
chMessage:Text    = ipMessageText + CHR(10).

cFileName         = cSendFile.

chAttach          = chMessage:Attachments:Add.
chAttach:source   = cFileName.
chAttach:Type     = 1.

REPEAT iCounter = 1 TO NUM-ENTRIES(ipMessageSendNum):
    ASSIGN
      cSendTo        = REPLACE(ipMessageSendTo," ","")
      cSendNum       = TRIM( ENTRY (iCounter,ipMessageSendNum) )
      cSendNum       = IF   (INDEX(cSendNum,"@") EQ 0)
                       THEN ("[FAX:" + cSendTo + "@" + cSendNum + "]")
                       ELSE cSendNum.
    chRecipient      = chMessage:Recipients:Add.
    chRecipient:name = cSendNum.
    chRecipient:Type = 1.
    chRecipient:resolve.
END.

chMessage:Update.
chMessage:send(Yes, No, 0).

release object chAttach.
release object chRecipient.
release object chMessage.
release object chSession.

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE StartFaxing wWin 
PROCEDURE StartFaxing :
/*------------------------------------------------------------------------------
  Purpose:     
  Parameters:  <none>
  Notes:       
------------------------------------------------------------------------------*/
  SwitchButtons().

  DisplayMessage("Fax Process Started...").

  DO WHILE NOT(lEndFaxProcess):

      processfax:
      DO TRANSACTION:

          FIND FIRST buffer-rt-fax WHERE 
              buffer-rt-fax.fax-sent = FALSE
              EXCLUSIVE-LOCK NO-WAIT NO-ERROR.

          IF LOCKED(buffer-rt-fax) THEN
          DO:
              PAUSE 1.
          END.

          IF AVAILABLE(buffer-rt-fax) THEN
          DO:

              IF (buffer-rt-fax.fax-sent) THEN LEAVE processfax.

              RUN ExportFaxData IN THIS-PROCEDURE.
              PAUSE 1.

              CASE buffer-rt-fax.company:
                  WHEN "IBC" THEN 
                      ASSIGN cDOCFile = cDir + "ibc.doc".
                  WHEN "AMERIHEALTH" THEN
                      ASSIGN cDOCFile = cDir + "amerihealth.doc".
                  OTHERWISE
                      ASSIGN cDOCFile = cDir + {&docfile}.
              END CASE.

              RUN FaxMailMerge  IN THIS-PROCEDURE
                  (cDOCFile, cCSVFile).
              PAUSE 1.

              RUN SendEmail     IN THIS-PROCEDURE
                  (buffer-rt-fax.company,
                   buffer-rt-fax.to-name,
                   buffer-rt-fax.fax-number,
                   buffer-rt-fax.subject,
                   "",
                   cSendFile).
              PAUSE 1.

              ASSIGN
                buffer-rt-fax.fax-sent = TRUE.
          END.

      END.

      RELEASE buffer-rt-fax.
      PROCESS EVENTS.

  END.

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _PROCEDURE StopFaxing wWin 
PROCEDURE StopFaxing :
/*------------------------------------------------------------------------------
  Purpose:     
  Parameters:  <none>
  Notes:       
------------------------------------------------------------------------------*/
  SwitchButtons().

  DisplayMessage("Fax Process Stopped").

END PROCEDURE.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

/* ************************  Function Implementations ***************** */

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION DisplayMessage wWin 
FUNCTION DisplayMessage RETURNS CHARACTER
  ( cMessage AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose:  
    Notes:  
------------------------------------------------------------------------------*/

ASSIGN
    EDITOR-Message:SCREEN-VALUE IN FRAME {&FRAME-NAME} =
      EDITOR-Message:SCREEN-VALUE IN FRAME {&FRAME-NAME} + {&NewLine} +
      cMessage + {&NewLine}.

  RETURN "".   /* Function return value. */

END FUNCTION.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION getRegistryValue wWin 
FUNCTION getRegistryValue RETURNS CHARACTER
    ( pBaseKey as char,   /* i.e. "HKEY_..."  */
      pKeyName as char,   /* main key, i.e. "software\ACME..." */
      pSecName as char,   /* section */
      pItem    as char    /* item identifier, "" = return list, ? = default */
     ) :
/*------------------------------------------------------------------------------
  Purpose:  
    Notes:  
------------------------------------------------------------------------------*/
def var iValue as char no-undo.

   load pKeyName base-key pBaseKey no-error.

   if not error-status:error then
   do:
      use pKeyName.
      if pItem = ? then
         get-key-value section pSecName
                       key     default
                       value   iValue.
      else
         get-key-value section pSecName
                       key     pItem
                       value   iValue.
      if iValue = ? then 
         iValue = "".
      unload pKeyName no-error.
   end. /* if no error*/

   return iValue.

END FUNCTION.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION SwitchButtons wWin 
FUNCTION SwitchButtons RETURNS CHARACTER
  ( /* parameter-definitions */ ) :
/*------------------------------------------------------------------------------
  Purpose:  
    Notes:  
------------------------------------------------------------------------------*/

ASSIGN
    BUTTON-Start:SENSITIVE IN FRAME {&FRAME-NAME} = 
      NOT(BUTTON-Start:SENSITIVE IN FRAME {&FRAME-NAME})
    BUTTON-Stop:SENSITIVE IN FRAME {&FRAME-NAME} = 
      NOT(BUTTON-Stop:SENSITIVE IN FRAME {&FRAME-NAME})
    lEndFaxProcess = NOT(lEndFaxProcess).

RETURN "".   /* Function return value. */

END FUNCTION.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME
 

harj

New Member
Cheers Chris,

I have had a quick look and it looks impressive. I will work my way through it.

Thanks

Harj
 

Chris Kelleher

Administrator
Staff member
Harj-

Your very welcome... obviously you will need to change the fields that you want to export, and the word document names.

If you have any questions, just feel free to ask away :)

-Chris
 
Top