Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-19-2017, 09:41 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 10-19-2017, 02:14 PM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 10-20-2017, 01:12 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 10-20-2017, 04:09 AM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 10-20-2017, 08:57 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 10-20-2017, 02:10 PM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
As for your Tables of Contents, that may or may not need updating. The code for that, if you need it, is as simple as adding:
.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]
Reply With Quote
  #7  
Old 10-21-2017, 02:53 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

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
Reply With Quote
  #8  
Old 10-21-2017, 03:47 AM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by datech-hh View Post
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.
But did you compare the performance?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 10-21-2017, 05:12 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

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
Reply With Quote
  #10  
Old 10-21-2017, 05:26 AM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 10-21-2017, 05:55 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

"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
Reply With Quote
  #12  
Old 10-21-2017, 05:59 AM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 10-21-2017, 06:01 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

Yes, but what if they will select only the first line?
Reply With Quote
  #14  
Old 10-21-2017, 06:05 AM
macropod's Avatar
macropod macropod is offline Update Styleref Fields Windows 7 64bit Update Styleref Fields Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #15  
Old 10-21-2017, 07:03 AM
datech-hh datech-hh is offline Update Styleref Fields Windows 10 Update Styleref Fields Office 2016
Novice
Update Styleref Fields
 
Join Date: Oct 2017
Posts: 9
datech-hh is on a distinguished road
Default

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
...
..
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Update Styleref Fields 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

Other Forums: Access Forums

All times are GMT -7. The time now is 12:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft