How to write if shape exists in excel vba

aditee
aditee used Ask the Experts™
on
Hello,
I am extracting particular color text from a particular column, say Q and pasting it in powerpoint. But the text is from different cell which get pasted in different text box. So i have used macros which will convert the data into one text box. The below code i am using to merge in one text box.
'to merge the textboxes into one text box
With myPresentation.Slides(1).Shapes
'set a reference to the 'master' text box
Set myShape = .Item(1)
'copy text from text boxes to master text box
For x = 2 To .Count
If .Item(x).Type = msoTextBox Then
'insert a carriage return
myShape.TextFrame2.TextRange.InsertAfter vbCrLf
.Item(x).TextFrame2.TextRange.Copy
myShape.TextFrame2.TextRange.InsertAfter(.Item(x).TextFrame2.TextRange).PasteSpecial (msoClipboardFormatNative)
End If
Next
'delete the text boxes that have been copied(must count backwards when deleting items from collections)
For x = .Count To 2 Step -1
.Item(x).Delete
Next
End With

Open in new window


 Likewise, there is some text which too needs to be extracted from different column, say H. I have used the same code for extracting as well merging it into single text box. But it may happen that there is not any text in column H. Using below code it gives me error about the range.

 Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
  'to merge the textboxes into one text box
  With myPresentation.Slides(1).Shapes
'set a refrence to the 'master' text box
Set myShape = .Item(2)
'copy text from text boxes to master text box
For x = 3 To .Count
If .Item(x).Type = msoTextBox Then
'insert a carriage return
myShape.TextFrame2.TextRange.InsertAfter vbCrLf
.Item(x).TextFrame2.TextRange.Copy
myShape.TextFrame2.TextRange.InsertAfter(.Item(x).TextFrame2.TextRange).PasteSpecial (msoClipboardFormatNative)
End If
Next
'delete the text boxes that have been copied(must count backwards when deleting items from collections)
For x = .Count To 3 Step -1
.Item(x).Delete
Next

End With

Open in new window

So, i used the code as If shape>0 then
else
end if
It works but when there is text in column it wont merge the text boxes. I am not able to understand the problem. Please help me out. Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

generic way
Set myShape = Nothing
On Error Resume Next
Set myShape = .Item(1)
On Error Goto 0
if not myShape Is Nothing Then
' there's a shape
Else
' there's no shape
End If

Open in new window

Regards
Jamie GarrochSenior Technical Consultant at BrightCarbon
Commented:
I'm not sure I fully understand the question but if I answer the question title, then you can find out if there are any shapes on a particular slide like this:

If myPresentation.Slides(1).Shapes.Count > 0 Then

Open in new window


When posting questions, please put code in a code block using the CODE tool in the editor as it makes the question easier to read.
TracyVBA Developer

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Rgonzo1971 (https:#a42370324)
-- Jamie Garroch (https:#a42370732)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

broomee9
Experts-Exchange Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial