Consulting

Results 1 to 2 of 2

Thread: Insert newline/paragraph break after a bookmark

  1. #1

    Insert newline/paragraph break after a bookmark

    Hello,


    I have a following format of text in a document - the text in a bookmark icludes a paragraph break:

    HEADING STYLE 1¶
    [Text in a bookmark in Normal Style¶]
    HEADING STYLE 2¶


    What I'd like to do is to insert a paragraph break just after the bookmark as the text inside will be replaced:

    HEADING STYLE 1¶
    [Text in a bookmark in Normal Style¶]
    ¶ (Normal Style required)
    HEADING STYLE 2¶

    I have a code but the result is following:

    HEADING STYLE 1¶
    [Text in a bookmark in Normal Style¶]
    HEADING STYLE 2¶ (just the numbering)
    Remaing part of the HEADING STYLE 2 line in Normal Style¶



    Can someone help me with a solution? Thank you.


    Option Explicit
    
    Sub Insert_Line_after_Bookmark()
    Application.ScreenUpdating = False
    
    Dim strFolder As String
    Dim strFile As String
    Dim strName As String
    Dim wdDoc As Document
    Dim i As Long
    Dim oRng As Range
    Dim BkMks As Bookmark
    
    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    strFile = Dir(strFolder & "\*.docx", vbNormal)
    
    While strFile <> ""
        Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
        wdDoc.Activate
        With wdDoc
            For i = 1 To ActiveDocument.Bookmarks.Count
                strName = ActiveDocument.Bookmarks(i).Name
                wdDoc.Bookmarks(strName).Select
                wdDoc.Range(Selection.End, Selection.End).Select
                Selection.TypeParagraph
                Selection.Style = ActiveDocument.Styles("Normal")
            Next i
        End With
        wdDoc.Close SaveChanges:=True
        strFile = Dir()
    Wend
    Set wdDoc = Nothing
    Application.ScreenUpdating = False
    End Sub
    
    
    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,063
    Location
    Adapting this to your code above should work:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oBM As Bookmark
    Dim oRng As Range
      Set oBM = ActiveDocument.Bookmarks("bmTest")
      Set oRng = oBM.Range
      oRng.InsertAfter vbCr
      oRng.Paragraphs.Last.Style = "Normal"
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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