Moving bookmark in word

Zonk1982

New Member
Joined
Dec 19, 2014
Messages
1
Hello all,

I would like to create a word document by copy-pasting the contents of several other documents to a bookmark.
After pasting, I would like to have the same bookmark available one line below the previously pasted content.

The program is working, but pastes every time at the beginning, so that the the content of my created word document is in reverse order:


Code:
Sub Textmarke_fuellen_setzen(text_value As String, bookmark As String, objDoc As Object, objword As Object)

Dim BMRange As Object
   
objword.Activate
objword.Visible = True

    If objDoc.bookmarks.Exists(bookmark) Then
         Set BMRange = objDoc.bookmarks(bookmark).Range
         BMRange.paste
    End If
 
End Sub

I have tried to move the selection and create a new bookmark below the pasted content, but the only thing that worked is not very elegant:


Code:
Sub Textmarke_fuellen_setzen(text_value As String, bookmark As String, objDoc As Object, objword As Object)

Dim BMRange As Object
   
objword.Activate
objword.Visible = True
If objDoc.bookmarks.Exists(bookmark) Then
         Set BMRange = objDoc.bookmarks(bookmark).Range
         BMRange.InsertAfter (vbCrLf & vbCrLf & "aaa")      'aaa is used as a marker for the find-method
         
         Set BMRange = Nothing                              'get rid of selection
         Set BMRange = objDoc.bookmarks(bookmark).Range    'set bookmark again
         BMRange.Text = text_value
         
         Set BMRange = objDoc.Content
         BMRange.Find.Execute findtext:="aaa", Forward:=True 'bad way to get the selection to the right place for creating new bookmark

         If BMRange.Find.Found = True Then
        objDoc.bookmarks.Add bookmark, BMRange
    End If
 End Sub

Any hints, how a bookmark range can be moved?
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
The simplest approach would be to paste in reverse order ...

If that doesn't suit, try:
Code:
Sub Textmarke_fuellen_setzen(DocTgt As Object, StrBkMkNm As String, Optional StrTxt As String, Optional RngSrc As Object)
Dim RngBkMk As Object
With ActiveDocument
  If .Bookmarks.Exists(StrBkMkNm) Then
    Set RngBkMk = .Bookmarks(StrBkMkNm).Range
    With RngBkMk
      'To add unformatted text
      .Text = StrTxt
      'To add formatted content from a defined range in another document
      .FormattedText = RngSrc.FormattedText
      'To paste something from the clipboard
      .Paste
      'Collapse the range so the new bookmark and, hence, the next update, comes at the end of the previous one
      .Collapse 0 ' = wdCollapseEnd
      .Bookmarks.Add StrBkMkNm, RngBkMk
    End With
  End If
End With
End Sub
Note the three different options for adding content this gives. To use the .Text option, you will need to supply the StrTxt variable. To use the .FormattedText option, you will need to supply the RngSrc variable.
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,993
Members
448,539
Latest member
alex78

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