A
andreas
Dear Experts:
Below macro lists all heading 1 paragraphs in a msgbox along with the
corresponding sections. I would like the contents of the MsgBox to be
added/transferred/printed to a new document as well. How is this
achieved?
Help is appreciated. Thank you very much in advance. Regards, Andreas
Sub List_Heading1_Text()
Dim opara As Paragraph
Dim strMsg As String
Dim rTmp As range ' a temporary range
Dim lTmp As Long ' sections count
Dim l2Tmp As Long
Set rTmp = ActiveDocument.range
'Create start of message
strMsg = "Heading 1 text: " & vbCr
'Iterate through all paragraphs in active document
'If style is Heading 1, append to message
For Each opara In ActiveDocument.Paragraphs
If opara.Style = ActiveDocument.Styles(wdStyleHeading1) Then
' strMsg = "Heading 1 text: " & vbCr
With opara.range
rTmp.End = .End ' redefine the temporary range
lTmp = rTmp.Sections.count ' count the sections
'Append the heading number and text to the message
strMsg = strMsg & " section " & CStr(lTmp) & ":"
strMsg = strMsg & " " & .ListFormat.ListString & " " & .Text &
" "
'MsgBox strMsg, vbOKOnly, "Heading 1 Text"
End With
End If
Next opara
MsgBox strMsg, vbOKOnly, "Heading 1 Text"
End Sub
Below macro lists all heading 1 paragraphs in a msgbox along with the
corresponding sections. I would like the contents of the MsgBox to be
added/transferred/printed to a new document as well. How is this
achieved?
Help is appreciated. Thank you very much in advance. Regards, Andreas
Sub List_Heading1_Text()
Dim opara As Paragraph
Dim strMsg As String
Dim rTmp As range ' a temporary range
Dim lTmp As Long ' sections count
Dim l2Tmp As Long
Set rTmp = ActiveDocument.range
'Create start of message
strMsg = "Heading 1 text: " & vbCr
'Iterate through all paragraphs in active document
'If style is Heading 1, append to message
For Each opara In ActiveDocument.Paragraphs
If opara.Style = ActiveDocument.Styles(wdStyleHeading1) Then
' strMsg = "Heading 1 text: " & vbCr
With opara.range
rTmp.End = .End ' redefine the temporary range
lTmp = rTmp.Sections.count ' count the sections
'Append the heading number and text to the message
strMsg = strMsg & " section " & CStr(lTmp) & ":"
strMsg = strMsg & " " & .ListFormat.ListString & " " & .Text &
" "
'MsgBox strMsg, vbOKOnly, "Heading 1 Text"
End With
End If
Next opara
MsgBox strMsg, vbOKOnly, "Heading 1 Text"
End Sub