ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro to delete an Object (https://www.excelbanter.com/excel-programming/433066-macro-delete-object.html)

JoeP

Macro to delete an Object
 
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.

EricG

Macro to delete an Object
 
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


"JoeP" wrote:

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.



All times are GMT +1. The time now is 07:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com