If Then Else question

L

LEU

I have the following macro looks for the word ‘NOTE:’ and put’s it into a
table. I would like to modify so that if it comes across the word ‘NOTE:’
that is already in a table, it will resize the table. I know I need to write
it to say: IF ‘WORD:’ is not in a table THEN do the following ELSE do the
following. I have tried a couple of different ways but can’t get it to work.

On Error GoTo Endthis
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
..Text = "NOTE:^w"
..Replacement.Text = "NOTE:^t"
..Format = False
..Forward = True
..Wrap = wdFindStop
End With

Do While Selection.Find.Execute(Replace:=wdReplaceOne)
Selection.Font.Underline = wdUnderlineNone
Selection.Words(1).Font.Underline = wdUnderlineSingle
Selection.Paragraphs(1).Range.Select
Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumRows:=1,
numColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=44,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=423,
RulerStyle:=wdAdjustNone
Selection.ParagraphFormat.TabStops(InchesToPoints(0.5)).Position = _
InchesToPoints(0.63)
With Selection.ParagraphFormat
..LeftIndent = InchesToPoints(0.63)
..KeepWithNext = True
..KeepTogether = True
..SpaceBeforeAuto = False
..SpaceAfterAuto = False
End With
With Selection.ParagraphFormat
..SpaceBeforeAuto = False
..SpaceAfterAuto = False
..FirstLineIndent = InchesToPoints(-0.63)
End With
Selection.Collapse Direction:=wdCollapseEnd
Loop

Endthis:
Selection.HomeKey wdStory

LEU
 
R

Russ

Look up Information Property in VBA help. Down at the bottom of the page it
shows the in table information.
 
L

LEU

Hi Russ,

Thanks for pointing me in the right direction. This is what I came up with
after looking in the help section. It works except that the first occurrence
of the word NOTE: is skipped and then it does all of the rest. Am I missing
something in my macro?

On Error GoTo endthis

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
..Text = "NOTE:^w"
..Replacement.Text = "NOTE:^t"
..Format = False
..Forward = True
..Wrap = wdFindStop
End With
Selection.Find.Execute Replace:=wdReplaceAll
Do While Selection.Find.Execute(Replace:=wdReplaceOne)

If Selection.Information(wdWithInTable) Then
Selection.Rows.AllowBreakAcrossPages = False
Selection.Font.Underline = wdUnderlineNone
Selection.Words(1).Font.Underline = wdUnderlineSingle
Selection.Tables(1).Borders.OutsideLineStyle = wdLineStyleSingle
Selection.Tables(1).Borders.OutsideLineWidth = wdLineWidth100pt
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=44,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=423, RulerStyle:= _
wdAdjustNone
Selection.ParagraphFormat.TabStops(InchesToPoints(0.5)).Position = _
InchesToPoints(0.6)

With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.63)
.KeepWithNext = True
.KeepTogether = True
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.FirstLineIndent = InchesToPoints(-0.63)
End With
With Selection.Tables(1)
.TopPadding = InchesToPoints(0.05)
.BottomPadding = InchesToPoints(0.05)
.LeftPadding = InchesToPoints(0.08)
.RightPadding = InchesToPoints(0.08)
.Spacing = 0
.AllowPageBreaks = False
.AllowAutoFit = False
End With
Selection.Collapse Direction:=wdCollapseEnd

Else

If Not Selection.Information(wdWithInTable) Then
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "NOTE:^w"
.Replacement.Text = "NOTE:^t"
.Format = False
.Forward = True
.Wrap = wdFindStop
End With

Do While Selection.Find.Execute(Replace:=wdReplaceOne)
Selection.Font.Underline = wdUnderlineNone
Selection.Words(1).Font.Underline = wdUnderlineSingle
Selection.Paragraphs(1).Range.Select
Selection.ConvertToTable Separator:=wdSeparateByParagraphs, NumRows:=1,
numColumns:=1, _
DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:=wdAutoFitFixed
Selection.Tables(1).Rows.SetLeftIndent LeftIndent:=44,
RulerStyle:=wdAdjustNone
Selection.Tables(1).Columns(1).SetWidth ColumnWidth:=423,
RulerStyle:=wdAdjustNone
Selection.ParagraphFormat.TabStops(InchesToPoints(0.5)).Position = _
InchesToPoints(0.63)
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.63)
.KeepWithNext = True
.KeepTogether = True
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
End With
With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.FirstLineIndent = InchesToPoints(-0.63)
End With
Selection.Collapse Direction:=wdCollapseEnd
Loop
End If

End If
Loop
endthis:


LEU
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Word 2007 Not responding 0
Find and Replace Question 3
Multiple Tables 2
Table resizing 2
TOC Help 0
TOC Problem 0
Table Format help 1
Table of Content 1

Top