AddOLEObject不接受我的文件名变体。

huangapple go评论51阅读模式
英文:

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是相同类型,不需要硬编码。

我尝试过通过运行TrimCStr来检查字符串,但没有成功。

我做错了什么,或者还能做什么来缩小问题范围?

英文:

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

huangapple
  • 本文由 发表于 2023年6月16日 04:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定