Macro to delete an Object

J

JoeP

In Excel 2003 I have a macro that inserts a pdf file into a spreadsheet. When
I run the macro it inserts the pdf file in and the object is assigned a name
Object 11; the next time I run the macro it inserts the pdf file in and the
object is assigned a name Object 12, and so on. I am fine with that part.

Now I want to record a macro to clear the object from the worksheet. The
problem is that since the object name is changing from Object 11 to Object
12. So when I go to record the macro it will delete the object.

But when I run the macro again to insert the pdf and then try to run the
macro to delete the pdf the macro fails since it cannot find the object that
I want to delete.

I guess the bottom line question is - is there a way to name an object when
inserted into a spreadsheet so that it always named Object 1?

Thanks for any help.
 
E

EricG

When you insert the PDF, you can give it a unique name. Then you will know
exactly which object to select for deletion every time. Example:

ActiveSheet.OLEObjects.Add(Filename:= _
"C:\documents\temp.pdf", Link:= _
False, DisplayAsIcon:=False).Select
Selection.Name = "My_PDF_File"

and later on...

ActiveSheet.Shapes("My_PDF_File").Select
Selection.Delete

HTH,

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top