0
votes

I am working on a macro that will extract the wrong spelling in a Word file, I was able to search for a macro that can do just that, however I wanted to add a function that can also extract the heading preceding the body text.

With oRow                             
    .Cells(1).Range.Text = oDoc.SpellingErrors(J).Information(wdActiveEndPageNumber)
    .Cells(2).Range.Text = oDoc.SpellingErrors(J).Information(wdFirstCharacterLineNumber)
    .Cells(3).Range.Text = oDoc.SpellingErrors(J)
    .Cells(4).Range.Text = 'SHOULD CAPTURE THE HEADING                      
 End With

*** Slide **** 2 Text

Shape: Title 1 >> Text: The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brownt fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

In the sample above, my existing macro is already extracting "brownt" because it is a wrong spelling. I need to add a line that will also capture "*** Slide **** 2 Text" because it is the heading of that paragraph.

1
I'm not sure we mean the same thing with "header"... In Word, the header is what appears at the top of every page, such as a page number or a date. (And a Footer is the bottom of the page.) Do you mean a Heading? Is it formatted with a built-in Heading style?Cindy Meister
Sorry, Yes @Cindy, I mean heading style specifically "Title"Arpee
Arpee, "Title" is not a Heading style, just a "regular" style. Heading styles are Heading 1, Heading 2, etc. There'd be a very simple way to do this if you were using a Heading style...Cindy Meister
Thanks for the correction Cindy, let's say it is "Heading 1"Arpee

1 Answers

0
votes

Word has some built-in bookmarks that make it easier to address certain parts of the document. Since they come from the old, Word Basic days, they work with the current selection.

Thus, the code below first selects the spelling error, then the entire heading level in which the spelling error is located. From there, the first paragraph of the heading level (the text formatted with a Heading style) is picked up and assigned to the target cell in the code in the question.

Dim rng as Word.Range
With oRow                             
    .Cells(1).Range.Text = oDoc.SpellingErrors(J).Information(wdActiveEndPageNumber)
    .Cells(2).Range.Text = oDoc.SpellingErrors(J).Information(wdFirstCharacterLineNumber)
    .Cells(3).Range.Text = oDoc.SpellingErrors(J)
    oDoc.SpellingErrors(J).Select 
    Set rng = oDoc.Bookmarks("\HeadingLevel").Range
    rng.Collapse wdCollapseStart
    rng.MoveEnd wdParagraph, 1
    rng.MoveEnd wdCharacter, -1
   .Cells(4).Range.Text = rng.Text
 End With