finding and setting indention

  • Thread starter gaubahn via OfficeKB.com
  • Start date
G

gaubahn via OfficeKB.com

hello to all,

im new to making macro and i mostly rely on Microsoft Word's Macro Recorder
for the basic layout of the Macro and from there i just make few adjustments
through Microsoft Visual Basic...and right now i find myself stuck and im
hoping for some professional help here which will be greatly appreciated.

Here's what im trying to do:
1. Look for any line in the document with a Tab Stop 3.5"
2. Indent everything in that line to 3.76"

here is the Macro

Sub Macro()

Selection.Find.ClearFormatting
Selection.Find.ParagraphFormat.TabStops.ClearAll
Selection.Find.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3.5),
_
Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)
End Sub

the problem with this is once i execute it the Tab Stop of the line where my
cursor is located is set to 3.76"

once again thanks in advance for your help.
 
K

Klaus Linke

Hi gaubahn,

Try

If Selection.Find.Execute then
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)
End If

(.Execute returns "True" if .Find found something)

Greetings,
Klaus
 
G

gaubahn via OfficeKB.com

Hi Klaus,

thanks for the help, but if you dont mind my asking where do i insert that
code??? im if the question seems lame but i really am new to all of this and
i tried placing them at the end or at the middle of With and End With but
nothing happens when i execute the macro...thanks again.

regards,
gaubahn



Klaus said:
Hi gaubahn,

Try

If Selection.Find.Execute then
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)
End If

(.Execute returns "True" if .Find found something)

Greetings,
Klaus
hello to all,
[quoted text clipped - 42 lines]
once again thanks in advance for your help.
 
K

Klaus Linke

Hi gaubahn,

Put it where you currently have

Selection.Find.Execute
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)

And delete the line
Selection.Find.ParagraphFormat.Borders.Shadow = False
.... it was put into the code erroneously by the macro recorder.

If you want to replace all occurrences at once, you could use
While Selection.Find.Execute
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)
Wend

("while Find.Execute matches something, change the tab stop")

On the other hand, "Find" is a bit problematic with tab stops, since Word
rounds the position. So Find might fail because the user interface shows
3.5" (which you then use in your code), but the tab might really be at
3.497" or somewhere else.

It would be safer (albeit slower) to look at all paragraphs in turn, and
change the tab if it currently is, say, between 3.4" and 3.6":

Sub ChangeYourTabs()
Dim myPara As Paragraph
Dim myTab As TabStop
For Each myPara In ActiveDocument.Paragraphs
For Each myTab In myPara.TabStops
Select Case PointsToInches(myTab.Position)
Case 3.4 To 3.6
myTab.Position = InchesToPoints(0.5)
End Select
Next myTab
Next myPara
End Sub

(BTW, 3.5" is a mighty big tab stop... you don't really mean points or
millimeters?)

Regards,
Klaus



gaubahn via OfficeKB.com said:
Hi Klaus,

thanks for the help, but if you dont mind my asking where do i insert that
code??? im if the question seems lame but i really am new to all of this
and
i tried placing them at the end or at the middle of With and End With but
nothing happens when i execute the macro...thanks again.

regards,
gaubahn



Klaus said:
Hi gaubahn,

Try

If Selection.Find.Execute then
Selection.ParagraphFormat.TabStops(InchesToPoints(3.5)).Position = _
InchesToPoints(3.76)
End If

(.Execute returns "True" if .Find found something)

Greetings,
Klaus
hello to all,
[quoted text clipped - 42 lines]
once again thanks in advance for your help.
 

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

Top