2

我想使用 C# 将一些文本写入现有的单词文本框。实际上什么也没发生,我不知道为什么。也许有人可以帮助我?

我的方法:

Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

void SearchTextBox(Microsoft.Office.Interop.Word.Document oDoc, string name, string newContent)
{
    foreach (Microsoft.Office.Interop.Word.Shape shape in oDoc.Shapes)
    {
        if (shape.Name == name)
        {
            shape.TextFrame.TextRange.Text = newContent;
            return;
        }
    }
}

称呼:

SearchTextBox(oWordDoc, "tbTest", "Please write some Text");

字文件

4

1 回答 1

1

这里的解决方案:

using Word = Microsoft.Office.Interop.Word

var WORD = new Word.Application();
Word.Document doc   = WORD.Documents.Open(@PATH_APP);
doc.Activate();

foreach (Microsoft.Office.Interop.Word.Shape shape in WORD.ActiveDocument.Shapes)
{
    if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
    {
        if (shape.AlternativeText.Contains("MY_FIELD_TO_OVERWRITE_OO1"))
        {       
             shape.TextFrame.TextRange.Text="MY_NEW_FIELD_VALUE";
        }

    }
}

doc.Save();
于 2019-03-26T07:16:21.127 回答