Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-11-2019, 05:06 PM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default Keeping or restoring "Start at" value for heading list after updating styles from a template

I have a macro that I use quite often when other people have been working on a document I created from a template, and have messed up the styles by pasting in things from elsewhere etc: essentially it performs the steps of linking the document to the correct template, ticking the "Automatically update document styles" box, pressing ok, then going back in and unticking the box. This is my standard way to getting the style list back in order.

It falls down when I have a document where the headings don't start at 1 though, which happens when I am doing a tender with (for example) Schedules from 1-10 and they want the numbering for Schedule 3 to start at 3.1. I do this by going into the list style and setting "Start at" to "3", and so on. When I update the styles, this value is set back to 1 and I need to go in and manually change it back to what it should be.

I figured I could get my macro to do this by setting a variable with the current value of "Startat", perform the linking/updating steps, then set it back with the value in the variable. My knowledge of VBA is quite poor: below is my current macro with a lame attempt at new code in blue (which obviously doesn't work) and in red explaining what I want to do: I'm hoping someone can suggest the correct code!


------


Sub ScheduleUpdateStylesFromTemplate()
'
Dim x as long (??)

Set x = ListGalleries(wdOutlineNumberGallery).ListTemplate s(1).ListLevels(1).StartAt
Here I want to set the variable "x" with the current value of "StartAt" for my "Numbered Headings" List Style, so I think I also need to reference that the list style is "Numbered Headings" somewhere??

With ActiveDocument
.UpdateStylesOnOpen = True


.AttachedTemplate = _
"Y:\RFT Template.dotx"
End With
With ActiveDocument
.UpdateStylesOnOpen = False
.AttachedTemplate = _
"Y:\RFT Template.dotx"
End With

With ListGalleries(wdOutlineNumberGallery).ListTemplate s(1).ListLevels(1)
.StartAt = x
End With

Here I want to set the "StartAt" value for "Numbered Headings"to be the value of "x" that I captured before I refreshed the styles

End Sub
Reply With Quote
  #2  
Old 04-11-2019, 11:52 PM
Guessed's Avatar
Guessed Guessed is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

It can be hard to identify which listtemplate you are trying to harness so it makes sense to go direct to the source and identify the right listtemplate by interrogating the list template associated with Heading 1.

I assume you already have the correct template attached so there is a faster way to import the styles from the attached template too.
Code:
Sub ScheduleUpdateStylesFromTemplate()
  Dim x As Long, aLT As ListTemplate
  
  Set aLT = ActiveDocument.Styles("Heading 1").ListTemplate
  
  x = aLT.ListLevels(1).StartAt
    ActiveDocument.UpdateStyles             'reimports styles from the attached template
  aLT.ListLevels(1).StartAt = x

End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 04-14-2019, 03:37 PM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default

Thank you Andrew!

I see that your code for updating the styles is much simpler than what I had, however my documents are all on SharePoint and accessed by lots of people, most of whom don't have access to the template, so it's forever being set back to "Normal". For each job I work on I modify the macro to have the path to the correct template for that job. Perhaps there's a way to code the template path into each file so the macro reads it from there, but that's for another time.

I've put in the steps you wrote, and stepping through it I can see that "x" gets assigned the correct value, however the step "aLT.ListLevels(1).StartAt = x" doesn't set the value. So the result is that the document still ends up with whatever "StartAt" value is set in the template, not the value of "x". Any idea why?

I've just purchased myself a couple of Word VBA courses on Udemy: determined to learn the underlying principles such as why you sometimes need "Set" when filling and variable and sometimes not!
Reply With Quote
  #4  
Old 04-14-2019, 03:55 PM
Guessed's Avatar
Guessed Guessed is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Where is your code stored? If it is in the attached template (where I would put it) then it can only be run when the template is attached and available. Other people opening the document doesn't cause the attached template setting to change (although the attached template may not be available) unless they have additional code changing the template.

The code works on my machine - perhaps your numbered paragraphs have a local override on them. If you select the first Heading 1 in the document and press Ctrl-Q and Ctrl-Space, what number is showing?

If it is still not working, can you post a sample doc I can test on? Make sure you have refreshed the styles from the template.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 04-14-2019, 05:41 PM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default

The code is in my Normal template, along with all my other macros. I tried saving it in the template (and the template as a docm), but same result.

It makes sense that the linked template shouldn't change, but it seems that every time I go back to a document on SP that's had multiple edits, the template is back to "Normal". These people wouldn't be running any code (at least not that they know of!). So I make linking it back to the right template part of the macro.

When I do ctrl+Q and ctrl+space it shows as "Heading 1" all the way through.

Attached is a sample template: not the one I was working on, but has the same result. Click into the line that's styled as Heading 1, Right click list style "Numbered Headings", edit, change "Start At" to 4. "1. Heading 1" becomes "4. Heading 1", which is what I want. Run the macro, and it changes back to "1. Heading 1".

If you look at the "locals" window, it reads the correct value into "x" (in this case, 4), but the last line of code which is supposed to set the value back to "x" once the styles have been updated doesn't change anything: the headings still start at 1.
Attached Files
File Type: dotx Template.dotx (30.8 KB, 8 views)
Reply With Quote
  #6  
Old 04-14-2019, 09:59 PM
Guessed's Avatar
Guessed Guessed is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Ahh, I see you have used a List style and applied it to Heading 1 (so Heading 1 list's settings are effectively being overridden by the list style). I never use list styles because of the extra layer of complexity, but since you have used it, the code needs to be addressing that ListTemplate instead
Code:
Sub ScheduleUpdateStylesFromTemplate()
  Dim x As Long, aLT As ListTemplate  
  Set aLT = ActiveDocument.Styles("Numbered Headings").ListTemplate
  x = aLT.ListLevels(1).StartAt
    ActiveDocument.UpdateStyles             'reimports styles from the attached template
  aLT.ListLevels(1).StartAt = x
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 04-14-2019, 11:22 PM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default

Thank you: that works! In a way. What happens is that the list itself is updated (when I look at it in the gallery, it shows it starting with e.g. 4), but the heading numbers in the document don't change (they stay at 1).

The gallery preview of the "Numbered Headings" list is right and shows "4". But when I go into the gallery itself, even though the numbers are right (4., 4.1, 4.1.1 etc) none of the headings are linked to the levels any more. I had to link headings 1-6 back to the levels again, and then my document numbers updated. I've tried it multiple times with the same result.

I thought, "ok, I'll just make my script do that as well!" so I added in ".LinkedStyle = "Heading 1" etc for all 6 levels and this did indeed leave me with a document with numbering updated, but still when I went into the gallery the styles weren't linked.

This feels like a bug to me: I've had this "de-linking" happen to me before seemingly at random. Perhaps there's a pattern of set of steps that leads to it, but I can't see them. Did it work right for you?
Reply With Quote
  #8  
Old 04-14-2019, 11:40 PM
Guessed's Avatar
Guessed Guessed is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

As far as I'm concerned List styles are buggy and I never use them.

If you dropped the List style and applied a list template to the paragraph styles directly then you would be in with a shot at solving this.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 04-14-2019, 11:57 PM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default

Yes, I'm starting to see why you might go down that path! I think I went the other way based on the famous Shauna Kelly tutorial on numbering as well as a colleague who advised using the custom lists because they're more flexible. But it appears there's no such thing as a free lunch...
Reply With Quote
  #10  
Old 04-15-2019, 12:16 AM
WordUser789 WordUser789 is offline Keeping or restoring "Start at" value for heading list after updating styles from a template Windows 10 Keeping or restoring "Start at" value for heading list after updating styles from a template Office 2016
Novice
Keeping or restoring "Start at" value for heading list after updating styles from a template
 
Join Date: Jun 2017
Posts: 22
WordUser789 is on a distinguished road
Default

Rather than be elegant, I recorded a macro where I went into the list definition, clicked through all numbering levels and pressed OK. Copied that entire wall of text into the end of the macro, changed "1" to "x" and voila! It appears to work now.

Thank you so much for your help on this!!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping or restoring "Start at" value for heading list after updating styles from a template Custom Template, Multi-List Heading Numbers Reverting to Template Rather than Keeping Local Change whsnow Word 2 03-24-2018 06:57 AM
Keeping or restoring "Start at" value for heading list after updating styles from a template Heading styles come "unlinked" from Multi-level list levels after save. spthomas Word 3 11-27-2013 03:55 PM
2 "autonumbers" in 1 list heading? christie Word 0 08-01-2011 08:24 AM
Keeping or restoring "Start at" value for heading list after updating styles from a template How to choose a "List" for certain "Heading" from "Modify" tool? Jamal NUMAN Word 2 07-03-2011 03:11 AM
Word 2007 template "Restart at 1" changes styles BuzzyG Word 0 06-10-2011 01:19 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:05 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