将 PDF 嵌入 Excel - 三个问题
Embedding PDF into Excel - three questions
我已经编写了一个宏来允许将 PDF 文件嵌入到 Excel sheet 中。我希望文件的图标显示在某个单元格中。到目前为止一切顺利,但我确实有一些问题。
但首先,我的嵌入文件的代码:
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
我的问题:
我希望图标位于单元格 AN5 的中心,但它在顶部和左侧对齐。如果我在 OLEObjects.Add 行中添加 Top 和 Left 数字,它会将图标移动到单元格 A1 中。我不知道如何相对于我选择的单元格使用顶部和左侧。
只要嵌入文件,PDF 就会在 Adobe Reader 中打开。有没有办法让它不打开?
IconFileName 部分有一串很长的字母和数字。 (我通过录制我将 PDF 插入 sheet 的宏得到了这个。)它看起来像一个注册表地址。如果我在另一台电脑上使用这个文件,图标会在那里显示吗?我假设这一长串字符在另一台计算机上会有所不同?任何人都知道我如何做到这一点,所以无论 运行 在哪台计算机上都能正常工作?我计划在完成后将此文件发送给其他几个人。
基于
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
来自OLEObjects.Add Method (Excel),要使用Left参数,'The initial coordinates of the new object, in points, relative to the upper-left corner of cell A1 on a worksheet, or to the upper-left corner of a chart.'所以如果你想要AN5中间的图标,您必须将 A:AM 的列宽和 1:4 的行高相加,然后加上大约 AN 列宽度的一半。您可以尝试丢弃 Select 并使用 Range("AN5").OLEObjects.Add ...这应该使 OLEObjects 将 AN5 视为相对 A1.
插入时不激活对象。
安装了您的 Adobe 版本的任何人都应该知道 GUID。
我已经编写了一个宏来允许将 PDF 文件嵌入到 Excel sheet 中。我希望文件的图标显示在某个单元格中。到目前为止一切顺利,但我确实有一些问题。
但首先,我的嵌入文件的代码:
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
我的问题:
我希望图标位于单元格 AN5 的中心,但它在顶部和左侧对齐。如果我在 OLEObjects.Add 行中添加 Top 和 Left 数字,它会将图标移动到单元格 A1 中。我不知道如何相对于我选择的单元格使用顶部和左侧。
只要嵌入文件,PDF 就会在 Adobe Reader 中打开。有没有办法让它不打开?
IconFileName 部分有一串很长的字母和数字。 (我通过录制我将 PDF 插入 sheet 的宏得到了这个。)它看起来像一个注册表地址。如果我在另一台电脑上使用这个文件,图标会在那里显示吗?我假设这一长串字符在另一台计算机上会有所不同?任何人都知道我如何做到这一点,所以无论 运行 在哪台计算机上都能正常工作?我计划在完成后将此文件发送给其他几个人。
基于
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
来自OLEObjects.Add Method (Excel),要使用Left参数,'The initial coordinates of the new object, in points, relative to the upper-left corner of cell A1 on a worksheet, or to the upper-left corner of a chart.'所以如果你想要AN5中间的图标,您必须将 A:AM 的列宽和 1:4 的行高相加,然后加上大约 AN 列宽度的一半。您可以尝试丢弃 Select 并使用 Range("AN5").OLEObjects.Add ...这应该使 OLEObjects 将 AN5 视为相对 A1.
插入时不激活对象。
安装了您的 Adobe 版本的任何人都应该知道 GUID。