将域代码添加到 INCLUDEIMAGE 域

Add Field Codes to INCLUDEIMAGE field

在通过 Interop 向现有 Word 文档添加字段时,我们需要设置 "Data not stored with document" 标志 ("\d"),但不知道如何操作。

此示例在插入图像方面效果很好 link 但它将图像存储在文档中而不是远程存储(我们需要)。

            if (doc.Bookmarks.Exists("TrackingPixel"))
            {
                object oBookMark = "TrackingPixel";
                object newText = @"https://www.remotelocation.com/trackingpixel/secretcode";

                Range rng = doc.Bookmarks.get_Item(ref oBookMark).Range;
                rng.Select();

                rng.Fields.Add(
                    Range: rng,
                    Type: WdFieldType.wdFieldIncludePicture,
                    Text: newText,
                    PreserveFormatting: true
                    );

            }

任何人都将不胜感激。 谢谢

可以通过多种方式向域代码添加开关。

在问题中出现的情况下,可以将开关添加到传递给 Text 参数的字符串中:

    if (doc.Bookmarks.Exists("TrackingPixel"))
    {
        string fieldSwitches = " \d";
        object oBookMark = "TrackingPixel";
        object newText = @"https://www.remotelocation.com/trackingpixel/secretcode" + fieldSwitches;

        Range rng = doc.Bookmarks.get_Item(ref oBookMark).Range;
        // There's usually no need to select the Range unless the user should work with it
        //rng.Select();

        rng.Fields.Add(
            Range: rng,
            Type: WdFieldType.wdFieldIncludePicture,
            Text: newText,
            PreserveFormatting: true
            );
    }

如果这是一个现有的域代码(应该在事实之后添加开关),则可以为该字符串分配新的内容。例如:

string fldCode = field.Code.Text;
fldCode += " \d";
field.Code.Text = fldCode;
field.Update();

FWIW 添加字段时,我经常将整个字段代码作为字符串传递(仅使用 Text 参数)并省略 Type 参数。我还将 PreserveFormatting 设置为 false 除非我知道我明确想要这种行为。首先是个人喜好。其次,\* MergeFormat 开关会在字段代码更新为其他(格式化的字符串)内容时导致非常奇怪的行为。但是,我会将它用于链接表。