6

テキストボックスのあるワード文書があります。自動検索を実行して、メイン ドキュメントで一致するものを置き換えますが、テキスト ボックスでは何も一致しません。検索と置換機能にテキスト ボックスを含めるように指示するにはどうすればよいですか。

Word.Range range = objDoc.Content;

object findtext = Field;
object findreplacement = Value;
object findwrap = WdFindWrap.wdFindContinue;
object findreplace = WdReplace.wdReplaceAll;

range.Find.Execute(findtext, missing, missing, missing, missing, missing, missing, findwrap, missing, findreplacement, findreplace);

range = objDoc.content 行を変更する必要があると思います。

4

1 に答える 1

11

少し面倒ですが、これは私にとってはうまくいきました:

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main()
        {
            const string documentLocation = @"C:\temp\Foo.docx";
            const string findText = "Foobar";
            const string replaceText = "Woo";

            FindReplace(documentLocation, findText, replaceText);
        }

        private static void FindReplace(string documentLocation, string findText, string replaceText)
        {
            var app = new Application();
            var doc = app.Documents.Open(documentLocation);

            var range = doc.Range();

            range.Find.Execute(FindText: findText, Replace: WdReplace.wdReplaceAll, ReplaceWith: replaceText);

            var shapes = doc.Shapes;

            foreach (Shape shape in shapes)
            {
                var initialText = shape.TextFrame.TextRange.Text;
                var resultingText = initialText.Replace(findText, replaceText);
                shape.TextFrame.TextRange.Text = resultingText;
            }

            doc.Save();
            doc.Close();

            Marshal.ReleaseComObject(app);
        }
    }
}

前:

前

後:

後

于 2013-01-10T12:07:50.293 に答える