I am trying to adjust a few minor structural issues in a word document that is an output from another piece of software over which I have no control.
This is my first endeavor into using VBA in Microsoft Word and I am having a hard time wrapping my head around indexing and "row numbers" within a text document.
The problem I am trying to solve is this:
I receive a word document (50-100 pages long) which is ultimately a catalogue of items and their descriptions, however one particular description has excess carriage returns(i.e paragraph ^p). I want to create a for loop which does the following (based on my EXCEL row and column-based logic):
1) go through the entire document and create two index vectors based upon the location of the phrase "Specific List:" and "Another Specific List:"
2) determine the number of "rows" between these two lines (i.e. I want them to be on subsequent lines)
3a) if the two phrases are 1 line apart, do nothing
3b) if the two phrases are not one line apart then:
Sub Remove_excess_case_returns ()
for i = 1:N < where N is the number of case returns between "Specific List:" and "Another Specific List:" minus 1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=", "
Selection.Delete Unit:=wdCharacter, Count:=1
end Sub
What I get (bold items are relevant lines I wish to remove case returns and make comma-separated list):
...
Aenean nec lorem. In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat ligula.
Specific List: Item 1
Another Specific List: Item 1
Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien. Donec ut est in lectus consequat consequat. Etiam eget dui. Aliquam erat volutpat. Sed at lorem in nunc porta tristique. Proin nec augue. Quisque aliquam tempor magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ac magna. Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis. Pellentesque porttitor, velit lacinia egestas auctor, diam eros tempus arcu, nec vulputate augue magna vel risus. Cras non magna vel ante adipiscing rhoncus. Vivamus a mi. Morbi neque. Aliquam erat volutpat. Integer ultrices lobortis eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Specific List: Item 1
Item 2
Item 3
Another Specific List: Item 1
...
What I want
...
Aenean nec lorem. In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat ligula.
Specific List: Item 1
Another Specific List: Item 1
Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien. Donec ut est in lectus consequat consequat. Etiam eget dui. Aliquam erat volutpat. Sed at lorem in nunc porta tristique. Proin nec augue. Quisque aliquam tempor magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ac magna. Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis. Pellentesque porttitor, velit lacinia egestas auctor, diam eros tempus arcu, nec vulputate augue magna vel risus. Cras non magna vel ante adipiscing rhoncus. Vivamus a mi. Morbi neque. Aliquam erat volutpat. Integer ultrices lobortis eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Specific List: Item 1, Item 2, Item 3
Another Specific List: Item 1
...
What I am currently using is this:
Sub test()
With Selection.Find
.Text = "Specific List:"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Do
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If InStr(1, Selection.Text, "Another Specific List:") Then Exit Do
Selection.MoveDown Unit:=wdLine, Extend:=wdExtend
Loop
'p_count = NEED TO EXTRACT ^p COUNT
If p_count > 1 Then
For i = 1 To p_count - 1 'replace all but the last ^P
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=", "
Selection.Delete Unit:=wdCharacter, Count:=1
Next
Else
End If
End Sub
How do I count the number of paragraph (enter) "^p" in a selection? I see this in the find and replace window when I search, it states "word found x occurrences of "