I have a word document that contains four bookmarks unsurprisingly named "Bookmark_1", "Bookmark_2", "Bookmark_3" and "Bookmark_4".
I want to paste a different sentence from Excel into each bookmark. The sentence to be pasted is chosen based on where it should appear in the document and the value of a variable named Status.
The bookmark name should be retained and not destroyed when pasting the new text as the document is cycled through several times, with successive documents containing different text based on the value of status.
The value of the Status variable is obtained from elsewhere with the original Excel sheet, but I replicated it manually in this Sub just to make the selection process work.
My issue is copying the chosen text in the Text(n) string into the bookmarks in the Word Document - which I seem unable to achieve.
Having watched several video's on you tube and read some articles stating how 'easy' it is, I managed to replicate their demonstration but have failed to successfully implement it into my worksheet.
The original example used this which I understand is pulling the text from a worksheet cell A1 which works just fine in testing.
.Bookmarks("Text2").Range.Text = ws.Range("A1").Value
However because I'm pulling from a string and not a cell, I get an error message "Runtime Error 424: Object Required
Can someone please advise where I'm going wrong?
- Sub Paste_Text_Into_Word_Bookmarks()
- Dim objWord As Object
- Dim ws As Worksheet
- Dim x, BmkCount As Integer
- Dim Text_1, Text_2, Text_3, Text_4, Status As String
- Set ws = ThisWorkbook.Sheets("Sheet1")
- Set objWord = CreateObject("Word.Application")
- objWord.Visible = True
- objWord.Documents.Open "C:\Users\Smudge\Desktop\test.docx" ' change as required
- 'Select text to be pasted into bookmarks
- 'Status is simulated here by manually making x = 1, 2 or 3
- x = 1
- If x = 1 Then Status = "Single"
- If x = 2 Then Status = "Pair 1"
- If x = 3 Then Status = "Pair 2"
- For BmkCount = 1 To 4 'Number of bookmarks in word doc
- If BmkCount = 1 And Status = "Single" Then
- Text_1 = "Text for Bookmark 1."
- ElseIf BmkCount = 1 And Status <> "Single" Then
- Text_1 = "Alternative text for Bookmark 1."
- ElseIf BmkCount = 2 And Status <> "Pair 2" Then
- Text_2 = "Text for Bookmark 2."
- ElseIf BmkCount = 2 And Status = "Pair 2" Then
- Text_2 = "Alternative text for Bookmark 2."
- ElseIf BmkCount = 3 And Status = "Single" Then
- Text_3 = "Text for Bookmark 3."
- ElseIf BmkCount = 3 And Status <> "Single" Then
- Text_3 = "Alternative text for Bookmark 3."
- ElseIf BmkCount = 4 And Status <> "Pair 1" Then
- Text_4 = "Text for Bookmark 4."
- ElseIf BmkCount = 4 And Status = "Pair 1" Then
- Text_4 = "Alternative text for Bookmark 4."
- End If
- Next BmkCount
- With objWord.ActiveDocument
- .Bookmarks("Bookmark_1").Range.Text = Text_1.Value
- .Bookmarks("Bookmark_2").Range.Text = Text_2.Value
- .Bookmarks("Bookmark_3").Range.Text = Text_3.Value
- .Bookmarks("Bookmark_4").Range.Text = Text_4.Value
- End With
- Set objWord = Nothing
- End Sub