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
219 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

828 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