trying to create macro to insert an object where filepath is based on contents of a cell in file

jrmorton07

New Member
Joined
Jul 20, 2011
Messages
1
I am very new to code and macros but I have a spreadsheet in excel 2007 that I want to create a macros for that will insert an object (which will be a pdf file) that is located in a saved folder on my pc and will change depending on the contents of the spreadsheet.

For example, If sheet 1 has a formula that the result of the formula is the file path for the attachment I want to add in cell (H1), I want the macro to do a insert object to insert the pdf file that is found in said filepath outlined in cell (h1). Sheet 2, on the other hand, will have a different file path in cell H1 based on the next project name and I need the macro to use the filepath found in sheet 2's H1 to tell the insert object where to pick up the file to insert in sheet 2.

I tried to record and paste the cell in the filepath but it wont allow you to copy and paste since it is a formula. I have to copy and paste the values of the cell and then I can copy and paste into the filepath when inserting an object. If I do the latter, it always looks for that file that was used when I recorded the macro.

Does anyone have a code they can provide that will either allow me to edit my code to change the filepath entered on the insert object or allow me to copy the result of the formula into the insert object filepath?

I thought I could do like I've done with save as when trying to utilize a cells contents as the file path/name and use the below code but that idea doesn't work for inserting an object...

ThisFile = Range("K1").Value
ActiveWorkbook.SaveAs Filename:=ThisFile
ActiveWindow.Close


HELP???
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Code:
    Dim ws As Worksheet
    Dim strPath As String
    
    Application.ScreenUpdating = False
    
    For Each ws In Worksheets                       'Loop through each worksheet
    
        strPath = ws.Range("H1").Value              'Read path
        If Dir(strPath, vbDirectory) <> "" _
           And strPath <> "" Then                   'Validate path
           
            ws.OLEObjects.Add Filename:= _
                strPath & "\MyPDFfilename.pdf", _
                Link:=False, DisplayAsIcon:=False   'If valid, open pdf file
        End If
    Next ws
    
    Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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