Insert Text between two bookmarks of a word document into a new word document (after bookmark)

Sarahmueller

New Member
Joined
May 17, 2020
Messages
24
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello,

i hope you all are healthy!


I want to insert the text written between two bookmarks ("Beginning_old") and ("End_old") of an baseline word document, into a new word document.
In the new word document, the text should be inserted between a bookmark called "Beggining_newdoc" and a bookmark called "End_newdoc". Afterwards, the new document should save and close.

I have come up with the following code:

VBA Code:
Sub Insertbetweenbookmarks()
Dim oTarDoc As Document, oSourceDoc As Document
Dim oRng As Word.Range
Set oSourceDoc = ActiveDocument
Set oTarDoc = Documents.Open("C:\path of new document")
With oTarDoc
Set oRng = .Bookmarks("Beggining_newdoc").Range
oRng.Text = oSourceDoc.Bookmarks("Beginning_old").Range.Text
.Bookmarks.Add "Beggining_newdoc", oRng
Set oRng = .Bookmarks("End_newdoc").Range
oRng.Text = oSourceDoc.Bookmarks("End_old").Range.Text
.Bookmarks.Add "End_newdoc", oRng
End With
lbl_Exit:
Exit Sub
End Sub

Nevertheless, this code only replaces the bookmarks in the new document with the ones of the old document.

Do you have an idea how to modify the macro, so that the text between the old bookmarks ["Beginning_old" and "End_old"] is inserted between the bookmarks ["Beggining_newdoc" and "End_newdoc"]
of the new word document?


Thank you in advance and best regards,

Sarah
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Why are you trying to insert content between two bookmarks instead of bookmarking the replaceable content and simply updating that?
 
Upvote 0
Because I want to insert the text into predefined sections of a word document. Since the length of the text that should be inserted varies every time i want to use the macro, inserting the text between two predefined bookmarks guarantees that i can keep the structure of the text.

Inserting the text between two bookmarks also means that i can keep the bookmarks, so that i can re-use the macro.
 
Upvote 0
I have tried using the following code:

VBA Code:
Sub CopyBookmark()
Dim wdDocOld As Document
Dim wdDocNew As Document
Dim rngSrc As Range
Dim rngDst As Range
Dim rngStart As Range
Dim rngEnd As Range

    Set wdDocOld = ThisDocument
    Set wdDocNew = Documents.Open(FileName:="New.docx")
    
    With wdDocOld
        Set rngStart = .Bookmarks("Start").Range
        Set rngEnd = .Bookmarks("End").Range
        Set rngSrc = .Range(rngStart.Start, rngEnd.End)
    End With
    
    Set rngDst = wdDocNew.Bookmarks("BookmarkNewDoc").Range
    
    rngDst.InsertAfter rngSrc.Text
    
End Sub


Start and End are the bookmarks of the old document, and BookmarkNewDoc is the bookmark in the new document.



But this macro also pastes the names of the bookmarks that define the range in the old document into the new document. I have tried using Range.End + 1 / Range.End - 1, but this does not work correctly.

Additionally, I want the macro to insert the text below the bookmark (since the bookmarks are headings of a word document) like this:

"BookmarkNewDoc"
Inserted text


Do you have an Idea how to solve my problem?

Best regards and thanks in advance,

Sarah
 
Upvote 0
Because I want to insert the text into predefined sections of a word document. Since the length of the text that should be inserted varies every time i want to use the macro, inserting the text between two predefined bookmarks guarantees that i can keep the structure of the text.
You're missing the point. You can insert multiple pages of variable formatted content, including headings, numbered & bulleted paragraphs, tables and pictures within a bookmark, then replace that content anytime you want with some different formatted content.

The code below does all you actually need to update the variable content for a given bookmark, including all the formatting you want to replicate from the source:
VBA Code:
With oTarDoc
  Set oRng = .Bookmarks("SomeBookmark").Range
  oRng.FormattedText = oSourceDoc.Bookmarks("SomeBookmark").Range.FormattedText
  .Bookmarks.Add "SomeBookmark", oRng
End With
In other words, you don't insert between two bookmarks; instead you insert within one bookmark.
 
Upvote 0
Thank you!

but how do I insert the name of the new Word-File into the code?
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top