使用Microsoft Interop Word创建多个表

时间:2019-01-31 14:28:17

标签: c# .net ms-word ms-office

如何找到活动的Word文档上的特定位置,并在找到该特定位置(位置为字符串)后插入多个表。

public void Createtable()
    {
        object oMissing = System.Reflection.Missing.Value;
        object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined 
                                                             bookmark */
        object oBookMark = "List of tool changer systems ";

        //Start Word and Open existing Safety device Word file document.
        Word._Application OpenWord;
        Word._Document OpenDoc;
        OpenWord = new Word.Application();
        OpenWord.Visible = true;
        OpenWord.WindowState = Word.WdWindowState.wdWindowStateMaximize;
        OpenDoc = OpenWord.Documents.Open(FilenametextBox.Text, ref oMissing, ref oMissing, ref oMissing);
        Word.Document wordDocument = OpenDoc as Word.Document;
        int tablecount = wordDocument.Tables.Count;
        wordDocument.Activate();



        int manualtablecount = 118;
        i = 0;
        for (int j = 1; j <= manualtablecount; j++)
        {
            Word.Table wTable = wordDocument.Tables[j];
            Word.Cell pCell = wTable.Cell(1, 1);
            Word.Cell pCell1 = wTable.Cell(2, 1);
            Word.Range wrdRng = wordDocument.Bookmarks.get_Item(ref oEndOfDoc).Range;

        if (pCell.Range.Text.Contains("List of tool changer systems"))
        {

          for (int iFW = 1; iFW <= 6; iFW++)
          {
            Word.Table FWTable;
            Word.Range wrdRangeFW = wordDocument.Bookmarks.get_Item(ref oBookMark).Range.Select();
            FWTable = wordDocument.Application.ActiveDocument.Tables.Add(wrdRangeFW,3, 5);

             FWTable.Range.ParagraphFormat.SpaceBefore = 3;
             FWTable.Range.Font.Size = 10;
             FWTable.Range.Font.Bold = 1;
             FWTable.Borders.Enable = 1;

              for (int r = 1; r <= 3; r++)
              {

                  if (r == 1)
                  {
                   FWTable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                      FWTable.Cell(r, 1).Range.Text = "Designation";
                      FWTable.Cell(r, 2).Range.Text = "Sensor type";
                      FWTable.Cell(r, 3).Range.Text = "Modulename";
                      FWTable.Cell(r, 4).Range.Text = "Element";
                      FWTable.Cell(r, 5).Range.Text = "OK";
                    }
                    else
                    {
                      FWTable.Cell(r, c).Range.Text = "";
                    }

                    }
               }
          }
    }

在这里,我想在打开的单词文档“工具更换器系统列表”中的可用特定字符串之后添加六个表

我尝试了可能的事情,但没有成功。
任何帮助将不胜感激!! 谢谢

0 个答案:

没有答案