在 Excel 中嵌入 OLEObject (Word/XLS/PDF doc) 时,如何设置图标相对于附件按钮的位置?
When embedding an OLEObject (Word/XLS/PDF doc) in Excel, how can I set the icon's position relative to the attachment button?
我正在尝试创建一个允许我附加 Word/Excel/PDF 文件的按钮。我已经阅读了很多其他文章和教程,所以我现在有了按钮和一个 VBA 宏,它为我提供了一个对话框来浏览我选择的文件。可以选择文件并将其嵌入 Excel 中。
我遇到的问题是让嵌入文件的位置位于我创建的按钮旁边。目前,它始终默认位于活动 sheet 的左上角,尽管我已尽最大努力硬编码不同的位置。
所以两个问题:
- 如何设置 OLEObject 的位置?
- 有没有办法识别命令按钮的单元格 reference/position,然后设置 OLEObject 相对于它的位置?例如,按钮右侧的两列。
谢谢
到目前为止,这是我的代码:
Sub AttachFile()
'Identify the cell the command button is in and set the location for attachment icon to be 3 columns to the right
Dim buttonName As String
Dim buttonAddress As String
Dim buttonLocation As Range
Dim iconLocation As Range
buttonName = ActiveSheet.Shapes(Application.Caller).Name
buttonAddress = ActiveSheet.Shapes(buttonName).TopLeftCell.Address
Set iconLocation = Range(buttonAddress).Offset(0, 3)
'Browse for the file
Dim vFile As Variant
vFile = Application.GetOpenFilename("All Files,*.*", Title:="Find file to insert")
If LCase(vFile) = "false" Then Exit Sub
'Embed the selected file
Dim attachment As OLEObject
Set attachment = ActiveSheet.OLEObjects.Add( _
Filename:=vFile, _
Link:=False, _
DisplayAsIcon:=True)
'Reposition the icon to be next to the command button
ActiveWindow.Zoom = 100
With attachment
.Top = iconLocation.Top
.Left = iconLocation.Left
End With
ActiveWindow.Zoom = 70
End Sub
我认为你的做法是正确的。尝试将您的代码更改为类似这样的内容
With attachment
.Top = cells("H89").top
.Left = cells("H89").left
End With
我目前不在 VBA 附近,但情况类似
我正在尝试创建一个允许我附加 Word/Excel/PDF 文件的按钮。我已经阅读了很多其他文章和教程,所以我现在有了按钮和一个 VBA 宏,它为我提供了一个对话框来浏览我选择的文件。可以选择文件并将其嵌入 Excel 中。
我遇到的问题是让嵌入文件的位置位于我创建的按钮旁边。目前,它始终默认位于活动 sheet 的左上角,尽管我已尽最大努力硬编码不同的位置。
所以两个问题:
- 如何设置 OLEObject 的位置?
- 有没有办法识别命令按钮的单元格 reference/position,然后设置 OLEObject 相对于它的位置?例如,按钮右侧的两列。
谢谢
到目前为止,这是我的代码:
Sub AttachFile()
'Identify the cell the command button is in and set the location for attachment icon to be 3 columns to the right
Dim buttonName As String
Dim buttonAddress As String
Dim buttonLocation As Range
Dim iconLocation As Range
buttonName = ActiveSheet.Shapes(Application.Caller).Name
buttonAddress = ActiveSheet.Shapes(buttonName).TopLeftCell.Address
Set iconLocation = Range(buttonAddress).Offset(0, 3)
'Browse for the file
Dim vFile As Variant
vFile = Application.GetOpenFilename("All Files,*.*", Title:="Find file to insert")
If LCase(vFile) = "false" Then Exit Sub
'Embed the selected file
Dim attachment As OLEObject
Set attachment = ActiveSheet.OLEObjects.Add( _
Filename:=vFile, _
Link:=False, _
DisplayAsIcon:=True)
'Reposition the icon to be next to the command button
ActiveWindow.Zoom = 100
With attachment
.Top = iconLocation.Top
.Left = iconLocation.Left
End With
ActiveWindow.Zoom = 70
End Sub
我认为你的做法是正确的。尝试将您的代码更改为类似这样的内容
With attachment
.Top = cells("H89").top
.Left = cells("H89").left
End With
我目前不在 VBA 附近,但情况类似