PDA

View Full Version : Updating TC fields when a matching text is changed!



arefkr
05-20-2010, 09:15 PM
Hi,

As I am working on a software which reads several Word documents and merges them. Each document has a text (a TextBox) in its Header. When creating the final document, I find that text, create a TC field for that, insert it into the document, update the Table Of Contents. I do it because Word can not create a TOC based on the Header or Footer contents.

This works fine until the header text is modified by the user. If the text is modified, TC fields needs to be updated accordingly. My question is that how can I write a macro that if the text of the text box (which is in Page Header) is changed, the matching TC field is found and its text property is updated? This might happen when the document is saved.

helps are highly appreciated.

fumei
05-21-2010, 09:25 AM
It would be difficult to do the "if" part of:

"that if the text of the text box (which is in Page Header) is changed, "

There is no change event for this, so determining "IF" it is changed is next to impossible.

"This might happen when the document is saved."

That is what I would do. Make the updating of fields automatically done on a document save. You do this by writing your own Sub FileSave. Make sure you include the actual Save instruction!

BTW: I dislike textboxes. Why are you using textboxes in the header like that? And in particular, why are you using textboxes in the header to generate your ToC. This seems very odd to me, but I am willing to learn.

arefkr
05-21-2010, 02:54 PM
Thanks for the reply,

Actually the mentioned document templates belong to a company. The company has a special software written with C# which uses the templates to generate proposals. To be honest, I have not yet figuredout why they have used TextBoxes and why they have put them in the headers! I am a programmer, I have to do what they want.
Anyway, I am a .NET developer and really dumb about VBA. Can U provide me some sample code? where can I find a really quick VBA tutorial? I should finish this requirement by the coming Tuesday :(

Thanks a bunch

Tinbendr
05-21-2010, 07:10 PM
It would help if you could provide a sample file.

But start by looking at this. (http://www.vbaexpress.com/forum/showthread.php?t=31816)

Especially #13.

arefkr
05-21-2010, 10:37 PM
Thank you for the reply. I am new to this forum so I do not have enough post counts to insert a link. can I have your email? please email me to digilux2002@yahoo.com so that I can send you a sample file back.

Thanks

Tinbendr
05-22-2010, 05:07 AM
Oops, sorry, I didn't notice your message count, I believe it's just five posts, and then you can attach files. Reply to this, then maybe introduce yourself here (http://www.vbaexpress.com/forum/forumdisplay.php?f=93).

Take a look at the link in my last message and see if you can figure it out. You'll get lots more help if you try than beg. :)

arefkr
05-23-2010, 03:21 AM
I post this to reach the 5 post credit :)

arefkr
05-23-2010, 03:22 AM
Dear Tinbendr,

Thank you a great deal for the help. I really appreciate it.
you can find a sample file here:

http://rapidshare.com/files/390212439/TOC_EXAMPLE.docx.html

From your point of view, which one would be easier? Changing the text of each TC field, or removing all TC fields and add new ones?
can U write some code for me please? I am really dumb in VBA, but as U know, I can not say No to my boss :)

Tinbendr
05-23-2010, 03:47 AM
Just attach it to the message. At the bottom of the Post Reply, look for Manage Attachments.

arefkr
05-23-2010, 04:51 AM
Here is the sample .docx file

arefkr
05-23-2010, 07:01 PM
Hi,
I wrote the following code which recreates TC fields upon Save. The problem is that Word disables macros by default. since heaps are users will download the macro-enabled template from our SharePoint site, is there a way to make the document template as Macro Enabled so that users will not receive a warning message?



Sub FileSave()
Dim oField As field

' remove existing TC fields
For Each oField In ActiveDocument.Fields
If oField.Type = wdFieldTOCEntry Then
oField.Delete
End If
Next oField

' insert a new TC field for each header text

Dim text As String
Dim lastText As String
Dim s As Section

text = ""
lastText = ""
For Each s In ActiveDocument.Sections
Dim oRange As Variant
text = ""
oRange = s.Range
Dim shp As Shape

For Each shp In s.Headers(wdHeaderFooterPrimary).Range.ShapeRange
If shp.Type = msoTextBox Then
text = """" + shp.TextFrame.TextRange.text + """"
If text <> "" Then Exit For
End If
Next shp
If text <> "" And text <> lastText Then
s.Range.Collapse (WdCollapseDirection.wdCollapseEnd)
Dim temp As Long
temp = s.Range.MoveEnd(WdUnits.wdParagraph, -1)
Dim temp1 As Variant
Dim p As Range
temp1 = s.Range.Paragraphs.Add(s.Range)

Dim f As Variant
f = ActiveDocument.Fields.Add(temp1, WdFieldType.wdFieldTOCEntry, text, False)
lastText = text
End If
Next s ' next section
If ActiveDocument.TablesOfContents.Count > 0 Then
ActiveDocument.TablesOfContents(1).Update
End If
ActiveDocument.Save
End Sub