0

在通过 Interop 将字段添加到现有 Word 文档时,我们需要设置“数据未与文档一起存储”标志 (“\d”),但不知道该怎么做。

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

            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
                    );

            }

任何持有将不胜感激。谢谢。

4

1 回答 1

0

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

在问题中提出的情况下,可以将开关添加到传递给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当域代码使用其他(格式化的、字符串)内容更新时,该开关可能会导致非常奇怪的行为。但是,我会将它用于链接表。

于 2019-02-25T11:50:21.103 回答