Solved

How can I run a macro on only the first page of a Word document to delete text boxes?

Posted on 2011-02-25
4
216 Views
Last Modified: 2012-05-11
I've come across some macros on the internet to delete text boxes but the problem is that they delete text boxes for the whole document. I've tried to limit this to the current page but have had no success. I've tried a Do While but it doesn't work because the current page does not change while the macro executes. Does anyone know how to do this? I have the current macro below compliments of http://kellyjones.netfirms.com/visualbasic/textbox.shtml.

Sub Remove_ALL_TextBoxes()
Dim aShape As Shape
For Each aShape In ActiveDocument.Shapes
   If aShape.Type = msoTextBox Then
      aShape.Delete
   End If
Next
End Sub
0
Comment
Question by:Coupland2000
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
Comment Utility
Try this

Sub Sample()
    Dim aShape As shape
    For Each aShape In ActiveDocument.Shapes
        If aShape.Type = msoTextBox Then
            '~~> Delete all Shapes from Page1
            If aShape.Anchor.Information(wdActiveEndPageNumber) = 1 Then
                aShape.Delete
            End If
        End If
    Next
End Sub

Open in new window


Sid
0
 
LVL 1

Author Comment

by:Coupland2000
Comment Utility
That worked great on the first page but is there anyway to have it execute on the currently viewed page in Word? I'm just curious. For now, I've just put in a inputbox to specify the page number.
0
 
LVL 30

Assisted Solution

by:SiddharthRout
SiddharthRout earned 500 total points
Comment Utility
Yes

This code will give you the page number where the cursor is at the moment.

Selection.Information (wdActiveEndPageNumber)

So if you incorporate the same in the above code, it becomes

If aShape.Anchor.Information(wdActiveEndPageNumber) = _
Selection.Information (wdActiveEndPageNumber)

Is this what you want?

Sid
0
 
LVL 1

Author Comment

by:Coupland2000
Comment Utility
Perfect! Thanks so much!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now