title: Updating Bookmarks in word with vba
body: <p>The following program attempts to generate a report from a word template. It will generate a new report or open an existing one if it already exists. I would like my users to be able to update the bookmarks in this report but they are being copied over. I found another thread on this site that discussed how to duplicate and replace the bookmarks and inserted it in my code below. The code is running without any errors but the bookmarks don't seem to be updating. When I run the code the second time on the added document the code breaks and I get run-time error '462: The remote server machine does not exist or is unavailable and highlights the first line of code that inserts values to the word bookmarks. I am assuming this is because the bookmark no longer exists. I'm a real newbie so maybe its something real simple. I appreciate any and all assistance. </p>
  
  <pre><code>Set wdApp = CreateObject("word.application")
  
  FilePath = Application.ThisWorkbook.Path &amp; "\" &amp; "WriteUp Template " &amp; ActiveSheet.Name &amp; ".docx"
  
  If Dir(FilePath) &lt;&gt; "" Then
  
  With wdApp
  .Visible = True
  .Activate
  .documents.Open Application.ThisWorkbook.Path &amp; "\" &amp; "WriteUp Template " &amp; ActiveSheet.Name &amp; ".docx"
  End With
  Else
  With wdApp
  .Visible = True
  .Activate
  .documents.Add Application.ThisWorkbook.Path &amp; "\" &amp; "WriteUp Template.docx"
  End With
  End If
  
  
   For Each xlName In Excel.ThisWorkbook.Names
  
  'if xlName's name is existing in document then put the value in place of the bookmark
  If wdApp.ActiveDocument.Bookmarks.Exists(xlName.Name) Then
      'Copy the Bookmark's Range.
      Set BMRange = wdApp.ActiveDocument.Bookmarks(xlName.Name).Range.Duplicate
      BMRange.Text = Range(xlName.Value)
      'Re-insert the bookmark
      wdApp.ActiveDocument.Bookmarks.Add xlName.Name, BMRange
  End If
  
  Next xlName
  
  
  
  'Insert title of Company
  
  Set CompanyTitle = Range("B1:B20").Find("Cash Flow", , , , , , False).Offset(0, 1)
  wdApp.ActiveDocument.Bookmarks("CompanyTitleCF").Range = CompanyTitle.Value
  </code></pre>