K
ken
I've created a macro to modify the Index table of a document. It has 3
parts:
1 Find the beginning of the Index table.
2 Find a specified string and cut it from the table.
3 Find a specified string (x number of times), move the cursor up/down
specified times and insert paste data.
Sub find_index()
' find the start of the index table
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 7")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "index"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Find.ClearFormatting
End Sub
Sub find_cut(cut_str As String)
' find cut point
' first; find the beginning of the index table
find_index
With Selection.Find
.Text = cut_str
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' select the entire line
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End Sub
Sub find_ins(ins_str As String, Rpt_Cnt As Integer, UpDown As Integer)
' find insert point
With Selection.Find
.Text = ins_str
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Dim cntr
cntr = Rpt_Cnt
Do
Selection.Find.Execute
cntr = cntr - 1
Loop Until cntr <= 0
' move the cursor to the beginning of the line
Selection.HomeKey Unit:=wdLine
cntr = UpDown
If (UpDown < 0) Then
Do
Selection.MoveDown Unit:=wdLine, Count:=1
cntr = cntr + 1
Loop Until cntr >= 0
ElseIf (UpDown > 0) Then
Do
Selection.MoveDown Unit:=wdLine, Count:=1
cntr = cntr - 1
Loop Until cntr <= 0
End If
' insert cut string
Selection.Paste
End Sub
I'm having 2 problems that I'm stuck on. The first problem has to do with
the call of the subroutine to find the insertion point (find_ins)
find_ins ( "Test API", 0, 0 )
I get a error message: "Compile error \ Expected: =". I don't program very
much with VBA (once every 5 years) and suspect that I'm forgetting something
basic here but what?
The other problem I'm having has to do with strings that have
leading/trailing double quotes. My macro won't compile with them.
Any ideas?
TIA,
Ken Erickson
parts:
1 Find the beginning of the Index table.
2 Find a specified string and cut it from the table.
3 Find a specified string (x number of times), move the cursor up/down
specified times and insert paste data.
Sub find_index()
' find the start of the index table
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 7")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = "index"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Find.ClearFormatting
End Sub
Sub find_cut(cut_str As String)
' find cut point
' first; find the beginning of the index table
find_index
With Selection.Find
.Text = cut_str
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' select the entire line
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End Sub
Sub find_ins(ins_str As String, Rpt_Cnt As Integer, UpDown As Integer)
' find insert point
With Selection.Find
.Text = ins_str
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Dim cntr
cntr = Rpt_Cnt
Do
Selection.Find.Execute
cntr = cntr - 1
Loop Until cntr <= 0
' move the cursor to the beginning of the line
Selection.HomeKey Unit:=wdLine
cntr = UpDown
If (UpDown < 0) Then
Do
Selection.MoveDown Unit:=wdLine, Count:=1
cntr = cntr + 1
Loop Until cntr >= 0
ElseIf (UpDown > 0) Then
Do
Selection.MoveDown Unit:=wdLine, Count:=1
cntr = cntr - 1
Loop Until cntr <= 0
End If
' insert cut string
Selection.Paste
End Sub
I'm having 2 problems that I'm stuck on. The first problem has to do with
the call of the subroutine to find the insertion point (find_ins)
find_ins ( "Test API", 0, 0 )
I get a error message: "Compile error \ Expected: =". I don't program very
much with VBA (once every 5 years) and suspect that I'm forgetting something
basic here but what?
The other problem I'm having has to do with strings that have
leading/trailing double quotes. My macro won't compile with them.
Any ideas?
TIA,
Ken Erickson