当通过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
);
}
任何举行将不胜感激。谢谢。
可以通过多种方式将切换添加到域代码中。
在问题中显示的情况下,可以将开关添加到传递给Text
parameter的字符串中:
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
开关可能导致非常奇怪的行为。但是,我会将它用于链接表。