【问题标题】:Embedding PDF into Excel - three questions将PDF嵌入Excel——三个问题
【发布时间】:2019-01-15 02:23:12
【问题描述】:

我编写了一个宏来允许将 PDF 文件嵌入到 Excel 工作表中。我希望文件的图标显示在某个单元格中。到目前为止,一切都很好,但我确实有一些问题。

但首先,我的文件嵌入代码:

Range("AN5").Select

ActiveSheet.OLEObjects.Add(ClassType:="AcroExch.Document.DC", Link:=False, _
    DisplayAsIcon:=True, _
    IconIndex:=0, IconLabel:=NameForPDFIcon, _
    IconFileName:="C:\WINDOWS\Installer\{AC76BA86-7AD7-1033-7B44-AC0F074E4100}\PDFFile_8.ico").Activate

我的问题:

  1. 我希望图标位于单元格 AN5 的中心,但它却在顶部和左侧对齐。如果我在 OLEObjects.Add 行中添加顶部和左侧数字,它会将图标移动到单元格 A1 中。我不知道如何相对于我选择的单元格使用 Top & Left。

  2. 只要嵌入文件,PDF 就会在 Adob​​e Reader 中打开。有没有办法让它不打开?

  3. IconFileName 部分包含很长的字母和数字字符串。 (我是通过录制将 PDF 插入工作表的宏而得到的。)它看起来像一个注册表地址。如果我在另一台计算机上使用此文件,图标会在那里显示吗?我假设这长串字符在另一台计算机上会有所不同?任何人都知道我怎样才能做到这一点,无论它在什么计算机上运行,​​它都能正常工作?我计划在完成后将此文件发送给其他几个人。

【问题讨论】:

    标签: excel vba pdf embed


    【解决方案1】:

    基于https://stackoverflow.com/questions/32235897/how-to-embed-documents-using-vba

    Declare Function FindExecutable Lib "shell32.dll" _
         Alias "FindExecutableA" (ByVal lpFile As String, _
                                  ByVal lpDirectory As String, _
                                  ByVal lpResult As String) As Long
    
    
    Sub tester()
        AddFile ActiveSheet.Range("H10"), "C:\Users\jblow\Desktop\Training.pdf"
        AddFile ActiveSheet.Range("H20"), "C:\Users\jblow\Desktop\Info.xlsm"
    End Sub
    
    Sub AddFile(c As Range, sFile As String)
    
        Dim exe As String, o
    
        exe = FindApp(sFile)
    
        Set o = c.Worksheet.OLEObjects.Add(Link:=False, DisplayAsIcon:=True, _
                IconFileName:=exe, IconIndex:=0, _
                IconLabel:="Testing", Top:=c.Top, Left:=c.Left, _
                Filename:=sFile)
    
        o.ShapeRange.Width = c.Width '<< fit to cell
    
    End Sub
    
    Function FindApp(sFile As String) As String
       Const MAX_FILENAME_LEN = 260
       Dim i As Integer, s2            As String
    
       'Check if the file exists
       If Dir(sFile) = "" Or sFile = "" Then
          MsgBox "File not found!", vbCritical
          Exit Function
       End If
       'Create a buffer
       s2 = String(MAX_FILENAME_LEN, 32)
       'Retrieve the name and handle of the executable, associated with this file
       i = FindExecutable(sFile, vbNullString, s2)
       If i > 32 Then
          FindApp = Left$(s2, InStr(s2, Chr$(0)) - 1)
       Else
          MsgBox "No association found !"
       End If
    End Function
    

    【讨论】:

    • 谢谢。通过一点点调整,我能够完成这项工作。我确实希望我的工作簿可以在 Windows 和 Mac 上运行,所以知道如何让它在 Mac 上运行吗?我假设顶部的声明函数在 Mac 上不起作用?
    • 对不起,我不知道你会如何在 Mac 上做同样的事情。
    【解决方案2】:
    1. 来自OLEObjects.Add Method (Excel),要使用Left 参数,'新对象的初始坐标,以磅为单位,相对于工作表上单元格A1 的左上角或上-图表的左角。'因此,如果您希望图标位于 AN5 的中间,则必须将 A:AM 的列宽和 1:4 的行高相加,然后加上大约一半的列宽AN 列。您可以尝试丢弃 Select 并使用 Range("AN5").OLEObjects.Add ... 这应该使 OLEObjects 将 AN5 视为相对 A1。

    2. 插入时不要激活对象。

    3. 安装了您的 Adob​​e 版本的任何人都应该使用该 GUID。

    【讨论】:

    • 谢谢。这些建议对我很有帮助!
    猜你喜欢
    • 2014-11-05
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2016-03-22
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多