我在使用 Office Interop 和 C# 3.5 开发的文字自动化程序中有一个奇怪的案例
该程序的一个任务是将 word 文档中的任何链接图像复制到不同的位置,并将这些图像的链接源重写到新位置。
现在,在一个文档中,当我检查链接文件(使用 Word 2010)时,它将图像指向类似于 images\image_file.jpg
的位置 - 所以,图像位于文档所在文件夹的子文件夹。这是完全正确的。
但是,当我的程序运行到该图像时,该图像的 LinkFormat.SourceFullName
为我提供了本地网络上的路径,例如\\net-storage\customer\001 - customername\data\images\documents\image_file.jpg
,与我期望的实际图像链接没有任何关联。
这里出了什么问题?如何在我的程序中获取正确的图像源?
编辑为sw_lasse:我确定这是一个相对路径,因为(在其他文档中)删除相对路径中的图像并更新word中的字段后,找不到图像.所以这绝对是一个相对路径。
此外,这两个路径(网络路径和相对路径)彼此没有关联。网络上的图像使用完全不同的文件夹层次结构,因此有一个 document
子文件夹,而它不存在于相对路径中。
最佳答案
我知道您正在编写 VSTO C# 代码,这是 VBA,但我相信这可以解释您看到的奇怪行为。
您不能使用:
myPicPath = Options.DefaultFilePath(wdPicturesPath)
...因为如果用户插入了来自不同文件夹的图片,则默认文件路径返回该文件夹而不是对话框中的实际设置。相反,您可以使用:
With Dialogs(wdDialogToolsOptionsFileLocations)
.Path = "PICTURE-PATH"
.Update
myPicPath = .Setting
If Not Right$(myPicPath, 1) = "\" Then
myPicPath = myPicPath + "\"
End If
MsgBox myPicPath
End With
同样,您不能使用:
myDocPath = Options.DefaultFilePath (wdDocumentsPath)
获取默认文档路径,因为这返回当前 FileOpen 路径,而不是默认文档路径!!
相反,使用:
Dim myDocPath As String
myDocPath = Dialogs(wdDialogToolsOptionsFileLocations).Setting
'Add a "\" at the end of the path, unless the setting is already followed by a "\" -
'which it will be if the setting is set to a root folder
If Not Right$(myDocPath, 1) = "\" Then
myDocPath = myDocPath + "\"
End If
MsgBox myDocPath
最近我非常擅长将 VBA 转换为 C#,如果您提供了一些不起作用的代码,我会转换此 VBA 以适应。
Ref: How to retrieve Word's default Documents path or Pictures path setting
关于c# - 链接图片源文件名与实际链接不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13340185/