Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro to find text box within Header then select text 2

Status
Not open for further replies.

RP1America

Technical User
Aug 17, 2009
221
US
I'm looking to change the font type for the text contained within multiple text boxes in both the header and footer of many documents.

What I am stuck on is how to select the text within a text box within either the header or footer.

Any thoughts on how to do this?

The overall result is that I need the ENTIRE text content of the document to be Arial Narrow, where there are currently varying font types.

Thanks in advance!

Ryan
 
I need the ENTIRE text content of the document to be Arial Narrow
So, simply change the font of ALL styles (NO VBA required) ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PH,

I appreciate the quick response!

Unfortunately, unless I am not understanding correctly, I don't believe it is that simple. This needs to be done across 200+ documents. I have code to cycle through the documents, and even what to do once the text is selected. I seem to be having a hard time understanding how to select text within a text box that is within a header/footer.
 
Have a look at StoryRanges

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Ryan,

no need to select any text for that; you only need to access the text range within the text boxes.

This little snippet should give you an idea:
Code:
Dim tb As Shape, doc As Document, sr As Range

'part where you cycle through your files
'with each file, do this:

Set doc = Documents.Open(FullPathToYourFile)
Set sr = doc.StoryRanges(wdEvenPagesFooterStory)
For Each tb In sr.Shapes
    If tb.Type = msoTextBox Then
        tb.TextFrame.TextRange.Font.Name = "Arial Narrow"
    End If
Next tb

The other story ranges you'll need would be:

wdEvenPagesHeaderStory
wdFirstPageHeaderStory
wdPrimaryFooterStory
wdPrimaryHeaderStory

In case you do not have different footers/headers for odd/even pages and not first page different, the two last, bolded story ranges should suffice for you.

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo,

Thanks!

I am receiving a Complile error on the "Shapes" of "For Each tb In sr.Shapes" = Method or data memeber not found.

Thoughts?
 
Sorry, my bad.
Should be
Code:
For Each tb In sr.[b]ShapeRange[/b]

Just tested it. Works fine for me.


[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Here is what I have so far...

Code:
Dim tb As Shape, doc As Document, sr As Range

Dim file
Dim path As String
path = "I:\Ryan Plew\Cost Pages Originals\2Proposal - 401(a) 403(b) Cost pages\"
file = Dir(path & "*.doc")
Do While file <> ""
   Documents.Open FileName:=path & file
'''


Set sr = doc.StoryRanges(wdPrimaryHeaderStory)
For Each tb In sr.ShapeRange
    If tb.Type = msoTextBox Then
        tb.TextFrame.TextRange.Font.Name = "Arial Narrow"
    End If
Next tb


'''
   With ActiveDocument
      .Save
      .Close
   End With
   file = Dir()
Loop

On "Set sr = doc.StoryRanges(wdPrimaryHeaderStory)", I am receiving the following error:

"Object variable or With block variable not set
 
Replace this:
Documents.Open FileName:=path & file
with this
Set doc = Documents.Open(path & file)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you SO much MakeItSo and PHV!! This worked beautifully!
 
PrimaryHeaderStory worked 'beautifully' :)


For some reason, PrimaryFooterStory isn't working.

The only piece of code that modified was changing wdPrimaryHeaderStory to wdPrimaryFooterStory. I ran the code again, however this time, the font in the Footer's text boxes did not update to Arial Narrow.

Thoughts?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top