Copy the object path to cell in vba

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
I use the code to get the object. How to add code to get the full path of the object and paste it to the active cell.
VBA Code:
Sub SelectOLE()
Dim objFileDialog As Office.FileDialog

    Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFilePicker)

        objFileDialog.AllowMultiSelect = False
        objFileDialog.ButtonName = "Select File"
        objFileDialog.Title = "Select File"
        objFileDialog.Show

        If (objFileDialog.SelectedItems.Count > 0) Then
                    
        Set f = ActiveSheet.OLEObjects.Add _
            (Filename:=objFileDialog.SelectedItems(1), _
              Link:=False, _
              DisplayAsIcon:=True, _
              IconLabel:=objFileDialog.SelectedItems(1), _
              Top:=ActiveCell.Top, _
              Left:=ActiveCell.Left _
           )
        f.Select
        
        With f
       .ShapeRange.LockAspectRatio = msoFalse
       .Width = ActiveCell.Width
       .Height = ActiveCell.Height
        End With
                  
        End If
      
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi Vincent88,

code writes path and filename with extension to the next cell to the shape:

VBA Code:
Sub SelectOLE_120521()
'https://www.mrexcel.com/board/threads/copy-the-object-path-to-cell-in-vba.1170676/

  Dim objFileDialog As Object
  Dim f As Object

  Set objFileDialog = Application.FileDialog(MsoFileDialogType.msoFileDialogFilePicker)

  With objFileDialog
    .AllowMultiSelect = False
    .ButtonName = "Select File"
    .Title = "Select File"
    .Show
  End With
 
  If objFileDialog.SelectedItems.Count > 0 Then
             
    Set f = ActiveSheet.OLEObjects.Add _
        (filename:=objFileDialog.SelectedItems(1), _
          Link:=False, _
          DisplayAsIcon:=True, _
          IconLabel:=objFileDialog.SelectedItems(1), _
          Top:=ActiveCell.Top, _
          Left:=ActiveCell.Left)
   
    With f
      .ShapeRange.LockAspectRatio = msoFalse
      .Width = ActiveCell.Width
      .Height = ActiveCell.Height
    End With
    ActiveCell.Offset(0, 1).Value = objFileDialog.SelectedItems(1)
  
  End If
 
  Set f = Nothing
  Set objFileDialog = Nothing
     
End Sub
Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top