【问题标题】:Add Field Codes to INCLUDEIMAGE field将域代码添加到 INCLUDEIMAGE 域
【发布时间】:2019-02-24 22:57:55
【问题描述】:

在通过 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
                    );

            }

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

【问题讨论】:

    标签: c# ms-word office-interop word-field


    【解决方案1】:

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

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

    【讨论】:

    • 完美 - 感谢您的帮助!我已经尝试过第一个选项,但它似乎不起作用,可能是我的错误。你的第二个是正确的。
    • @xnetdude 可能,您没有考虑在切换之前/切换之间包含必要的空间?输入答案时我几乎没有想到...
    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多