0
votes

When I insert the data into Bookmark, it goes to the beginning of the line.

What a property needs to be set to insert the text in the middle of the paragraph?

Data is copied from Excel
Tried so far:
Copy range and paste
Copy text from value and pastespecial
Paste is floating everywhere, but not at Bookmark.

If wDoc.Bookmarks.Count = 0 Then GoTo BookmarkMissing

    For i = 1 To wDoc.Bookmarks.Count
        If wDoc.Bookmarks(i).Name = sBookmarkName Then
            Set wdRange = wDoc.Bookmarks(i).Range
            Let bBookmarkFound = True
        End If
    Next i
    If Not bBookmarkFound Then GoTo BookmarkMissing
Else
    Set wdRange = wDoc.Range
End If
    
MyData.SetText rngToSend.Value2
MyData.PutInClipboard

On Error Resume Next

On Error GoTo 0

'rngToSend.Copy

'wdRange.PasteSpecial Placement:=wdFloatOverText, DataType:=2
wdRange.InsertAfter
wdRange.Tables(1).ConvertToText Separator:=" "
'wdRange.PasteSpecial Placement:=wdFloatOverText, DataType:=2
'wdRange.PasteSpecial Placement:=wdInLine
3
What do you mean by "always go to the beginning of the line"? Please use the edit button below your question and add a screenshot showing where the text is going and where it needs to be. Please ensure that the bookmark is visible.Timothy Rylatt

3 Answers

1
votes

Your code can be greatly simplified. There is no need to loop through bookmarks to find if the one you need exists as the object model has a method to do that.

So this code:

  If wdoc.Bookmarks.Count = 0 Then GoTo BookmarkMissing
  For I = 1 To wdoc.Bookmarks.Count
      If wdoc.Bookmarks(I).name = sBookmarkName Then
          Set wdRange = wdoc.Bookmarks(I).Range
          Let bBookmarkFound = True
      End If
  Next I
  If Not bBookmarkFound Then GoTo BookmarkMissing

Can be replaced with this

  If wdoc.Bookmarks.Exists(sBookmarkName) Then
    wdoc.Bookmarks(sBookmarkName).Range.text = rngToSend.Value2
  Else
    GoTo BookmarkMissing
  End If
0
votes

Since you're moving plain text, the clipboard is not necessary.

wDoc.Bookmarks(i).Range.Text = rngToSend.Value2
0
votes

Thank you guys for your help. However, I had a bug, one old line of code (not visible in my post_ was still pasting value as a Excel range and inserting a table instead of text.

So now is solved