0
votes

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 "

1
There's no bold - that's a lot of text that's not telling us much of anything... You can use use edit link below the question to tighten this up and make it meaningful. Also, you have to use "Word-speak" if you want Word specialists to understand. "case-return" is has no meaning in the Word world - do you mean "carriage return"? There's a HUGE difference in Word between a "line" (Shift+Enter) and a paragraph (Enter). Which, exactly do you mean? I suspect paragraph (=carriage return), but it's important to be certain. Also, are "lines" or paragraphs to be deleted? - Cindy Meister
Have you tried with Word's FIND feature to get started with the basic code needed? - Cindy Meister
Thanks Cindy, The text is unimportant, I included that to give you an idea of the structure of the document. It is only the list (which I have highlighted in bold) that I want to deal with. I have tried it as a means to get there. I think I am close. How do I extract the count from find? When I do a search using the advanced search feature, there is a line in the searchbox which states "word found x items matching your criteria" I want to use that count to run a for loop to replace all but the last paragraph (enter) ^p - Bek_Fleitjie
I answered the separate question you posted about the counting - saw it before this comment. Does that solve what you're trying to ask here? - Cindy Meister

1 Answers

0
votes

You really don't need a macro for this - it can all be done via a wildcard Find/Replace, where:

Find = [^13]{1,}(Item [0-9])
Replace = ^32\1

with or without setting the Find font parameter to Bold. You could, of course, record the Find/Replace as a macro. Similarly, removing excess paragraph breaks is as simple as doing a wildcard Find/Replace, where:

Find = [^13]{2,}
Replace = ^p