Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
226 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 2000 total points
ID: 34986183
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
ID: 34988676
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 2000 total points
ID: 34989954
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
ID: 34996743
Perfect! Thanks so much!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
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 …

885 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