Ok, I'm struggling to get this code to work the way I want it to. I have a document that I'm trying to set up a macro to insert copied text from a source document into the middle of the target document. I have placed a bookmark there, and the code that I have will actually paste the text, but it always pastes the text at the very beginning of the bookmark. I need to be able to run this macro multiple times to insert multiple paragraphs after the bookmark.
The source document is filled with paragraphs, each one of which is inside an enclosing bookmark named "B", then an alpha-numeric code after. The target form we are trying to populate will need anywhere from 1 to 7 of these paragraphs. I don't mind having to run the macro several times (although, if theres a way to use a comma separated input box list to loop through, that would be nice), but the way the macro runs now, it puts the paragraphs in reverse order, meaning that if I run it for Paragraph 1, then for Paragraph 2, it orders them as Paragraph 2, then Paragraph 1. I need to be able to insert the second (and subsequent) paragraph after the first one.
I've tried using insertbefore and insertafter, and can't get them to work. I tried to add a new bookmark before the original bookmark, and then using the new bookmark as the drop location for the text, but couldn't get that code functioning. There is also a command button (1) just below where I would want the paragraphs to drop in, so if it's possible to use that as an insertion point, that would be wonderful. But I couldn't find anything at all on that, so I went the bookmark route.
Private Sub CommandButton1_Click()
Dim TarDoc As Document, SourDoc As Document
Dim aRng As Word.Range
Dim Mark As String, MarkB As String
Application.ScreenUpdating = False
Set SourDoc = Documents.Open("C:\Projects\SourceDocument.docx")
Set TarDoc = ThisDocument
Set aRng = TarDoc.Bookmarks("End").Range
Do
Mark = InputBox("100, 289, 981a...", "Enter Number and Letter as Shown")
If Len(Mark) Then ' do nothing if user enters nothing
MarkB = "B" & Mark
If SourDoc.Bookmarks.Exists(MarkB) Then
SourDoc.Bookmarks(MarkB).Range.Copy
aRng.PasteAndFormat (wdFormatOriginalFormatting)
Mark = vbNullString ' to enable exiting the loop
Else
MsgBox "Bookmark """ & Mark & """ doesn't exist.", vbInformation, _
"Invalid entry"
End If
End If
Loop While Len(Mark) ' the user entered a wrong mark
Application.ScreenUpdating = True
End Sub
Mark = vbNullString
can be replaced withExit Do
which might be a more intuitive way of exiting the loop – YowE3K