Using Bookmarks to Populate Word BUT Style Problem

Josh_

New Member
Joined
Sep 17, 2015
Messages
22
I have been trying to find a solution to my roblem, BUT can not find it anywhere.

Simply put, I have data (a list) I want to put into a word document. However whenever my text is inserted to the bookmark the Word style automatically reverts the the "normal" style and not to the style I save the bookmark in.

I'm sure it's just a simple thing of selecting the style I want before I insert the text... but I'm going mad trying to find it.

This is my code:

Sub test1()

Dim objWord As Object
Dim ws As Worksheet
Dim ScopeItem, Amendments, SOWDate As String
Dim i, n As Long
'Dim SOWDate As Date


Set ws = ThisWorkbook.Sheets("02050")
Set objWord = CreateObject("Word.Application")


i = ws.Range("B" & Rows.Count).End(xlUp).Row
SOWDate = Format(Now, "MMM dd, YYYY")
ScopeItem = Chr(149) & " All scope items as per drawings and specifications, including" & ws.Range("B3").Value & Chr(13)


For n = 9 To i
ScopeItem = ScopeItem & Chr(149) & " " & ws.Range("B" & n).Value & Chr(13)
Next n


objWord.Visible = True
'objWord.Documents.Open "C:\Users\Josh\OneDrive\SOW Manager\Scope of Work Template V1.0 TEST.dotx" ' change as required
objWord.Documents.Open "C:\Users\Eustung\OneDrive\SOW Manager\SOW Template V1.0.dotm" ' change as required



With objWord.ActiveDocument
.Bookmarks("Scope").Range.Text = ScopeItem 'But not in "normal" style! In my "List" style
.Bookmarks("ProjectName").Range.Text = ws.Range("B1").Value
.Bookmarks("ProjectNumber").Range.Text = ws.Range("B2").Value
.Bookmarks("TradeName").Range.Text = ws.Range("B6").Value
.Bookmarks("CostCode").Range.Text = ws.Range("B4").Value
.Bookmarks("DivisionNameHeader").Range.Text = ws.Range("B5").Value
.Bookmarks("DivisionName").Range.Text = ws.Range("B5").Value
.Bookmarks("SOWDate").Range.Text = SOWDate
End With

Set objWord = Nothing


End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
At a guess, you Word bookmark spans a paragraph break and you're overwriting that, with the result you're obliterating the Style. Try replacing:
Code:
For n = 9 To i
 ScopeItem = ScopeItem & Chr(149) & " " & ws.Range("B" & n).Value & Chr(13)
 Next n
with:
Code:
For n = 9 To i
  ScopeItem = ScopeItem & Chr(149) & " " & ws.Range("B" & n).Value
  If n < i Then ScopeItem = ScopeItem & vbCr
 Next n
and replacing:
Code:
.Bookmarks("Scope").Range.Text = ScopeItem
with:
Code:
.Bookmarks("Scope").Range.InsertBefore ScopeItem
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

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