I'm trying to create a process in an Excel addin to 1) open a Word document, 2) search the document for bookmarks, then 3) insert the value from an Excel range that is named the same as the Word bookmark.
I'm hoping that I can get this to be done within Excel code and not any Word VBA. I also want this code to be pretty generic to be used with documents of varying numbers of bookmarks. So I would like to use variables and loops to the fullest extent possible.
Here's what I have so far that isn't working for me:
Sub WordBookmarks() Dim strDoc As String Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim xlRange As Excel.Range strDoc = Range("c:\temp\docfile.docx").Value Set wdApp = New Word.Application With wdApp .Visible = True .WindowState = wdWindowStateMaximize End With With wdApp .Documents.Open (strDoc) For Each n In ActiveDocument.Bookmarks .Bookmarks.Item(n.Name).Range.InsertAfter Application.Range(n.Name) Next End With End Sub
This is crashing on the .bookmarks.Item(n.Name) command, but I'm not certain the rest of the process works properly either.