MS Word addin形成表和文本彼此相对

时间:2017-12-09 20:01:35

标签: c# ms-word vsto

我正在C#中编写一个Word插件,它将在文档的当前选择点添加一组文本和一个表,目前正在发生的事情是当单击该按钮时,将插入文本并插入文本但是文本将在表格内。

主要是,我需要从表格中获取文本,但理想情况下我希望文本是独立的,但是在表格旁边,所以文本与左边对齐,表格与右边对齐。

我目前所拥有的: enter image description here

我想要什么: enter image description here

我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Office = Microsoft.Office.Core;

using Word = Microsoft.Office.Interop.Word;

namespace WordAddIn1
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Microsoft.Office.Interop.Word.Range textFromDoc = Globals.ThisAddIn.Application.Selection.Range;
            textFromDoc.Font.Size = 10;
            textFromDoc.Font.Name = "Verdana";
        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {
            Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;

            if (Globals.ThisAddIn.Application.Options.Overtype)
            {
                Globals.ThisAddIn.Application.Options.Overtype = false;
            }

            object missing = System.Type.Missing;
            Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
            Word.Table newTable = Globals.ThisAddIn.Application.ActiveDocument.Tables.Add(
            currentRange, 1, 2, ref missing, ref missing);

            // Get all of the borders except for the diagonal borders.
            Word.Border[] borders = new Word.Border[6];
            borders[0] = newTable.Borders[Word.WdBorderType.wdBorderLeft];
            borders[1] = newTable.Borders[Word.WdBorderType.wdBorderRight];
            borders[2] = newTable.Borders[Word.WdBorderType.wdBorderTop];
            borders[3] = newTable.Borders[Word.WdBorderType.wdBorderBottom];
            borders[4] = newTable.Borders[Word.WdBorderType.wdBorderHorizontal];
            borders[5] = newTable.Borders[Word.WdBorderType.wdBorderVertical];

            // Format each of the borders.
            foreach (Word.Border border in borders)
            {
                border.LineStyle = Word.WdLineStyle.wdLineStyleSingle;
                border.Color = Word.WdColor.wdColorBlack;
            }

            Word.Cell cell = newTable.Cell(1, 1);
            cell.Range.Text = "Name";
            Word.Cell cell1 = newTable.Cell(1, 2);
            cell1.Range.Text = "Eman";

            if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
            {               
                //currentSelection.
                currentSelection.Font.Size = 15;
                currentSelection.Font.Name = "Arial Black";
                currentSelection.Font.Bold = 1;
                currentSelection.Font.Color = (Word.WdColor)(0 + 0x100 * 84 + 0x10000 * 164);
                currentSelection.TypeText("Test\nTest\nTest\nTest\nTest");
                currentSelection.TypeParagraph();
            }
            else
                if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
            {
                // Move to start of selection.
                if (Globals.ThisAddIn.Application.Options.ReplaceSelection)
                {
                    object direction = Word.WdCollapseDirection.wdCollapseStart;
                    currentSelection.Collapse(ref direction);
                }
                currentSelection.TypeText("Inserting before a text block. ");
                currentSelection.TypeParagraph();
            }
            else
            {
                // Do nothing.
            }

        }
    }
}

0 个答案:

没有答案