Results 1 to 3 of 3

Thread: Search an expression (code) and turn the whole line into heading

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    The issue with your code is that Find only searches for the complete text at once. Here's an improved macro that iterates through lines and checks for the code:
    Sub Head6_To_Heading()
        Dim doc As Word.Document
        Dim rng As Word.Range
        Dim para As Word.Paragraph
        Dim foundText As String
        Set doc = ActiveDocument
        foundText = "(head-"  ' Code to search for
        For Each para In doc.Paragraphs
             ' Check if line contains the code
             If InStr(para.Text, foundText) > 0 Then
                 Set rng = para.Range
                 ' Apply Heading 6 style
                 rng.Style = doc.Styles("Heading 6")
                 Exit For ' Stop after finding the first occurrence (modify if needed)
            End If
        Next para
    End Sub
    Last edited by Aussiebear; 08-12-2024 at 11:39 AM.

Tags for this Thread

Posting Permissions

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