簡體   English   中英

如何在C#Excel Interop中將文本添加到現有形狀(文本框)

[英]How to add text to an existing shape(textbox) in C# Excel Interop

我有一個excel,其中包含文本框/形狀,我將用特定數據填寫。 我在識別每個形狀時使用了以下代碼:

//using Excel = Microsoft.Office.Interop.Excel;

Excel.Worksheet xlWorkSheet
foreach(Excel.Shape shp in xlworksheet.Shapes)
{
    //code to add text to shape goes here....
}

我也試過用:

shp.TextFrame2.TextRange.Characters.Text = "Test";

shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test";

但會給出一個錯誤,指出The specified value is out of rangeMember not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND)) Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))

在現有文本框中添加文本的正確方法是什么?

在設置文本之前,您必須檢查Shape的類型是否為msoTextBox

Excel.Worksheet xlWorkSheet
foreach (Excel.Shape shp in xlworksheet.Shapes)
{
    if (shp.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
    {
        shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM