Excel cell value with space between paragraphs with excel to word bookmark

molesy01

Board Regular
Joined
Dec 23, 2012
Messages
61
Hi
I have a userform in excel that copies text from a cell and puts it within a bookmark in a word template. I have created code which works however the issue i have is that i require more than 1 cell value to populate into a single bookmark with a space between the paragraphs. Hope someone can help. Thanks

sub ExportButton(control As IRibbonControl)

Dim objWord As Object
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Testing")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Activate
objWord.Documents.Open "\\Desktop\Testing.dotm"
With objWord.ActiveDocument
.Bookmarks("RPD").Range.Text = ws.Range("E10").Value
.Bookmarks("RPD").Range.Text = ws.Range("E12").Value
.Bookmarks("RPD").Range.Text = ws.Range("E14").Value

End With

Set objWord = Nothing

End Sub
 
Hi Dave. Thanks for this, it errors on

objWord.ActiveDocument.Bookmarks(i).Range.Text = vbNullString.

It says the range cannot be deleted.
 
Last edited:
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Maybe change this bit...
'clear bookmarks
VBA Code:
For i = LBound(BkMkArr) To UBound(BkMkArr)
objWord.ActiveDocument.Bookmarks(BkMkArr(i)).Range.Text = ""
Next i
 
Upvote 0
Missed the -1...
VBA Code:
'clear bookmarks
For i = LBound(BkMkArr) To UBound(BkMkArr) - 1
objWord.ActiveDocument.Bookmarks(BkMkArr(i)).Range.Text = ""
Next i
 
Upvote 0
Apologies Steve, I forgot how VBA bookmarks work. When you insert text it destroys the bookmark so you need to replace it if you're planning on using it again. Here's the corrected code to trial. Dave
VBA Code:
Sub ExportButton()
Dim objWord As Object, WdStart As Boolean, LastRow As Integer, i As Integer, BCnt As Integer
Dim ws As Worksheet, BkMkCnt As Integer, ShtCnt As Integer, RowCnt As Integer, orng As Object
Dim ShtArr() As Variant, BkMkArr() As Variant, TempStr As String, BKName As String
'sheet names
ShtArr = Array("Section A", "Section B", "Section C", "Section D", "Section E", _
                "Section F", "Section G", "Section H", "Section I")

'start Word app
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
On Error GoTo 0
Set objWord = CreateObject("Word.Application")
WdStart = True
End If
'open template *********change path to suit
objWord.Visible = False
objWord.Documents.Open "C:\Users\Desktop\HBRTest.dotx"

'load bookmarks into an array
ReDim BkMkArr(objWord.ActiveDocument.Bookmarks.Count)
For i = 1 To objWord.ActiveDocument.Bookmarks.Count
BkMkArr(i - 1) = objWord.ActiveDocument.Range.Bookmarks(i)
Next i

'loop sheets
For ShtCnt = LBound(ShtArr) To UBound(ShtArr)
'clear bookmarks
For i = LBound(BkMkArr) To UBound(BkMkArr) - 1
Set orng = objWord.ActiveDocument.Bookmarks(BkMkArr(i)).Range
orng.Text = vbNullString
objWord.ActiveDocument.Bookmarks.Add BkMkArr(i), orng
Next i

'set ws and lastrow "E"
Set ws = ThisWorkbook.Sheets(ShtArr(ShtCnt))
With ws
    LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
End With

'loop every other "E" entry (10 to lastrow)
For RowCnt = 10 To LastRow Step 2
TempStr = CStr(ws.Cells(RowCnt, "E"))
BKName = CStr(ws.Cells(RowCnt, "F"))

'if more than one "E" row in same bookmark make string
For BkMkCnt = (RowCnt + 2) To LastRow Step 2
If CStr(ws.Cells(RowCnt, "F")) = CStr(ws.Cells(BkMkCnt, "F")) Then
TempStr = TempStr & Chr(11) & CStr(ws.Cells(BkMkCnt, "E"))
Else
Exit For
End If
Next BkMkCnt

'put "E" string in bookmark
With objWord.ActiveDocument
'search for bookmark
For BCnt = LBound(BkMkArr) To UBound(BkMkArr) - 1
If .Bookmarks(BkMkArr(BCnt)) = BKName Then
Set orng = objWord.ActiveDocument.Bookmarks(BkMkArr(BCnt)).Range
orng.Text = TempStr
objWord.ActiveDocument.Bookmarks.Add BkMkArr(BCnt), orng
Exit For
End If
Next BCnt
End With

Next RowCnt

'save *********change path to suit
objWord.ActiveDocument.SaveAs2 (ThisWorkbook.Path & "\" & ShtArr(ShtCnt) & ".docx")
Next ShtCnt
'close template/clean up
objWord.ActiveDocument.Close savechanges:=False
If WdStart Then
objWord.Quit
Set objWord = Nothing
End If
End Sub
 
Upvote 0
Hi Dave, thanks again for your hard work, it keeps getting an error highlighted in yellow, orng.Text = vbNullString
 
Upvote 0
Morning Steve. I made up a test doc and trialed that line of code (and inserting text to the bookmark) before posting the last code version. There must be something else going on. Trial adding the message box line of code below to see if the bookmark array is populating. Dave
VBA Code:
'load bookmarks into an array
ReDim BkMkArr(objWord.ActiveDocument.Bookmarks.Count)
For i = 1 To objWord.ActiveDocument.Bookmarks.Count
BkMkArr(i - 1) = objWord.ActiveDocument.Range.Bookmarks(i)
MsgBox BkMkArr(i - 1) '**********ADD THIS LINE
Next i
[CODE=vba]
 
Upvote 0
Morning Dave.
Yes, the bookmark array is populating fine. Ive just been through all of the bookmarks with the MsgBox that pops up and lists them. When it completed the same error came up.
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,948
Messages
6,175,571
Members
452,652
Latest member
eduedu

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