如何在 Word 97 中,或从 Visual FoxPro 为 Windows 5.0 及更高版本的 Office Word 2003 中创建一个已分类的表

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

概要

本文演示了如何创建和设置 Microsoft Word 97 或从 Visual FoxPro 为 Windows 5.0 和更高版本使用 OLE 自动化的 Microsoft Office Word 2003 中的表的格式。 表包含一个分组字段实例、 状态、 公司或客户销售的标识。本示例按国家或地区进行分组。在 Word 文档的数据收集到一个临时的游标使用 SQL 查询。

更多信息

对于正常工作的此代码在查询中的第一个字段必须是在 Word 97 报告中使用的分组字段。例如,如果需要使用报表的表中的所有字段,但要在组的字段的字段是不将源表中的第一个字段然后发出类似于以下查询:
SELECT state,* FROM Customer GROUP BY state, custid INTO CURSOR category
				
这会强制组字段状态本例将第一个字段在查询结果中。

注意: 此代码仅适用于使用服务版本-1 为 Word 97。使用 Word 97 的以前的版本将导致一个"类型不匹配"和其他 OLE 错误。

创建一个程序,然后输入代码下面给出。在执行代码时用户将看到两个提示。这些提示询问用户如何格式化 Word 文档。在第一个提示,询问用户是否要有或没有每个各自的页上分组。第二个问题会询问用户是否在表的行之间的下划线或不。这些问题是只是查看首选项和显示设置如何程序员可能会在表格的格式在 Word 中与这些及其他首选项。

