Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 21st, 2010, 01:22 AM
Registered User
 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Updating TC fields when a matching text is changed!

Hi,

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.

This link describes the source of this requirement:

http://social.msdn.microsoft.com/For...6-1dd64a1eeee4

helps are highly appreciated.
Reply With Quote
  #2 (permalink)  
Old May 21st, 2010, 10:52 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Just a thought! How will it be if you loop through all the TextBoxes in the headers of document and re-create TC field and then update TOC. This will be easy if the no of textboxes are less.

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #3 (permalink)  
Old May 22nd, 2010, 02:29 AM
Registered User
 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks mate,
The biggest problem is that I am a C# developer and I do not know VBA :(
BTW, if I create new TC fields, how can I remove the old ones? can U provide me some sample codes?
you can find a sample file here:

http://rapidshare.com/files/39021243...MPLE.docx.html

Last edited by arefkr; May 22nd, 2010 at 02:38 AM.
Reply With Quote
  #4 (permalink)  
Old May 22nd, 2010, 11:06 PM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Here is a simple way to remove all TC fields in the document

Code:
Sub Remove_TC_Fields()
Dim oField As Field
For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldTOCEntry Then
        oField.Delete
    End If
Next oField
End Sub
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #5 (permalink)  
Old May 22nd, 2010, 11:22 PM
Registered User
 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Shasur View Post
Hi

Here is a simple way to remove all TC fields in the document

Code:
Sub Remove_TC_Fields()
Dim oField As Field
For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldTOCEntry Then
        oField.Delete
    End If
Next oField
End Sub

Cheers
Shasur

Thanks a lot for the code, how can I insert a Macro into a Word Template Document?
Seems that I might be able to translate my current C# code to VBA. I will give it a try and will be back to you if I get stuck :)
Reply With Quote
  #6 (permalink)  
Old May 24th, 2010, 07:05 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Press Alt + F11 in the Document Template. This will open the VBE (Visual Basic Editor). You can see the document and its project listed on the leftpane.

Add a new module and add the necessary code to that

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #7 (permalink)  
Old May 24th, 2010, 08:13 PM
Registered User
 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. I wrote my first VBA macro ever and it worked like a charm :)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating Dates and Changing two other fields arholly Access VBA 11 December 7th, 2006 01:31 PM
Updating Excel with Form fields nancy Classic ASP Professional 6 April 7th, 2006 03:18 PM
updating text fields with option buttons redtechcoms Access VBA 7 February 2nd, 2006 03:14 PM
using textbox keypress/text changed bazookajoe76 C# 5 October 10th, 2004 01:03 AM
Updating table fields ppenn Access VBA 2 September 20th, 2003 07:09 PM



All times are GMT -4. The time now is 05:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.