J
James
Hello,
I wonder if someone could help me please? - Im hoping this should be
fairly easy to anyone adept at Word VBA. I have put together this
untidy Macro and would like to make it more efficient and compact if
poss - but I dont know how as this is on the limit of my ability
really.
My goal is to get the macro to search for List Bullets, delete any
trailing spaces, delete common punctuation at end of bullet and add a
period to the end of the last bullet in the list (which it does not do
yet).
I would like this to happen one bullet at a time (per button click) so
I can check that having no punctuation is suitable.
If someone could help me with this I would be most grateful
Thanks
James
Public Sub bulletpunctuation()
Dim myRange As Word.Range
Dim mystring As String
Dim rngTarget As Word.Range
Dim oPara As Word.Paragraph
Dim yPara As Word.Range
'Delete trailing spaces
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^w^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
'Look for bullets and move to end
Set rngTarget = Selection.Range
With rngTarget
Call .Collapse(wdCollapseEnd)
.End = ActiveDocument.Range.End
For Each oPara In .Paragraphs
If oPara.Range.ListFormat.ListType =
WdListType.wdListBullet Then
Set yPara = oPara.Range
yPara.Move wdParagraph
yPara.End = yPara.End - 2
yPara.MoveEnd wdCharacter, 2
yPara.Select
'replace end punctuation
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ";"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ":"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "."
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
yPara.Move wdParagraph, 1
Exit For
End If
Next
End With
End Sub
I wonder if someone could help me please? - Im hoping this should be
fairly easy to anyone adept at Word VBA. I have put together this
untidy Macro and would like to make it more efficient and compact if
poss - but I dont know how as this is on the limit of my ability
really.
My goal is to get the macro to search for List Bullets, delete any
trailing spaces, delete common punctuation at end of bullet and add a
period to the end of the last bullet in the list (which it does not do
yet).
I would like this to happen one bullet at a time (per button click) so
I can check that having no punctuation is suitable.
If someone could help me with this I would be most grateful
Thanks
James
Public Sub bulletpunctuation()
Dim myRange As Word.Range
Dim mystring As String
Dim rngTarget As Word.Range
Dim oPara As Word.Paragraph
Dim yPara As Word.Range
'Delete trailing spaces
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^w^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
'Look for bullets and move to end
Set rngTarget = Selection.Range
With rngTarget
Call .Collapse(wdCollapseEnd)
.End = ActiveDocument.Range.End
For Each oPara In .Paragraphs
If oPara.Range.ListFormat.ListType =
WdListType.wdListBullet Then
Set yPara = oPara.Range
yPara.Move wdParagraph
yPara.End = yPara.End - 2
yPara.MoveEnd wdCharacter, 2
yPara.Select
'replace end punctuation
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ";"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ":"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Set myRange = yPara
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "."
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
yPara.Move wdParagraph, 1
Exit For
End If
Next
End With
End Sub