0

OfficeInteropとMSWord(Microsoft.Office.Interop.Word)を使用してテンプレートを変更し、テンプレート内のブックマークをテキストのセクションに置き換えています。私はこれを行う方法があります:

public void ReplaceBookmarkText(Bookmark bookmark, string newValue)
{
    if (newValue != null) {
        bookmark.Range.Text = newValue;
    }
}

これはプレーンテキストで問題なく機能します。新しいメソッドを作成したいと思います。2番目のパラメーターはHTMLコードであり、コードはフォーマットされたテキストに変換され、Rangeのテキストが置き換えられます。私が自分のやり方で物事を行うことができれば、私は次のようなものを書くでしょう:

public void ReplaceBookmarkTextWithHtml(Bookmark bookmark, string html)
{
    if (newValue != null) {
        bookmark.Range.Html = html;
    }
}

もちろん、クラスHtmlのメンバーではありません。Range私も次のことを試しました:

public void ReplaceBookmarkTextWithHtml(Bookmark bookmark, string html)
{
    if (newValue != null) {
        bookmark.Range.FormattedText = html;
    }
}

FormattedTextただし、プロパティはタイプであるため、これは機能しませんRange

これをどのように行うことができるかについてのアイデアはありますか?

4

2 に答える 2

0

これが私が最終的に思いついた解決策です。コピーと貼り付けを実行する必要があります。

public void ReplaceBookmarkTextWithHtml(Bookmark bookmark, string html)
{
    if (html != null) {
        Clipboard.SetData(DataFormats.Html, ClipboardFormatter.Html(html));
        bookmark.Range.PasteSpecial(DataType: WdPasteDataType.wdPasteHTML);
    }
}
于 2012-10-30T17:42:12.893 に答える