0
votes

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.

1
From this code is not clear to me what you want to do. You are opening an existing file and intending to update its references? But why are they not working? As you can see in this link (stackoverflow.com/questions/14299292/…), Fields.Update is meant for other thing: creating a given reference and "allow it to start working". Can you please elaborate a bit regarding the exact problem (are the references working if you open the file manually, for example?).varocarbas
I change the explains. I hope this modification will help you to understand my problem. If, manually, I open my word document and I select the cross-reference and I press F9, the update works.Chalumeau
As shown in the aforementioned link, under these conditions (adding a given bookmark at runtime), oDoc.Fields.Update() should work. Are you saying that if you write this line right below oDoc.Bookmarks.Item("TITLE1").Range.Text = "My New Title"; close the app and open the Word document manually, it is not updated? Have you tried writing oDoc.Save?varocarbas
Ok, I forgot one detail which I think its important. The Cross-reference is in a header.Chalumeau
Yes, it is VERY important. Now I am doing something else. I can do some tests in a while and let you know if I find something.varocarbas

1 Answers

2
votes

Finally I find the solution.

Please let me show the code:

Imports Microsoft.Office.Interop

Public Sub UpdateWord()
    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim oHeader As Word.HeaderFooter
    Dim oSection As Word.Section

    'Start Word and open the document template.
    oWord = CreateObject("Word.Application")
    oWord.Visible = False
    oDoc = oWord.Documents.Open(Path_Word_Document)

    For Each oSection In oDoc.Sections
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
                For Each oField In oHeader.Range.Fields
                    oField.Update()
                Next oField
            End If
        Next oHeader
    Next oSection


    oWord.Documents.Save()
    oWord.Documents.Close()
    oWord.Quit()
End Sub  

Thanks you Varocarbas for your help.