aiter Posted September 8, 2016 Posted September 8, 2016 (edited) In excel I am trying to delete shapes within a range. Shapes can be pictures, ole objects etc. I have got the range successfully selected. Now the problem is I cannot succeed in getting to these shapes within that selection. I have tried $oExcel = _Excel_Open() ; derive NewRange . . . $NewRange.Select ; now select that range ; now try to get to the shape selection within that range and delete all the shapes $ShapeRange = $oExcel.Selection.Shapes.SelectAll $ShapeRange.delete The problem seems to the shapes is not a valid reference after selection. Desperate for help. Edited September 8, 2016 by aiter spacing
water Posted September 8, 2016 Posted September 8, 2016 Start the macro recorder in Excel, select/delete the shapes and then post the resulting VBA macro. Should then be easy to translate to AutoIt. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
aiter Posted September 8, 2016 Author Posted September 8, 2016 The problem is I do not want to select a specific shape, I want to delete all the shapes whatever they in the range I have selected. if I could iterate through the shapes within selection and delete them one by one, it would help. I have tried the macro to select one shape and this is what I get ActiveSheet.Shapes.Range(Array("Picture 1")).Select Selection.Delete Problem is I do not know if the shape is a picture.
aiter Posted September 8, 2016 Author Posted September 8, 2016 I suspect that shapes is not selectable within a range. I need to select all the shapes within the active sheet, get the cell range for the shape, see if it intersects the range I have selected then delete it. VBA is not easily translatable in autoit, at least for me.
water Posted September 8, 2016 Posted September 8, 2016 $oShapeRange = $oExcel.Selection.ShapeRange For $oShape In $oShapeRange $oShape.Delete Next My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
aiter Posted September 8, 2016 Author Posted September 8, 2016 "C:\AutoIt\RewDocMerge\aiplay2.au3" (71) : ==> The requested action with this object has failed.: $oShapeRange = $oExcel.Selection.ShapeRange $oShapeRange = $oExcel.Selection^ ERROR So close.
water Posted September 8, 2016 Posted September 8, 2016 This link describes how it works the other way round. Checks all shapes and if they intersect with your range then delete them: https://www.experts-exchange.com/questions/27395289/Excel-VBA-Delete-Shape-Objects-Within-a-Cell-Range.html My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
aiter Posted September 8, 2016 Author Posted September 8, 2016 I found this page, but to translate the interesect part into autoit is going to be a challenge for me For Each s In ActiveSheet.Shapes If Not Intersect(Range("B9:I25"), s.TopLeftCell) Is Nothing And _ Not Intersect(Range("B9:I25"), s.BottomRightCell) Is Nothing Then s.Delete End If Next s Thanks for all your help so far.
water Posted September 8, 2016 Posted September 8, 2016 First thing you need to do is decide how to handle shapes which are only partly within the selected shape. Do you want to ignore them or should they be deleted as well? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
aiter Posted September 8, 2016 Author Posted September 8, 2016 (edited) To give you some background on what I am doing :- A variable merge which allows switches to be defined in the spreadsheet eg {{switch-1}} Text, pictures, anything {{/switch-1}} Now, when I find the switch I evaluate whether it is true or false. If false, it must delete everything within the switch bracketing. If true, it must leave it (removing the switch references). This allows logos to be included/excluded amongst other things. Edited September 8, 2016 by aiter
aiter Posted September 8, 2016 Author Posted September 8, 2016 Success! $NewRange.Select ; this is the switch bracketing selection For $s In $oExcel.ActiveSheet.Shapes $b = $s.TopLeftCell $a = $oExcel.Intersect($NewRange,$b) if IsObj($a) then $s.Delete EndIf Next
water Posted September 8, 2016 Posted September 8, 2016 I fear this is only part of the solution. If the shape only intersects with the upper right, lower left or lower right side with the range then the code does not work. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
aiter Posted September 8, 2016 Author Posted September 8, 2016 It is working for what I need. See screenshot before and after
water Posted September 8, 2016 Posted September 8, 2016 Fine My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now