如何自动执行邮件合并到 Word 使用 OLE 和 ODBC

文章翻译 文章翻译
文章编号: 181926 - 查看本文应用于的产品
展开全部 | 关闭全部

概要

本文演示如何使用 OLE 自动化中使用为 Windows 表的数据从一个可视 FoxPro (VFP) 的 Microsoft Office Word 2003 中创建邮件合并。

更多信息

下面是 Windows 的可视 FoxPro 代码使用 Word.Application 对象模型执行邮件合并中使用 ODBC 数据源到现有的可视 FoxPro 表。此代码可以使用现有数据源名称 (DSN) 创建在 ODBC 驱动程序管理器中,或者它可以创建一个动态使用 API 调用来添加和使用该 DSN。在下面的示例"可视 FoxPro 表"是在 ODBC 驱动程序管理器中创建 DSN,并指向 VFP 主文件夹中的 \Samples\Data 文件夹。这些路径很可能必须进行更改以反映用户的路径。

注意使用服务版本-1 (sr-1) 为 Word 97 和 Microsoft Office Word 2003 进行了测试此代码。 使用 Word 97 的以前的版本将导致类型不匹配和 $ 其它 OLE 错误。

创建一个程序,然后输入以下代码:
  ***/ Begin program code /***
   * Code to create a new data source to a VFP table.
   *
   * Use the Declare DLL function to prototype the
   * SQLConfigDataSource function.
   * Using SQLConfigDataSource prevents having to go into
   * the ODBC Driver Manager and create the DSN.

   ***
   

IF VAL(SUBSTR(VERSION(4),2,1))>7
lthemes=_screen.Themes
_screen.Themes=.f.
ENDIF

   DECLARE Integer SQLConfigDataSource in odbccp32.dll Integer, ;
      Integer, String, String

   ***
   * Create a string containing the settings appropriate to the driver.
   * The following is an example for the Microsoft VFP ODBC driver
   * accessing the Customer.dbf file.
   ***

   *** Change the path below to point to the Customer table ***
   *** in the \Samples\Data folder.                      ***
   settings="DSN=visual foxpro tables"+chr(0)+;
             "Description=VFP ODBC Driver"+chr(0)+;
             "SourceDB=e:\vfp7\samples\data"+chr(0)+;
             "SourceType=DBF"
   =SQLConfigDataSource(0,1,"Microsoft Visual FoxPro Driver",settings)

   * NOTE: Ensure there are no spaces on either side of the equal sign (=).

   ON ERROR DO errhand WITH ERROR(), MESSAGE(),  MESSAGE(1), PROGRAM( ),
   LINENO( ) && Trap OLE & other errors.

   * Initialize variables passed to Word to create form letter.
   intro1="Congratulations! You are one of our best customers since you have ;
   purchased $"
   intro2=" each month from us. "
   intro3="As a result, your maximum order amount has been increased by ;
   $2500.00. If you have any questions, please feel free to contact us."

   oWord = CREATEOBJECT("Word.Application")
      WITH oWord
         * Assign values to variables
         dsname="e:\VFP7\SAMPLES\DATA\customer.DBF"
         wformat=0
         wconfirmconv=0
         wreadonly=0
         wlinktosource=0
         waddtofilelist=0
         wpassworddoc=""
         wpasswordtemp=""
         wrevert=0
         wprotectdoc=""
         wprotecttemp=""
         wconn="DSN=visual foxpro tables;uid=;pwd=;"+;
            "sourcedb=e:\vfp7\samples\data;sourcetype=dbf"+;
            "exclusive=no;backgroundfetch=yes;collate=machine;"
         wsqlstatement="SELECT contact,company,title,address,city,;
            postalcode,STR(maxordamt,12,2) as maxordamt FROM customer ;
            WHERE (customer.maxordamt>$100000)"

         .Visible=.T.      && Make Word visible.
         .WindowState = 2   && Minimize Word.


         .Documents.Add      && Add new document.
         .Selection.InsertParagraphAfter

         .ActiveDocument.MailMerge.OpenDataSource;
            (dsname,wformat,wconfirmconv,wreadonly,wlinktosource,;
             waddtofilelist,wpassworddoc,wpasswordtemp,wrevert,;
             wprotectdoc,wprotecttemp,wconn,wsqlstatement)
         .ActiveDocument.MailMerge.EditMainDocument
         .Selection.InsertDateTime("dddd, MMMM dd, yyyy", 1)
         .Selection.MoveRight
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"contact")
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"company")
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .ActiveDocument.MailMerge.Fields.Add;
             (oWord.Selection.Range,"title")
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"address")
         .Selection.InsertAfter(", ")
         .Selection.MoveRight
         .ActiveDocument.MailMerge.Fields.Add(oWord.Selection.Range,"city")
         .Selection.InsertAfter("  ")
         .Selection.MoveRight
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"postalcode")
         .Selection.MoveRight
         .Selection.InsertParagraphAfter
         .Selection.InsertParagraphAfter
         .Selection.InsertAfter("Dear ")
         .Selection.MoveRight
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"contact")
         .Selection.MoveRight
         .Selection.InsertAfter(":")
         .Selection.MoveRight
         .Selection.InsertParagraphAfter
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .Selection.InsertAfter(intro1)
         .Selection.MoveRight
         .ActiveDocument.MailMerge.Fields.Add;
            (oWord.Selection.Range,"maxordamt")
         .Selection.MoveRight
         .Selection.InsertAfter(intro2)
         .Selection.MoveRight
         .Selection.InsertAfter(intro3)
         .Selection.MoveRight
         .Selection.InsertParagraphAfter
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .Selection.InsertAfter("Thank you,")
         .Selection.MoveRight
         .Selection.InsertParagraphAfter
         .Selection.InsertParagraphAfter
         .Selection.MoveDown
         .Selection.Fields.Add(oWord.Selection.Range,-1,"author")
         .ActiveDocument.MailMerge.Destination = 0 && new document.
         .ActiveDocument.MailMerge.Execute         && Run the merge.
         nanswer = MESSAGEBOX("Would you like to view the finished ;
         document?",36,"Switch to Word")
         DO CASE
            CASE nanswer = 6
               .ActiveWindow.View.Type = 3    && Page layout view.
               .Visible = .T.
               .Application.Activate   && Bring Word forward.
               .WindowState = 0         && Word in normal state.
               =INKEY(5)         && Wait 5 seconds then bring VFP back.
               .WindowState = 2    && Then minimize Word.
              ENDCASE
         nanswer = MESSAGEBOX("Print the document?",36,;
         "Print the merged document.")
         DO CASE
            CASE nanswer = 6         && Yes
               .ActiveDocument.PrintOut   && Print the merged form letter.
         ENDCASE

         nanswer = MESSAGEBOX("Save the document?",36,;
         "Enter the name to save the merged document.")
         DO CASE
            CASE nanswer = 6         && Yes
               cbaddoc=SPACE(25)
               DO WHILE .T.
                  DEFINE WINDOW getname FROM 1,1 TO 15,60 ;
                     FONT 'COURIER NEW',10 FLOAT GROW ZOOM
                     ACTIVATE WINDOW getname
                     cdoc=SPACE(25)
                     mquit = SPACE(1)
                  IF !EMPTY(cbaddoc)
                     @2,1 SAY "Existing file name: "+cbaddoc COLOR R+/w
                  ENDIF
                  @4,1 SAY "Enter a name for the Word document."
                  @5,1 GET cdoc color b/w VALID emptyChk(cdoc)
                  @7,25 GET mquit FUNCTION '*T Save' SIZE 2,10
                  READ CYCLE
                  CLEAR WINDOW getname
                  cdoc=ALLTRIM(cdoc)
                  cpath= .Options.DefaultFilePath(0)   && Get Word's
                                                 && default document path.
                  IF FILE(cpath+"\"+cdoc+".doc")
                     WAIT WINDOW "Document already exists! Enter another ;
                     file name." TIME 1
                     cbaddoc=cdoc
                     LOOP
                  ELSE
                     .ActiveDocument.SaveAs(cdoc)
                  ENDIF
                  .Documents.Close(0)
                  .Application.Quit
                  EXIT
               ENDDO
            CASE nanswer = 7         && No
               .Documents.Close(0)
               .Application.Quit
            ENDCASE
      ENDWITH

   ON ERROR  && Restore system error handler.
   IF VAL(SUBSTR(VERSION(4),2,1))>7
_screen.Themes=lthemes
   endif

   PROCEDURE emptychk
   PARAMETER pcdoc
   IF !EMPTY(pcdoc)
      RETURN .T.
   ELSE
      WAIT WINDOW "Enter a name for the Word Document"
      RETURN 0
   ENDIF

   PROCEDURE errhand
   PARAMETER merror, mess, mess1, mprog, mlineno
   CLEAR
   IF merror=1426
      mlineno=LTRIM(STR(LINENO()))
      merror=LTRIM(STR(merror))
      =MESSAGEBOX("You have an OLE error. Usually this is caused by
      quitting Word or canceling out of a dialog box in Word. "+CHR(13)+;
      "The error message is "+mess)
   ELSE
      mlineno=LTRIM(STR(LINENO()))
      merror=LTRIM(STR(merror))
      =MESSAGEBOX("An error occured on line "+mlineno+;
      ". The error message is "+mess+" The error number is "+merror)
   ENDIF
   ON ERROR
   RETURN
   ***/ End program code /***

						

参考

有关如何获得 Windows 97 服务的 Word 版本 1 (sr-1) 的其他信息请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
172475如何获取和安装 MS Office 97 sr-1
Microsoft Visual Basic 应用程序帮助 (97") 的 ; 在"OLE"上的搜索

属性

文章编号: 181926 - 最后修改: 2005年2月16日 - 修订: 3.4
这篇文章中的信息适用于:
  • Microsoft Visual FoxPro 3.0 标准版
  • Microsoft Visual FoxPro 3.0b 标准版
  • Microsoft Visual FoxPro 5.0 标准版
  • Microsoft Visual FoxPro 5.0a
  • Microsoft Visual FoxPro 6.0 专业版
  • Microsoft Visual FoxPro 7.0 Professional Edition
  • Microsoft Visual FoxPro 8.0 专业版
  • Microsoft Visual FoxPro 9.0 Professional Edition
关键字:?
kbmt kbautomation kbhowto kbinterop KB181926 KbMtzh
机器翻译
注意:这篇文章是由无人工介入的微软自动的机器翻译软件翻译完成。微软很高兴能同时提供给您由人工翻译的和由机器翻译的文章, 以使您能使用您的语言访问所有的知识库文章。然而由机器翻译的文章并不总是完美的。它可能存在词汇,语法或文法的问题,就像是一个外国人在说中文时总是可能犯这样的错误。虽然我们经常升级机器翻译软件以提高翻译质量,但是我们不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的错误使用所引起的任何直接的, 或间接的可能的问题负责。
点击这里察看该文章的英文版: 181926
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。

提供反馈

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com