PDA

View Full Version : VBA Code Updating Word Doc Bookmarks Data to Push New Data from Excel Cells



ak_254
03-15-2018, 12:44 PM
Hi all,


I currently have a Word Document with multiple Bookmarks in place that I use to push data from an Excel Cell range to fill in via VBA Macro.


Since the data in the Excel Cell range is dynamic and consistantly changing I will need to be able to update the text in these Bookmarks on a continous basis.


I am currently able to push data out from a Cell in Excel to a Bookmark in Word but need a way to clear/replace the text in the Bookmarks from a previous update.


Any help or point in the right direction would be greatly appreciated!




The VBA code I am currently running in Excel:




Sub Update_Catalog_Data()


' Update Accessory Stock Status in Catalog


Dim objWord As Object
Dim ws As Worksheet


Set ws = ThisWorkbook.Sheets("Accessory List")


Set objWord = CreateObject("Word.Application")


objWord.Visible = True


objWord.Documents.Open "C:\Desktop\Accessory Catalog.docx"


With objWord.ActiveDocument

.Bookmarks("Store1").Range.Text = ws.Range("A10").Value
.Bookmarks("Store2").Range.Text = ws.Range("B10").Value


End With

Set objWord = Nothing


End Sub

Dave
03-16-2018, 05:03 AM
Hi ak_54 and welcome to this forum. U can trial this. HTH. Dave

Dim ORng As Object
With objWord.ActiveDocument
If .Bookmarks("Store1").Exists Then
Set ORng = .Bookmarks("Store1").Range
ORng.Delete
ORng.Text = CStr(ws.Range("A10").Value)
.Bookmarks.Add "Store1", ORng
End If
End With
Set ORng = Nothing

ps. Please use code tags

mana
03-16-2018, 06:00 AM
https://wordmvp.com/FAQs/MacrosVBA/InsertingTextAtBookmark.htm