Results 1 to 9 of 9

Thread: Extract only the title of an article (first paragraph) from a Word file using VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Quote Originally Posted by ovisele View Post
    The structure of the Word is as follows (please see two files attached):


    --- JOURNAL of MEDICINE and LIFE
    JML | REVIEW
    The role of novel poly (ADP-ribose) inhibitors in the treatment of locally advanced and metastatic Her-2/neu negative breast cancer with inherited germline BRCA1/2 mutations. A review of the literature
    Authors list, etc, etc ---


    I assume that this is the first paragraph
    An unfounded assumption, as displaying Word's formatting marks would immediately show.

    Your files have quite different structure: one has 'JOURNAL of MEDICINE and LIFE' in the document body; the other has it in the header. Moreover, some of your titles span more than one paragraph. Consequently, you can't work from a paragraph # (4 in one document, 7 & 8 in the other). Instead, you need to use Find to locate the text.

    Since your two examples have the titles in the Normal Style and a bold font, try:

    Sub Demo()Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Font.Bold = True
        .Style = wdStyleNormal
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchWildcards = True
      End With
      Do While .Find.Execute
        If Rng Is Nothing Then
          Set Rng = .Duplicate
        Else
          If Rng.End + 1 = .Start Then
            Rng.End = .End
          Else
            Exit Do
          End If
        End If
      Loop
      MsgBox Rng.Text
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by macropod; 03-18-2021 at 02:26 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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