PDA

View Full Version : Solved: Check for incorrect tab setting



clhare
01-21-2010, 07:47 AM
Hi all!

I am trying to create a macro that will search all document footers for specific text (there are two variations of the text that I have to search for), and wherever found, update the text and make sure the tab settings are correct based on the left/right margins in that section. There are two possible corrections for each occurrence of the text:

1) If the left/right margins in the section are both set at 1", I want to clear all tabs and set a .55" left tab and a 5.63" right tab.

2) If the left/right margins are both set at .5", I want to clear all tabs and set a .55" left tab and a 6.63" right tab.

I have the following macro that will search for the two text variations and replace them with the correct text, but I am having trouble checking the margins and resetting the tabs. I've tried to do this several different ways, but no matter what the tabs are changed in the main document and not in the footers. I only want to change the tabs in the footers.

Sub CorrectFooters()

' Declare variables
Dim rngPane As Range
Dim intSection As Integer
Dim strSetup As String
Dim intSecCount As Integer
Dim intHFType As Integer
intSecCount = ActiveDocument.Sections.Count
For intSection = 1 To intSecCount
With ActiveDocument.Sections(intSection)
' Check section margins
If .PageSetup.LeftMargin = InchesToPoints(1) Then
strSetup = "Type1"
ElseIf .PageSetup.LeftMargin = InchesToPoints(0.5) Then
strSetup = "Type2"
End If
' Replace footer text in each footer type
For intHFType = 1 To 3
Set rngPane = .Footers(intHFType).Range
With rngPane.Find
' Search #1
.ClearFormatting
.Replacement.ClearFormatting
.Execute FindText:="987654321 A54321 112233abc ^?^?^?^?", Forward:=True, _
Wrap:=wdFindContinue, Format:=False, ReplaceWith:="123456 54321 112233abc [kit#] [class#]", _
Replace:=wdReplaceAll

' Search #2
.ClearFormatting
.Replacement.ClearFormatting
.Execute FindText:="987654321 A54321 112233abc", Forward:=True, _
Wrap:=wdFindContinue, Format:=False, ReplaceWith:="123456 54321 112233abc [kit#] [class#]", _
Replace:=wdReplaceAll
' Correct tab settings based on section margins
Selection.WholeStory
Selection.ParagraphFormat.TabStops.ClearAll
If strSetup = "Type1" Then
ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.55), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(5.63), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
Selection.MoveLeft Unit:=wdCharacter, Count:=1
ElseIf strSetup = "Type2" Then
ActiveDocument.DefaultTabStop = InchesToPoints(0.5)
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.55), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(6.63), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End If
End With
.Footers(intHFType).Range.Borders(wdBorderTop).LineStyle = wdLineStyleNone
Next intHFType
End With
Next intSection

End Sub

Thanks for your help!

fumei
01-21-2010, 11:29 AM
You are using:
Selection.ParagraphFormat.TabStops.
etc. etc.


Simply put...Selection is not IN the footers.

Either put the Selection in the footers, or use the range object you have already (better).
rngPane.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(0.55), _
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
rngPane.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(5.63), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
rngPane is an odd name though.

Why are you doing:
Selection.MoveLeft Unit:=wdCharacter, Count:=1
after your TabStops instructions? You do not do anything afterwards. It seems a bit pointless.