2016-08-29 50 views
-1

請指教如何使用宏在Excel單元格中創建對象。 請參考下面的圖片:在Excel的單元格中使用VB或宏創建excel附件對象

[Sample image

我想附上附件像的圖像,但使用腳本或任何形式的公式。

感謝

+0

使用宏記錄器,開始記錄,將圖像添加到單元格,完成後,停止它。您將找到包含相關步驟的宏,以將圖像包含在宏錄製器的「顯示宏」部分中。 – SaschaM78

回答

1

下面是我用我的評論中描述的方法創建了一個示例:宏

Excel中

'Select the cell that should contain the object 
Range("B5").Select 
'Add an object to the given cell 
ActiveSheet.OLEObjects.Add(Filename:= _ 
    "C:\Users\de12668\Documents\Zeichnung1.vsd", Link:=False, DisplayAsIcon:= _ 
    True, IconFileName:= _ 
    "C:\WINDOWS\Installer\{90140000-0057-0000-0000-0000000FF1CE}\visicon.exe", _ 
    IconIndex:=0, IconLabel:="A sample"). _ 
    Select 

更新1

如果在第一列被提供給元件的路徑,使用該添加相應的鏈接:

Dim myRange As range 
Dim longLastRow As Long 
Dim counter As Long 

Set myRange = Worksheets(1).range("A1") 
longLastRow = Cells(Rows.Count, myRange.Column).End(xlUp).Row 

For counter = 1 To longLastRow 
    range("B" & counter).Select 
    ActiveSheet.OLEObjects.Add(Filename:= _ 
    range("A" & counter).Value, Link:=False, DisplayAsIcon:= _ 
    True, IconFileName:= _ 
    range("A" & counter).Value, _ 
    IconIndex:=0, IconLabel:=""). _ 
    Select 
Next 
+0

感謝SaschaM78的快速響應,我沒有關於VB或宏的知識可以指導我多一個場景我有存儲在本地系統中的文檔鏈接列表,所以我可以使用宏爲每個鏈接創建對象請參閱圖像以供參考http://imgur.com/a/zIik1。我在A欄中有一個鏈接,我想在B欄中創建他們的附件,Kindly Suggest。 –

0

打開VBA編輯器(ALT + F11) 工具 - >引用 - >包含「Microsoft腳本運行時」
複製下面的代碼粘貼到Excel VBA
爲文檔路徑A1
運行
檢查輸出是否它適合你。

Sub CreateObject() 
Dim shpGroup As Shape 
Dim shpTextbox As Shape 

Dim fso As New FileSystemObject 
Dim mfile As File 
Dim mfolder As Folder 
Dim mpath As String 
Dim mrow As Integer 

mpath = ActiveSheet.Range("A1").Value  'Path of the document files in the local system 
mrow = 2 

If fso.FolderExists(mpath) Then 
    Set mfolder = fso.GetFolder(mpath) 
    For Each mfile In mfolder.Files 
     ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Range("A" & mrow), _ 
     Address:=mfile.ShortPath, _ 
     TextToDisplay:=mfile.ShortPath 
     ActiveSheet.Range("A" & mrow).Value = mfile.ShortPath 
     Set shpGroup = ActiveSheet.Shapes.AddPicture("C:\inetpub\wwwroot\learn\sun.jpg", msoFalse, msoTrue, 0, 0, 50, 50)  'give the Image path 
     shpGroup.LockAspectRatio = msoFalse 
     shpGroup.Left = ActiveSheet.Range("B" & mrow).Left 
     shpGroup.Top = ActiveSheet.Range("B" & mrow).Top 
     shpGroup.Width = ActiveSheet.Range("B" & mrow).Width 
     shpGroup.Height = ActiveSheet.Range("B" & mrow).Height 
     mrow = mrow + 1 
    Next 
End If 

Set mfile = Nothing 
Set mfolder = Nothing 
Set fso = Nothing 
End Sub 
相關問題