Lm th? no ? t? ?ng ho mail merge ? Word b?ng cch s? d?ng OLE v ODBC

D?ch tiu ? D?ch tiu ?
ID c?a bi: 181926 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

TM T?T

Bi vi?t ny ch?ng t? lm th? no ? s? d?ng t? ?ng ha OLE ? t?o m?t mail merge trong Microsoft Office Word 2003 b?ng cch s? d?ng d? li?u t? m?t Visual FoxPro (VFP) cho C?a s? b?ng.

THNG TIN THM

D?i y l o?n code Visual FoxPro for Windows b?ng cch s? d?ng Word.Application m h?nh ?i t?ng mail merge b?ng cch s? d?ng m?t ngu?n d? li?u ODBC ? th?c hi?n m?t hi?n c Visual FoxPro b?ng. M? ny c th? s? d?ng m?t ngu?n d? li?u hi?n c tn (DSN), t?o ra trong qu?n l? tr?nh i?u khi?n ODBC, ho?c n c th? t?o ra m?t ngy cc bay b?ng cch s? d?ng m?t API g?i thm v s? d?ng DSN. Trong v d? d?i y, "Visual FoxPro bn" l DSN t?o trong qu?n l? tr?nh i?u khi?n ODBC v i?m ?n th m?c \Samples\Data trong th m?c nh VFP. Cc ?ng d?n s? r?t c th? c ph?i ?c thay ?i ? ph?n nh nh?ng ng?i s? d?ng ?ng d?n.

Chu y M? ny ?c th? nghi?m v?i d?ch v? pht hnh-1 (SR-1) cho Word 97 v Microsoft Office Word 2003. B?ng cch s? d?ng cc phin b?n tr?c c?a Word 97 gy ra m?t lo?i Mismatch v OLE khc l?i.

T?o ra m?t chng tr?nh v nh?p vo o?n m? sau:
  ***/ 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 /***

						

THAM KH?O

? bi?t thm chi ti?t v? lm th? no ? c ?c phin b?n Word cho Windows 97 d?ch v? 1 (SR-1), nh?p vo s? bi vi?t sau ? xem bi vi?t trong c s? ki?n th?c Microsoft:
172475Lm th? no ? c ?c v ci ?t MS Office 97 SR-1
Microsoft Visual Basic cho cc ?ng d?ng tr? gip (t? ' 97); t?m ki?m trn "OLE"

Thu?c tnh

ID c?a bi: 181926 - L?n xem xt sau cng: 20 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft Visual FoxPro 5.0 Standard Edition
  • Microsoft Visual FoxPro 6.0 Professional Edition
  • Microsoft Visual FoxPro 7.0 Professional Edition
  • Microsoft Visual FoxPro 8.0 Professional Edition
  • Microsoft Visual FoxPro 9.0 Professional Edition
T? kha:
kbautomation kbhowto kbinterop kbmt KB181926 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:181926

Cung cp Phan hi

 

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