PDA

View Full Version : Word Headings to Excel



gtech
02-03-2006, 10:01 AM
I need a macro that will go through the document and list the text from Heading 2 and Heading 6 Word document styles into a single column in a spreadsheet. Any help in getting me started would be appreciated. http://vbaexpress.com/forum/images/smilies/banghead.gif

fumei
02-05-2006, 10:44 AM
You know...there may be a simpler way of doing this. Don't use Find.Dim oPara As Word.Paragraph
Dim aDoc As Document
Set aDoc = ActiveDocument
For Each oPara In aDoc.Paragraphs
If oPara.Style = "Heading 1" Or oPara.Style = "Heading 2" Then
' replace this below with your code to
' move the content to Excel
' use oPara.Range.Text - this is the text of the
' paragraph with Heading 1 or Heading 2
MsgBox oPara.Range.Text & vbTab & oPara.Style
End If
Next
Set aDoc = Nothing

gtech
02-06-2006, 09:43 AM
Gerry,

Thanks, works great with one very small problem.

The text that is pasted to the spreadsheet shows a small square box symbol at the end of every row of text. Any idea how to get rid of this symbol?

gtech
02-06-2006, 10:06 AM
Gerry, the text looks like this:

3.3.1, App A Table, Item 1 3.3.1, App A Table, Item 2 3.3.1, App A Table, Item 3-A 3.3.1, App A Table, Item 3-B

fumei
02-06-2006, 10:15 AM
Sure, strip off that character. It is probably Chr(13), or the paragraph mark.

Personally, I think it is best to make your string that is being passed to Excel an actual variable.Dim strText2Go As String
For Each oPara In aDoc.Paragraphs
If oPara.Style = "Heading 1" Or oPara.Style = "Heading 2" Then
strText2Go =Left(oPara.Range.Text, Len(oPara.Range.Text) - 1)
' now you can simply use strText2Go as a variable
' and do whatever you need with it
End If
Next

what that does is use Left - which extracts from a string x characters, starting from the left. Left(text, x);

But as the text is going to be any length, you need to use the length - 1...the last character. So x = Len(text) - 1;

Therefore Left(text, Len(text) - 1), ORLeft(oPara.Range.Text, Len(oPara.Range.Text) - 1)

gtech
02-06-2006, 12:45 PM
Gerry,

Worked great..thanks.

It works so well now I am anticipating the manager to ask if I can add another header from the document to the spreadsheet. The problem is this header (projVersion) is embedded in tables within each paragraph so projVersion is not associated with a new paragraph. With this code work for a heading that is not associated with a new paragraph?

fumei
02-07-2006, 09:29 AM
1. Just for your knowledge, each cell in a table IS a new paragraph. And each paragraph in a cell is also a new paragraph. You can test this easily - make a new document with NO paragraphs. Add a table with 3 rows, 2 columns. Do a Word count. It will indicate paragraphs = 2. HOWEVER, if you do:MsgBox _
ActiveDocument.Content.Paragraphs.Countyou get paragraphs = 10. One for each cell, one for each end-of-row, one for the end-of-document.

So- the code will run through each paragraph - whether it is in a table, or not.

2. Be careful about what you call "header".
The problem is this header (projVersion) is embedded in tables within each paragraph so projVersion is not associated with a new paragraph. This confuses me. "this header (projVersion)" - uh, is this a STYLE? What do you mean by "embedded in tables within each paragraph"? Huh????? As for "associated with a new paragraph" - I have not a clue what you mean by that.

gtech
02-07-2006, 11:01 AM
Gerry,

Thanks for clarifiying. Obviously I have much to learn.

Yes, projVersion is a style that I have set up for one of the cells. I added this style to the other styles being selected and it works!

Thanks again.

fumei
02-07-2006, 11:44 AM
Yes, it would, as it is the Style that is used for the IF logic statement. Oh, again, it is best if you learn ( and I am certainly not being critical here!) what terms mean.
I added this style to the other styles being selected and it works! The styles are not being "selected", they are being used logically.

IF the style of the paragraph is XXXXX THEN do this.

Logically there is absolutely no difference if the statement is:

IF the style of the paragraph is AAAAA THEN do this. OR;

IF the style of the paragraph is QQQQQQ THEN do this.

The statement simply checks what the style is, and if it is the one mentioned, then it follows the THEN instruction. If it is not, then it does not follow the THEN instruction. IF statements are always validated as either True, or Not True. You can expand them by using ElseIf...see Help.