0

ジレンマはかなり単純です。すべてのフォントの背景色をクリアし (テーブル セルの背景色は変更しない)、Word 文書の取り消し線付きのすべてのテキストを削除し、文書を別のフォルダーに保存する小さなアプリを作成する必要があります。それ以外の場合、ドキュメントの書式は変更されません。

以下は、Find.Execute() を使用して見つかったランダムな文字列に特定の種類の書式設定を適用する方法を示す、Google で利用可能なランダムな例から集めた大きな例です。ただし、上記の方法だけを行う方法については、手がかりがありません。

public static string searchDoc(string fileNameRef)
    {

        Microsoft.Office.Interop.Word._Application word = new Microsoft.Office.Interop.Word.Application(); ;
        Microsoft.Office.Interop.Word._Document doc = new Microsoft.Office.Interop.Word.Document();
        object missing = System.Type.Missing;

        try
        {
            System.IO.FileInfo ExecutableFileInfo =
                    new System.IO.FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);

            object fileName =
                System.IO.Path.Combine(ExecutableFileInfo.DirectoryName, fileNameRef);

            doc = word.Documents.Open(ref fileName, 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);
            doc.Activate();

            //object findStr = "hello"; //sonething to find
            // THIS is the part where I fail, I can't find of a way to Find.Execute on formatting
            // as opposed to mere strings.
            //while (word.Selection.Find.Execute(ref findStr))  //found...
            //{
            //    //change font and format of matched words
            //    word.Selection.Font.Name = "Tahoma"; //change font to Tahoma
            //    word.Selection.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdRed;  //change color to red
            //}

            object saveFileName = ExecutableFileInfo.DirectoryName + "\\New\\" + fileNameRef;

            doc.SaveAs(ref saveFileName, 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);

        }
        catch (Exception)
        {
        }
        finally
        {
            doc.Close(ref missing, ref missing, ref missing);
            word.Application.Quit(ref missing, ref missing, ref missing);
        }

        return fileNameRef;
    }

助けてくれてありがとう!とにかく、フォーマットを見つける方法を始めるだけで、大いに役立つと思います。:)

4

1 に答える 1

1

これは C# 固有の質問ではありません。これは Word オブジェクト モデルに関する質問です (ここここを参照してください)。

具体的な質問については、Word でマクロ レコーダーを有効にして、アクションを実行し、生成された VBA コードを確認することをお勧めします。その後、C# で適用できます。

これを試して:

using System;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;

namespace WordFormattingFindReplace {
    class Program {
        static void Main(string[] args) {
        }

        public static string searchDoc(string fileName) {
            _Application word = new Application(); ;
            _Document doc;

            string folderName = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string filePath = Path.Combine(folderName,fileName);

            doc = word.Documents.Open(filePath);

            var find=doc.Range().Find;
            find.Text="Hello";
            find.Format=true;
            find.Replacement.Font.Name="Tahoma";
            find.Replacement.Font.ColorIndex=WdColorIndex.wdRed;
            find.Execute(Replace:WdReplace.wdReplaceAll);

            doc.SaveAs2(Path.Combine(folderName,"New",fileName));

            doc.Close();

            //We need to cast this to _Application to resolve which Quit method is being called
            ((_Application)word.Application).Quit();

            return fileName;
        }
    }
}

いくつかのメモ:

  • using明確にするためにステートメントを使用します。の代わりにMicrosoft.Office.Interop.Word._Application word、ファイルの先頭に追加するusing Microsoft.Office.Interop.Wordと、次のように書くことができます_Application word
  • フォルダ名だけが必要な場合は、オブジェクトを作成する代わりに、静的Path.GetDirectoryNameメソッドを使用して変数として保存しますstringFileInfo
  • .NET 4 以降、Documents.Open、 、Document.SaveAsおよびを呼び出すときにオプションの引数をスキップできますDocument.Close。これは、object missing.
  • ここにはユーザーが本当に見る必要があるものは何もないので、呼び出すDocument.Activate必要はありません
  • Word.Application呼び出しごとにインスタンスを再作成するのではなく、インスタンスを再利用することをお勧めします。
于 2013-02-19T17:14:19.193 に答える