I want to update all the cross-references on an existing Word Document using a VB.net application.
For example, my word document (.docm)(I write comments between /-- --/ for understanding) :
My Document title : TEST TO UPDATE
/--this title is in the BOOKMARK named "TITLE1"--/
The document title is : { REF TITLE1 \h }
/--{ REF TITLE1 \h } is the code for the cross-reference that refers to the bookmark "TITLE1"--/
/--This text is in the header of my document--/
I use a VB.net application to change the document title :
Below my code :
Imports Microsoft.Office.Interop
Public Sub UpdateWord()
Dim oWord As Word.Application
Dim oDoc As Word.Document
'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = False
oDoc = oWord.Documents.Open(Path_Word_Document)
oDoc.Bookmarks.Item("TITLE1").Range.Text = "My New Title"
oWord.Documents.Save()
oWord.Documents.Close()
oWord.Quit()
End Sub
When I launch this sub, my document title is updated, but the cross-reference keep the old title value.
Do you know how I have to do to update the cross-reference in my VB.net sub.
Thanks
I use MS Word 2010, Visual Studio 2010 (.NET Framework 3.5) on Win7.
oDoc.Fields.Update()
should work. Are you saying that if you write this line right belowoDoc.Bookmarks.Item("TITLE1").Range.Text = "My New Title";
close the app and open the Word document manually, it is not updated? Have you tried writingoDoc.Save
? – varocarbas