简体   繁体   English

用单词表替换Word文档中的字符串标记

[英]Replace A string token in Word document with a word table

I am working with Microsoft.Office.Interop.Word.Application. 我正在使用Microsoft.Office.Interop.Word.Application。 I have some templated word document in which file there will be some Token like [PutfirstTableHere] At the runtime i will create Table and want's to replace that token in the existing word document with the generated Table Can any body let me know how can i replace a string token with Table in the Word? 我有一些模板化的Word文档,其中的文件中会包含一些[Token],例如[PutfirstTableHere]。在运行时,我将创建Table,并希望用生成的Table替换现有Word文档中的该令牌。任何机构都可以让我知道如何替换Word中带有表的字符串令牌? Not able to find any example/sample for my current problem 找不到我当前问题的任何示例/样本

Try this: 尝试这个:

protected void InsertTableAtBookMark(string[][] docEnds, string bookmarkName)
    {
        Object oBookMarkName = bookmarkName;
        Range wRng = WordDoc.Bookmarks.get_Item(ref oBookMarkName).Range;

        Table wTable = WordDoc.Tables.Add(wRng, docEnds.Length, docEnds[0].Length);
        wTable.set_Style("Table Grid");

        for (int i = 0; i < docEnds.Length; i++)
        {
            for (int j = 0; j < docEnds[0].Length; j++)
            {
                wTable.Cell(i, j).Range.Text = docEnds[i][j];
                wTable.Cell(1, 1).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                wTable.Cell(1, 1).VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            }
        }
        Borders wb = wTable.Borders;
        wb[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleNone;
        wb[WdBorderType.wdBorderVertical].LineStyle = WdLineStyle.wdLineStyleNone;

        wTable.Borders = wb;
    }

Where WordDoc is of type Document from Microsoft.Office.Interop.Word namespace; 其中WordDoc是Microsoft.Office.Interop.Word命名空间中Document的类型;

You can search document content using Find Interface and its Execute method. 您可以使用“ 查找接口”及其Execute方法搜索文档内容。 First argument is text to search in range (in your case i would recommend Word.Document.Content property) from which find object was created. 第一个参数是要在其中创建查找对象的范围内搜索的文本(在您的情况下,我建议使用Word.Document.Content属性)。

Code: 码:

Word.Document doc = Application.ActiveDocument;
Word.Range wholeDoc = doc.Content;                

Word.Find find = wholeDoc.Find;

object token = "[MyTableToken]";                
object missing = Type.Missing;

bool result = find.Execute(ref token, true, true, ref missing, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

while (result)
{
    // wholeDoc object is replaced with executed search/find result
    CreateTable(wholeDoc.Duplicate);

    result = find.Execute(ref token, true, true, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}

sample create table method: 示例创建表方法:

private void CreateTable(Word.Range range)
{
    Word.Tables tables = null;
    try
    {
        int sampleRowNumber = 3, sampleColumnNumber = 3;

        range.Text = "";
        tables = range.Tables;

        tables.Add(range, sampleRowNumber, sampleColumnNumber);
    }
    finally
    {
        Marshal.ReleaseComObject(range);
        Marshal.ReleaseComObject(tables);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM