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
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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 is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
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 …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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