Log in

View Full Version : [SLEEPER:] Insert newline/paragraph break after a bookmark



Pearce
10-15-2019, 01:29 PM
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
End If
Set oFolder = Nothing
End Function

gmaxey
10-16-2019, 04:49 AM
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