英文:
AddOLEObject does not accept my filename variant
问题
以下是您的VBA子例程的翻译:
Private Sub InsertPDF(fullFileName, displayName)
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.DC", _
FileName:=fullFileName, _
LinkToFile:=False, _
DisplayAsIcon:=True, _
IconFileName:="C:\Windows\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _
IconIndex:=0, _
IconLabel:=displayName
End Sub
当我运行它时,它会显示错误消息:“Word 无法获取 <file> 链接的数据”。
在调试时,局部窗口显示具有正确路径的变量,可以通过直接从那里复制并将fullFileName
硬编码到程序中的设置值来证明,例如“C:\users\user\file.pdf”,代码就可以运行。 类型显示为“Variant/String”,我认为这是正确的。 变量displayName
是相同类型,不需要硬编码。
我尝试过通过运行Trim
和CStr
来检查字符串,但没有成功。
我做错了什么,或者还能做什么来缩小问题范围?
英文:
I have the following VBA subroutine in a word document:
Private Sub InsertPDF(fullFileName, displayName)
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.DC", _
FileName:=fullFileName, _
LinkToFile:=False, _
DisplayAsIcon:=True, _
IconFileName:="C:\Windows\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _
IconIndex:=0, _
IconLabel:=displayName
End Sub
When I run it, it errors with the message: "word cannot obtain the data for the <file> link".
While debugging the locals window show the variable with the correct path, which is evidenced by copying it directly from there and hard-coding fullFileName
in the routine to the value set, e.g. "C:\users\user\file.pdf" the code works. The Type shows as "Variant/String", which I believe it correct. The variable displayName
is the same type and it does not need to be hard-coded.
I have tried to sanity check the string by running it through Trim and CStr without success.
What am I doing wrong, or what else can I do to narrow down the problem?
答案1
得分: 0
There was a trailing CRLF at the end of the string. I normalised the string on input like so; note the additional line at the top of the sub:
有一个字符串末尾有一个尾随的CRLF。我在输入时对字符串进行了规范化,如下所示;请注意子程序顶部的附加行:
Private Sub InsertPDF(fullFileName, displayName)
fullFileName = Replace(fullFileName, vbCrLf, "")
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.DC", _
FileName:=fullFileName, _
LinkToFile:=False, _
DisplayAsIcon:=True, _
IconFileName:="C:\Windows\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _
IconIndex:=0, _
IconLabel:=displayName
End Sub
英文:
There was a trailing CRLF at the end of the string. I normalised the string on input like so; note the additional line at the top of the sub:
Private Sub InsertPDF(fullFileName, displayName)
fullFileName = Replace(fullFileName, vbCrLf, "")
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.DC", _
FileName:=fullFileName, _
LinkToFile:=False, _
DisplayAsIcon:=True, _
IconFileName:="C:\Windows\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico", _
IconIndex:=0, _
IconLabel:=displayName
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论