Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
222 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 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

715 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