我已经彻底阅读了上面的答案,你张贴在下面的链接。
Programmatically Update Linked Named Range of excel object in MS Word (2007)
我面临的问题,同时更新共享驱动器路径使用相同的上述步骤。我的excel文件在一个共享驱动器文件夹中,我尝试过手动插入OLE对象,我成功了。同时使用类似的逻辑:
ActiveDocument.Bookmarks("R1").Range.InlineShapes.AddOLEObject filename:=filename _
& "!Range1", LinkToFile:=True"
它给出了以下错误:
Word无法创建指向指定对象的链接。请直接将该对象插入您的文件中,而不创建链接。
我不知道为什么会出现这个错误。如能在这方面提供任何帮助,我们将不胜感激。
注意:我正在更新一个新的范围以及文件的位置和文件名。我已经验证了excel文件中的范围是有效的。
谢谢
发布于 2018-06-28 05:05:17
由于链接已经存在,所以不应该使用.AddOLEObject。相反,您应该编辑文件路径。例如:
Dim iShp As InlineShape
Const strPath As String = "New Path"
For Each iShp In ActiveDocument.InlineShapes
With iShp
If Not .LinkFormat Is Nothing Then
With .LinkFormat
.SourceFullName = Replace(.SourceFullName, .SourcePath, strPath)
End With
With .Field
.Code.Text = Replace(.Code.Text, "5 - EW_RA!R2C17", "6 - EW_RA!R2C17")
.Update
End With
End If
End With
Next
发布于 2018-07-13 01:25:14
对于包装好的对象,使用早期绑定:
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim wdShp As Word.Shape, wdRng As Word.Range, i As Long, Fmt As Long, StrID As String, StrNm As String
Dim vRel As Long, vPos As Single, hRel As Long, hPos As Single, Hght As Single, Wdth As Single
Const strPath As String = "New Path"
With wdApp
.Visible = True
Set wdDoc = .Documents.Open(Filename:="C:\Users\" & Environ("Username") & "\Documents\Target Document.docx", _
AddToRecentFiles:=False, Visible:=True)
With wdDoc
For i = .Shapes.Count To 1 Step -1
With .Shapes(i)
If Not .LinkFormat Is Nothing Then
Set wdRng = .Anchor: StrID = .OLEFormat.progID: StrNm = "\" & .LinkFormat.SourceName
Fmt = .WrapFormat.Type: Hght = .Height: Wdth = .Width
vRel = .RelativeVerticalPosition: vPos = .Top
hRel = .RelativeHorizontalPosition: hPos = .Left
.Delete
With wdRng
.Fields.Add Range:=.Duplicate, Type:=wdFieldEmpty, PreserveFormatting:=False, _
Text:="LINK " & StrID & " " & Chr(34) & Replace(strPath & StrNm, "\", "\\") & Chr(34) & " " & _
"6 - EW_RA!R2C17" & " \p"
.End = .End + 1
Set wdShp = .Fields(1).InlineShape.ConvertToShape
End With
With wdShp
.WrapFormat.Type = Fmt: .Height = Hght: .Width = Wdth
.RelativeVerticalPosition = vRel: .Top = vPos
.RelativeHorizontalPosition = hRel: .Left = hPos
End With
End If
End With
Next
.Close True
End With
.Quit
End With
https://stackoverflow.com/questions/51074459
复制相似问题