下面是示例代码:
    */ Begin program code /*
       CLEAR
       SET TALK OFF
       SET CONSOLE OFF
       ctempfield = ""               && Variable to hold group category.
       headings_added = .F.
       newgrouppage = .F.

   * Make sure the FIRST field in the SELECT is the field the report
   * is categorized by. The Customer table is located in the
   * \VFP\Samples\Data folder. In Visual FoxPro 6.0, the Customer table is
   * in the Microsoft Visual Studio\Common\Samples\Data folder.
   
    ** We toggle the value of SET ENGINEBEHAVIOR in VFP 8.0 and
    ** 9.0 to allow this SELECT-SQL command to function. 
    **  For more information on the SET ENGIENBEHAVIOR command,
    ** see the VFP Help file. 

   IF "8.0"$VERSION() OR "9.0"$VERSION()
x=SET("enginebehavior")
SET ENGINEBEHAVIOR 70
   SELECT country, company, contact, title,maxordamt,phone;
   FROM HOME()+"Samples\Data\testdata!customer" GROUP BY;
      country,cust_id INTO CURSOR category
      SET ENGINEBEHAVIOR x
      ELSE
      SELECT country, company, contact, title,maxordamt,phone;
   FROM HOME()+"Samples\Data\testdata!customer" GROUP BY;
      country,cust_id INTO CURSOR category
endif

   IF _TALLY > 0
      oWord = CREATEOBJECT("Word.Application")
      oWord.Documents.Add
      owRange = oWord.Activedocument.Range(0,0)
      numcols = FCOUNT()-1   && Get number of fields for detail section
      oWord.Activedocument.Tables.Add(owRange, 1, numcols)

      * First prompt, separate pages for each group.
      nanswer = messagebox("Put each group on a new page?",36,;
         "Sepatate Pages")
      DO CASE
         CASE nanswer = 6         && Yes
            newgrouppage = .T.
         CASE nanswer = 7         && No
            headings_added = .F.
      ENDCASE

      WAIT WINDOW "Please wait while the data is formatted in Word.";
         + CHR(13)+"This may take several minutes..." NOWAIT

      DO WHILE !EOF()
         ctempfield = EVAL(FIELD(1)) && Set 1st field in table as category
         WITH oWord
            .Selection.Font.Reset
            .Selection.TypeText(EVAL(FIELD(1)))
            .Selection.SelectRow
            .Selection.ParagraphFormat.Alignment = 1
            .Selection.Font.Name = "Arial"
            .Selection.Font.Size = 16
            .Selection.Font.Bold = .T.
            .Selection.SelectRow
            .Selection.Cells.Merge
            .Selection.MoveRight(12)
            .Selection.Cells.Split(1,numcols)

            IF NOT headings_added && Put at least one heading in document
               .Selection.MoveRight(12)
               FOR i = 2 TO FCOUNT()
                  .Selection.Font.Italic = .T.
                  .Selection.ParagraphFormat.Alignment = 1
                  .Selection.Font.Name = "Times New Roman"
                  .Selection.Font.Size = 8
                  .Selection.TypeText((FIELD(i)))
                  .Selection.MoveRight(12)
                  headings_added = .T.
               ENDFOR
            ENDIF

            FOR i = 2 TO FCOUNT()
               curfield = EVAL(FIELD(i))
   * Check data type. Does not check Double, Float, Integer, General, Memo.
               IF TYPE((FIELD(i)))<>"C"
               DO CASE
                  CASE TYPE((FIELD(i))) = "D"         && Date field
                     curfield = DTOC((FIELD(i)))
                  CASE TYPE((FIELD(i))) = "N"         && Numerical
                     curfield = STR((FIELD(i)))
                  CASE TYPE((FIELD(i))) = "Y"         && Currency
                     curfield = STR(EVAL(FIELD(i)),8,2)
                  CASE TYPE((FIELD(i))) = "L"         && Logical
                     IF curfield
                        curfield = "True"
                     ELSE
                        curfield = "False"
                     ENDIF
                  CASE TYPE((FIELD(i))) = "T"         && DateTime
                     curfield = TTOC(EVAL(FIELD(i)))
                  ENDCASE
                  .Selection.Font.Reset
                  .Selection.TypeText(curfield)
               ELSE
                  .Selection.Font.Reset
                  .Selection.TypeText(curfield)
               ENDIF
               .Selection.Font.Reset
               .Selection.MoveRight(12)
            ENDFOR
            SKIP
         ENDWITH

         DO WHILE ctempfield = EVAL(FIELD(1))   && Get other like records.
            WITH oWord
               FOR i = 2 TO FCOUNT()
                  curfield = EVAL(FIELD(i))
                  IF TYPE((FIELD(i)))<>"C"
                     DO CASE
                     CASE TYPE((FIELD(i))) = "D"
                        curfield = DTOC((FIELD(i)))
                     CASE TYPE((FIELD(i))) = "N"
                        curfield = STR((FIELD(i)))
                     CASE TYPE((FIELD(i))) = "Y"
                        curfield = STR(EVAL(FIELD(i)),8,2)
                     CASE TYPE((FIELD(i))) = "L"
                        IF curfield
                           curfield = "True"
                        ELSE
                           curfield = "False"
                        ENDIF
                     CASE TYPE((FIELD(i))) = "T"
                        curfield = TTOC(EVAL(FIELD(i)))
                     ENDCASE
                     .Selection.TypeText(curfield)
                  ELSE
                     .Selection.TypeText(curfield)
                  ENDIF
                     .Selection.MoveRight(12)
                  ENDFOR
            ENDWITH
            SKIP
         ENDDO

         IF RECNO() > RECCOUNT()   && Prevents an empty table/cells.
            EXIT
         ELSE
            IF newgrouppage
               headings_added = .F.    && False: add headings to each page.
               oWord.Selection.InsertBreak(2)  && Page break each category.
            ENDIF
         ENDIF
      ENDDO

      oWord.Selection.SelectRow   && Ensures no extra rows in the table.
      oWord.Selection.Rows.Delete

      * This section underlines or turns off all lines in the table.
      nanswer = MESSAGEBOX("Turn off underlines Y/N",36,;
         "No underlines in the table?")
      DO CASE
      CASE nanswer = 6       && Yes, turn off all underlines.
         WAIT WINDOW 'Formating table with no underlines in the table.';
            NOWAIT
         WITH oWord
            For Each aTable In .ActiveDocument.Tables && Format all tables.
               aTable.Borders(-1).LineStyle = 0         && Top border.
               aTable.Borders(-2).LineStyle = 0         && Left
               aTable.Borders(-3).LineStyle = 0         && Bottom
               aTable.Borders(-4).LineStyle = 0         && Right
               aTable.Borders(-5).LineStyle = 0         && Horizontal
               aTable.Borders(-6).LineStyle = 0         && Vertical
               aTable.Borders.Shadow = 0
            ENDFOR
         ENDWITH
      CASE nanswer = 7         && Number just underlines.
         WITH oWord
            WAIT WINDOW 'Formating table with underlines between records.';
               NOWAIT
            For Each aTable In .ActiveDocument.Tables && Format each table.
               aTable.Borders(-1).LineStyle = 0         && Top border
               aTable.Borders(-2).LineStyle = 0         && Left
               aTable.Borders(-3).LineStyle = 1         && Bottom
               aTable.Borders(-4).LineStyle = 0         && Right
               aTable.Borders(-5).LineStyle = 1         && Horizontal
               aTable.Borders(-6).LineStyle = 0         && Vertical
               aTable.Borders.Shadow = 0
            ENDFOR
         ENDWITH
         oWord.ActiveWindow.View.TableGridlines = .F. && No table gridlines
      ENDCASE

   * Get the number of pages in the Word report. The code adds the report
   * headings to the document header when the user chooses not to have the
   * report categories print on separate pages. Makes viewing groups
   * headings easier on other pages.
      numpages = oWord.ActiveDocument.ComputeStatistics(2)
      IF numpages > 1 AND NOT newgrouppage
         WITH oWord
            .Selection.MoveDown
            .ActiveWindow.ActivePane.View.Type = 3 && Put Word in Page view
            .ActiveWindow.ActivePane.View.SeekView = 9      && Open header.
            .Selection.ParagraphFormat.TabStops.ClearAll   && Clear tabs.

            * Printed header width is computed by subtracting margins
            * from page width. The margins are divided by 72. Word stores
            * these values as points; i.e. 72points/inch.
            pagewidth = 8.5-(.ActiveDocument.PageSetup.RightMargin+;
            .ActiveDocument.PageSetup.LeftMargin)/72
            tabspace = (pagewidth/(numcols))*72 && Convert inches to points
            tabstops = tabspace
            FOR i = 2 TO FCOUNT()
               .Selection.Font.Italic = .T.   && Format heading captions.
               .Selection.ParagraphFormat.Alignment = 1
               .Selection.Font.Name = "Times New Roman"
               .Selection.Font.Size = 8
               .Selection.TypeText((FIELD(i)))
               .Selection.TypeText(chr(9)) && Tab to set the next heading.
               .Selection.ParagraphFormat.TabStops.Add(tabstops) && Tab
               tabstops = tabstops+tabspace
            ENDFOR
         ENDWITH
      ENDIF
      WITH oWord
         .ActiveWindow.View.Type = 3 && Switch to page view. Normal view=1
         .ActiveWindow.ActivePane.View.SeekView = 0 && Open main document.
         .Selection.Homekey(6)               && Go to top of document.
         .Visible = .T.                     && Make Word visible.
         .Application.Activate               && Bring Word forward.
         .WindowState = 0    && Show Word in normal state. Maximized=1
         .ActiveWindow.ActivePane.View.ShowAll = 0 && No nonprinting items.
      ENDWITH
   ELSE
      =MESSAGEBOX("There were no records in the query.",16,;
         "Empty Query")
   ENDIF
   */ End program code /*
				

参考

有关获取 Word 为 Windows 97 服务释放-1 的详细信息,请参阅 Microsoft 知识库中下面的文章:
172475如何获取和安装 MS Office 97 sr-1
Microsoft Word Visual Basic 帮助

(c) Microsoft Corporation 1998,保留的所有权限。 由 Dean Christopher,Microsoft 公司的贡献

属性

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

提供反馈