簡體   English   中英

microsoft.office.interop.word 替換形狀內的表格文本

[英]microsoft.office.interop.word replace table text inside shape

我正在使用 microsoft.office.interop.word 來替換(表格單元格文本)並且我保持這個表格的形狀,我試圖替換這個表格里面的文本但是替換時的問題是表格格式正在刪除,行正在合並,如下圖所示。 請我需要以某種方式修復我的代碼以在替換文本后保持格式

原文件在此處輸入圖像描述

output 文件在此處輸入圖像描述

我正在使用這段代碼:

    private void FindAndReplace(Word.Application wordApp, object ToFindText, object replaceWithText)
    {

        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundLike = false;
        object nmatchAllforms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiactitics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        int x = 0;
        var shapes = myWordDoc.Shapes;
        foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
        {
            if (shape.TextFrame.HasText != 0)
            {
                var initialText = shape.TextFrame.TextRange.Text;
                var resultingText = initialText.Replace(ToFindText.ToString(), replaceWithText.ToString());
                if (initialText != resultingText)
                {
                    shape.TextFrame.TextRange.Text = resultingText;
                }
            }
        }
        wordApp.Selection.Find.Execute(ref ToFindText,
           ref matchCase, ref matchWholeWord,
           ref matchWildCards, ref matchSoundLike,
           ref nmatchAllforms, ref forward,
           ref wrap, ref format, ref replaceWithText,
           ref replace, ref matchKashida,
           ref matchDiactitics, ref matchAlefHamza,
           ref matchControl);
    }
    private void CreateWordDocument(object filename, object SaveAs)
    {
        try
        {

            Word.Application wordApp = new Word.Application();
            object missing = Missing.Value;
            if (File.Exists((string)filename))
            {
                object readOnly = false;
                object isVisible = false;
                wordApp.Visible = false;
                myWordDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing, ref missing);

                myWordDoc.Activate();

                wordApp.Application.Documents.Open(filename, ReadOnly: true, Visible: false);


                DataTable dtfees = DB.GetData("select fees from fees where feesid='" + 1 + "'");
                if (dtfees.Rows.Count > 1)
                {
                    string fees1 = dtfees.Rows[0][0].ToString();
                    string fees2 = dtfees.Rows[1][0].ToString();
                    string fees3 = dtfees.Rows[2][0].ToString();
                    string fees4 = dtfees.Rows[3][0].ToString();
                    this.FindAndReplace(wordApp, "tyfes1", fees1);
                    this.FindAndReplace(wordApp, "Tyfes2", fees2);
                    this.FindAndReplace(wordApp, "Tyfes3", fees3);


                }
                myWordDoc.SaveAs2(ref SaveAs, ref missing, ref missing, ref missing,
                  ref missing, ref missing, ref missing,
                  ref missing, ref missing, ref missing,
                  ref missing, ref missing, ref missing,
                  ref missing, ref missing, ref missing);

                myWordDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
            }
        }
        catch (Exception ex)
        {

        }


    }

您的代碼的主要錯誤是,無論您的形狀中有什么材料(文本/表格等),如果其中包含 findText 並且 erpelacment 文本與查找文本不同,則形狀的 TexTObject 的全部內容將被替換通過一個沒有表格的字符串,在這一行

shape.TextFrame.TextRange.Text = resultingText;

從仍然在您的代碼中的 Find.Execute 來看,我猜您嘗試使用 Find object 進行替換,但無法弄清楚如何讓它僅在 Shapes 中工作。

如果我理解您在做什么,我認為以下相當簡單的更改會奏效,但您需要檢查 C# 並進行測試。

foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
        {
            if (shape.TextFrame.HasText != 0)
            {
                shape.TextFrame.TextRange.Find.Execute(ref ToFindText,
                    ref matchCase, ref matchWholeWord,
                    ref matchWildCards, ref matchSoundLike,
                    ref nmatchAllforms, ref forward,
                    ref wrap, ref format, ref replaceWithText,
                    ref replace, ref matchKashida,
                    ref matchDiactitics, ref matchAlefHamza,
                    ref matchControl);
            }
        }

暫無
暫無

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

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