#1
|
|||
|
|||
Update Styleref Fields
Hello Forum,
I'm using Stylereffields like {STYLEREF "TxtStruct 1"\} The Problem, if i Change the Paragraph Style e.g. from TxtStruct 1 to TxtStruct 2 the Styleref field is not updated. Is there way to update those fields? Rgds Thomas |
#2
|
||||
|
||||
Naturally, if you change a paragraph's Style to one not used by the StyleRef field, the StyleRef field will no longer pick up anything to do with that paragraph; you would have to change the Style name in the StyleRef field to overcome that - but then it would ignore all paragraphs in the original Style and would instead reference all paragraphs in the new Style.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hello Paul,
"you would have to change the Style name in the StyleRef field to overcome " Thats what im doing at the moment. I wrote a macro that detects and changes the field.code.text. Problem 1) The user must click on a ribbon button to run the macro. I would Need an Event that detects document stylechanges. Is there any Event in word i could use for that? Problem 2) All fields should be updated as soon as a styleref has been changed. That takes up to 1 or two minutes in large documents. Thats not feasable. The reason is that my stylerefs to be used eg for Tocs and Indexes. I could live with 2) not updating all fields, instead i would implent a macro that Forces ActiveDocument.Fields.Update by ribbon button click or so. Thomas |
#4
|
||||
|
||||
There is no event you can trap for a change of the Style name applied to some content. Frankly, I can't see why you're changing the Style name of the content you want to have showing in the StyleRef field; surely it would be better to retain that - and the presently-applied Style names for the content - and, if necessary, change the Style formatting and use a different Style (perhaps based on the current one) for whatever else you're presently re-assigning the Style to. I also don't understand what your use of StyleRef fields has to do with Tables of Contents or Indexing.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
One of the application are Indexes with Relations to the Style (Headlines e.g.)
Example 2.5 Lorem ipsum dolor sit amet, consetetur sadipscing {XE "sadipscing" \f \"E" \t {STYLEREF "Headlinestyle 2" \w}} elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. If the Headlinelevel changes e.g. to 2.5.1. or to any other Level The Style Reference still says 2.5. That must be corrected for the index The Indexline result should be like this sadipscing........................................ ...2.5 Thomas |
#6
|
||||
|
||||
The following macro should be fairly quick at updating your Index entries:
Code:
Sub IndexUpdater() Application.ScreenUpdating = False Dim i As Long, strStl As String, strRef As String With ActiveDocument For i = 1 To .Fields.Count With .Fields(i) If .Type = wdFieldIndexEntry Then If .Code.Fields.Count = 1 Then With .Code.Fields(1) If .Type = wdFieldStyleRef Then strStl = .Code.Paragraphs(1).Style strRef = Split(Split(.Code.Text, "STYLEREF")(1), Chr(34))(1) If strRef <> strStl Then .Code.Text = Replace(.Code.Text, strRef, strStl) End If End With End If ElseIf .Type = wdFieldIndex Then .Update End If End With Next End With Application.ScreenUpdating = True End Sub .TablesOfContents(1).Update before the final 'End With'. Do note that a Tables of Contents inserted via References|Tables of Contents is far slower at updating than one created by inserting a TOC field. That's because the Tables of Contents inserted via References|Tables of Contents gets inserted in a kind of content control and it's that that imposes the performance hit.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Hello Paul,
thanks the for the Code. This is similar to my approach. The problem will be the performance if you run through the complete document. We have hundreds of fields in very large documents. Rgds Thomas |
#8
|
||||
|
||||
But did you compare the performance?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Yes i did. I've tested the code in my context. The only way to prevent is imo minimizing the update range and toc or fieldupdating seperately. I have a lot of specifications and constraints in this issue because there are only diffrent types of styles concerned and also paragraphs below Headlines without or different styles but including Stylereffields that belongs to the Headline. Quiet complicated, but at the moment it seems to work quiet good. We're still testing.
Thomas |
#10
|
||||
|
||||
Perhaps you should consider pointing all the StyleRef fields to the major Headlinestyle levels only until all the other edits are finished.
Even if there were an event you could trap, I hardly imagine you'd want to wait for perhaps a minute for all the updates to ripple through the document every time you made a Headlinestyle level change somewhere...
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
"Even if there were an event you could trap, I hardly imagine you'd want to wait for perhaps a Minute"
correct, only this part If strRef <> strStl Then .Code.Text = Replace(.Code.Text, strRef, strStl) seems to be uncritical as Long as you operate an assessable range and perform field updates later. In fact, in most cases only some paragraphs are concerned and Need to be updated Example. The user will change the Headline 1.2 to 1.1.3. The update range will be the red area. Only REFSTYLE fields in those paragraphs must be detected and changed. If the user has finished all changes he can click on an update button, to check the results. 1.1 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata 1.2 sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr - justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata - justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren 2 sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet |
#12
|
||||
|
||||
To work with just a selection, all you need do is change:
ActiveDocument to: Selection The index itself can be updated by clicking in it and pressing F9.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Yes, but what if they will select only the first line?
|
#14
|
||||
|
||||
Well, obviously, if you change:
ActiveDocument to: Selection only the selected content will be processed. More complicated code could be used to process the entire range associated with a selection's heading - but only if you're using Word's heading Styles.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
At the moment they have to consider own headlines styles and textstructure styles. I'm using a function to detect those styles. I'm also using the "range" object instead of selection, because the user will change styles by selecting only the first line. If this line (Paragraph beginnig with 1.2) is selcted changed (eg by click on a button) i Loop to all paragraphs until the next concerned style occurs. In my example the Loop runs to the Paragraph beginning with "2". Now i can change the text of the Styleref Fields
Fixing the range: Set ranActRange = ActiveDocument.Range(Start:=ActiveDocument.Paragra phs(intI).Range.Start, End:=ActiveDocument.Paragraphs(x - 1).Range.End) x is the "For" counter of currently checked Paragraph. x-1 because Paragraph x is out of the concernd range that is supposed to be updated. Now i can Change the fields as suggested exactly as in your method. For Each fld In ranActRange.Fields ... .. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I update all fields from a new input | Kozzzle | Word | 7 | 10-19-2017 06:12 PM |
how to update calculated fields | sectionbreak | Mail Merge | 4 | 06-04-2014 12:12 AM |
Macro to update fields | rhatx | Word VBA | 0 | 03-02-2011 12:14 PM |
VBA to update certain (but not all) fields | sparkyrose | Word VBA | 0 | 05-20-2010 12:50 PM |
Can no longer update fields! | slindsay | Word | 0 | 09-03-2009 05:10 PM |