Print contents of a msgbox to a new document

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
 
J

Jay Freedman

At the point where the MsgBox is displayed, all the text for it is already in
the string variable strMsg. To create a new document and place that text into
it, add these lines after the MsgBox line and before the End Sub line:

Set oDoc = Documents.Add
oDoc.Range.Text = strMsg

You should also add this line among the Dim statements at the beginning:

Dim oDoc As Document


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
A

andreas

At the point where the MsgBox is displayed, all the text for it is already in
the string variable strMsg. To create a new document and place that text into
it, add these lines after the MsgBox line and before the End Sub line:

Set oDoc = Documents.Add
oDoc.Range.Text = strMsg

You should also add this line among the Dim statements at the beginning:

Dim oDoc As Document

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all
may benefit.













- Show quoted text -

Hey Jay,

thank you very much. It is working, Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top