#1
|
|||
|
|||
Update fields and Tables of Content with one macro
Hi guys, new to this forum and new to vba. I have a user form with a TOC and a field section that needs to be updated often. I wrote some VBA in Mac and it works fine, however when I transfer over to Windows the code does not work-I'm stumped here's the code-
Sub UpdateFields() ' ' UpdateFields Macro ' ' If ActiveDocument.ProtectionType = wdNoProtection Then ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End If If ActiveDocument.ProtectionType <> wdNoProtection Then Selection.Range.Sections.First.Range.Select Selection.Fields.Update End If With ActiveDocument .Unprotect ActiveDocument.TablesOfContents(1).Update End With With ActiveDocument .Protect wdAllowOnlyFormFields, NoReset:=True End With End SubÏ This runs great in Mac but does not work in Windows and gives me an error code of "The requested member of the collection does not exist". Thanks for your help- |
#2
|
||||
|
||||
The following will unprotect a form, and update all the fields, including tables of contents
Code:
Sub UpdateAllFields() Dim oStory As Range Dim oTOC As TableOfContents Dim bProtected As Boolean If ActiveDocument.ProtectionType <> wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If For Each oTOC In ActiveDocument.TablesOfContents oTOC.Update Next oTOC For Each oStory In ActiveDocument.StoryRanges oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange oStory.Fields.Update Wend End If Next oStory Set oStory = Nothing If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, _ NoReset:=True, _ Password:="" End If lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
The code works, but needs to be run twice. The first time thru the field section updates and the TOC either adds or subtracts the fields. Only with the second run does the TOC reorder-Thanks
|
#4
|
|||
|
|||
Also just noticed that the macro wiped out all of the form field information.
|
#5
|
||||
|
||||
Put the TOC code after the main code. You don't need to unprotect the form to update the fields.
Code:
Sub UpdateAllFields() Dim oStory As Range Dim oTOC As TableOfContents Dim bProtected As Boolean For Each oStory In ActiveDocument.StoryRanges oStory.Fields.Update If oStory.StoryType <> wdMainTextStory Then While Not (oStory.NextStoryRange Is Nothing) Set oStory = oStory.NextStoryRange oStory.Fields.Update Wend End If Next oStory Set oStory = Nothing If ActiveDocument.ProtectionType <> wdNoProtection Then bProtected = True ActiveDocument.Unprotect Password:="" End If For Each oTOC In ActiveDocument.TablesOfContents oTOC.Update Next oTOC If bProtected = True Then ActiveDocument.Protect _ Type:=wdAllowOnlyFormFields, _ NoReset:=True, _ Password:="" End If lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
Thanks a lot Graham, works great. BTW, why did the other code work in Mac and not in Windows??
|
#7
|
||||
|
||||
Actually it does work. The problem is the sub name. Change that to UpdateAllFields or UpdateMyFields and it works.
I should have thought of this earlier, but a cut and paste job was quicker.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#8
|
|||
|
|||
Actually because I work mostly in Mac, I created the doc in Mac and ported over to Windows. The Mac version worked with the code I posted above, and the only thing I had to do for the Windows version was to create QAT icons, the macros were the same. So I don't understand why there had to be such a big change in the code to make it work. I have noticed slight differences in usage of punctuation between Mac and Windows, but other than that most of the times code is a copy and paste gig-Thanks for your time-Brent
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Forms fields: edit content easily in tables | kirby gilman | Word | 8 | 12-04-2014 08:43 AM |
Macro to select and update all tables in document | CircleG | Word VBA | 6 | 10-17-2014 07:25 PM |
Auto Update Fields Macro | kveldulv | Outlook | 0 | 06-07-2013 02:30 PM |
macro to update fields | PeaceDove | Word | 3 | 01-17-2012 02:45 PM |
Macro to update fields | rhatx | Word VBA | 0 | 03-02-2011 12:14 PM |