Consulting

Results 1 to 3 of 3

Thread: Solved: List Text that uses built-in style Heading 1 in a MsgBox

  1. #1
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    3
    Location

    Solved: List Text that uses built-in style Heading 1 in a MsgBox

    Dear Experts:

    Below macro lists the text that uses built-in style Heading 1 in a new document.

    I would like the macro changed with the following feature:

    - list the text in a msgbox, not in a new document

    I hope this is feasible. Thank you very much in advance for your
    help.

    Regards, Andreas

    Sub ListHeading1ParasInNewDocument()
    Dim oPara As Paragraph
    Dim oDocH1 As Document
    Dim oDoc As Document
    Set oDoc = ActiveDocument
    Set oDocH1 = Documents.Add
    'Make sure oDocH1 starts empty and with style Normal
    With oDocH1
    .range = ""
    .Paragraphs(1).Style = oDoc.Styles(wdStyleNormal)
    End With
    'Iterate through all paragraphs in active document
    'If style is Heading 1, insert text in oDocH1
    For Each oPara In oDoc.Paragraphs
    If oPara.Style = oDoc.Styles(wdStyleHeading1) Then
    oDocH1.range.InsertAfter oPara.range.Text
    End If
    Next oPara
    'Clean up
    Set oDoc = Nothing
    Set oDocH1 = Nothing
    End Sub

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]Sub ListHeading1ParasInMessageBox()
    Dim oPara As Paragraph
    Dim oDoc As Document
    Dim myString As String

    Set oDoc = ActiveDocument
    ' Iterate through all paragraphs in active document
    ' If style is Heading 1, add text to string
    ' plus a paragraph mark
    For Each oPara In oDoc.Paragraphs
    If oPara.Style = oDoc.Styles(wdStyleHeading1) Then
    myString = myString & oPara.Range.Text & vbCrLf
    End If
    Next oPara
    ' display message
    MsgBox myString
    'Clean up
    Set oDoc = Nothing
    End Sub
    [/vba]

  3. #3
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    3
    Location
    Hey fumei,
    exactly what I wanted. Thank you very much. Regards, Andreas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •