Skip to main content
 首页 » 编程设计

c#之如何使用 Microsoft.Office.Interop.Word 在某些 Word 文档中查找和修改文本框

2024年02月23日26shihaiming

我必须通过 TextBox id 访问和更改某些 word 文档的某些文本框的文本。 例如,假设我在一个 word 文档中有一些名为 txtDate 的对象,我想将其内容更改为现在的日期,那么我如何通过它的 ID 获取这个对象?

请您参考如下方法:

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