Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-10-2020, 10:43 AM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default Applying heading styles from normal template to all files in a folder

I got the code below from one of Macropod's posts on another thread. I recorded a macro of me applying the Styles to one of the desired files and was hoping I could cut and paste with a little modification to get it to work in Macropod's macro. The entire macro i recorded isn't here, just one relevant part (and the most relevant part of that is bolded).

I had hoped something like "strFolder\strFile" in the place of the bolded section would work, but of course it doesn't. Could one of you do whatever tweaking is necessary to get it work please? Thanks for your help.




Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  If strFolder & "" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc

      'Call your other macro or insert its code here
          With ActiveDocument
        .UpdateStylesOnOpen = False
        .AttachedTemplate = "Normal"
    End With
    Application.OrganizerCopy Source:= _
        "C:\Users\MyName\AppData\Roaming\Microsoft\Templates\Normal.dotm", _
        Destination:= _
        "C:\Users\MyName\Documents\FolderName\FileName.docx" _
        , Name:="Default", Object:=wdOrganizerObjectStyles

      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Reply With Quote
  #2  
Old 10-10-2020, 01:25 PM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

I was hoping (again. But i was actually hopeful this time), that the following version would be successful. Alas...

Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
  If strFolder & "" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
        x = wdDoc.Path & Application.PathSeparator & wdDoc.Name
    With wdDoc

      'Call your other macro or insert its code here
        .UpdateStylesOnOpen = False
        .AttachedTemplate = "Normal"
    Application.OrganizerCopy Source:= _
        "C:\Users\MyName\AppData\Roaming\Microsoft\Templates\Normal.dotm", _
        Destination:="x", Name:="Default", Object:=wdOrganizerObjectStyles

      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #3  
Old 10-11-2020, 04:02 PM
Charles Kenyon Charles Kenyon is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 6,996
Charles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to behold
Default

I have not looked at your code, sorry.
Here is my template that copies heading styles to the Active Document through a macro which is documented in the template.
Outline Heading Styles Global Stylesheet Add-In

You do not need to attach the normal template or any template to copy all of the styles or selected from it.

If using OrganizerCopy method, you should do it for all styles three times if any of the styles are based on another style being copied. (This may be superstition but I've done it that way since Word 97 to avoid problems.)

Here is code to update all styles wiithout using the organizer or attaching the template.

Code:
Private Sub CopyNormalTemplateStylesVogelar()
   ' Copies styles from normal template
   ' 2019-12-01   Hans Vogelar
   ActiveDocument.CopyStylesFromTemplate (NormalTemplate.fullname)
End Sub

Private Sub CopyAllStylesGlobal()
    ' Copies all styles from Global Template - the template containing the macro
    ' 2020-01-12  Charles Kenyon
     ActiveDocument.CopyStylesFromTemplate (ThisDocument.fullname)
 End Sub
Macro to copy styles from one template to another - Microsoft Community

Both Graham Mayor and Greg Maxey have free utilities to batch process documents. Unless you are working on learning to code and enjoy it, you may want to use their utilities.

Here is the MVP article on batch processing. You seem to be following a similar path. How to Find & ReplaceAll on a batch of documents in the same folder by Ibby.

Last edited by Charles Kenyon; 10-27-2020 at 07:29 AM.
Reply With Quote
  #4  
Old 10-11-2020, 04:30 PM
Guessed's Avatar
Guessed Guessed is online now Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,985
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

Is it just the heading styles you want to import or is it all styles?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 10-11-2020, 08:50 PM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Smile

Many thanks Charles,
It's a pity you didn't look at the code I provided. It was almost all Macropod's, just a small part in the middle from me. I reckon someone like you could have fixed it directly in a lot less time than it took you to type your post. But I suppose your thinking is that one of those other options would take care of numerous future problems for me.

However I've been at those link locations before - although what's offered may be very useful in some areas; from memory, the Find and Replace function is very drastically reduced whereas with Macropod's macro you can use Find and Replace fully functional (just record or code what you want it to do and then place it in the middle of Mac's macro). As far as I can tell, the code I have is far more useful (for Find/Replace at least).

I used the 2 lines of code you provided to get my (Macropod's) code working. Here it is for anyone who may benefit (and it seems from the thread views that some are interested):


Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName: strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
If strFolder & "" & strFile <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc

'Call your other macro or insert its code here
.CopyStylesFromTemplate (NormalTemplate.FullName)
.CopyStylesFromTemplate (ThisDocument.FullName)


.Close SaveChanges:=True
End With
End If
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder( 0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function


Andrew, nice to hear from you again after my promised holiday

I only need the heading styles at the minute, but it doesn't bother me that all styles are copied. But if you know how to differentiate the two then it may well come in useful later.

Thank you both.
Reply With Quote
  #6  
Old 10-11-2020, 10:37 PM
macropod's Avatar
macropod macropod is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,563
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

See: https://www.msofficeforums.com/28497-post34.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 10-13-2020, 10:24 AM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

Many thanks Macropod,

and thanks for your original contribution. Most helpful
Reply With Quote
  #8  
Old 10-27-2020, 01:51 PM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

I posted this earlier with an extra sentence or two that someone apparently didn't like, so the entire post (and Charles' reply) was removed without notice. I've removed the extra. I can't see how anyone could complain about the remainder and it seems perfectly legitimate; so here it is:

I've tried the Organiser method (including pressing the button 3 times); both one-line codes that Charles gave me, and his Global stylesheet Addin; I used Macropod's "StyleCopier"; and I've tried messing around with various different settings - deleting or hiding unwanted styles and re-prioritising the Styles. But despite it all - one or more of 3 styles ("No-spacing", "Normal", and "underline") keep turning up in the Gallery.

How do i get rid of the 3 mentioned problem styles? Or is a macro required that searches the first 9 styles, and removes them if they're not heading styles?

Thanks for your help.
Reply With Quote
  #9  
Old 10-27-2020, 02:10 PM
macropod's Avatar
macropod macropod is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2010
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,563
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

You cannot delete any of Word's built-in Styles. Hence they will always be present in the Gallery. You can, of course, change their Gallery priority.

Your other post (and reply) have not been deleted. Someone split them to a separate thread: https://www.msofficeforums.com/word/...confusion.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 10-27-2020, 03:16 PM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

Thanks Paul,
Quote:
Your other post (and reply) have not been deleted. Someone split them to a separate thread:
I don't mean any offence, but a note should really have been left letting me know that. I don't see that the thread should have been split; it was a continuation of the previous posts and was VBA related (and still is, as we see from Charles' provision of relevant VBA codes in the other/new thread). Splitting a thread just because a not-strictly-VBA subject is mentioned doesn't appear to help matters. Should they be rejoined now that Charles has provided VBA codes?

Quote:
You cannot delete any of Word's built-in Styles. Hence they will always be present in the Gallery. You can, of course, change their Gallery priority.
I pretty much figured that in the meantime, however I assumed there must be some kind of glitch in my case otherwise someone (I reasoned) would certainly have mentioned it when I was being given the various methods/codes/macros/Add-ins. I thought it was clear I was trying to apply new styles settings to old documents - why would I want to apply the changes to "All files in a folder" if the files weren't old?

So the priority settings in the old documents remains and overrides my attempts to copy all-new settings. There's no problem with new documents.

I assume applying one of the two codes given me by Charles in the other thread will solve that part of the problem:
Code:
activedocument.Styles("Normal").Priority = 99
activedocument.Styles("Normal").QuickStyle=False
Thanks for your help once again.

Now to try and understand templates and list templates...
Reply With Quote
  #11  
Old 10-27-2020, 03:16 PM
Guessed's Avatar
Guessed Guessed is online now Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,985
Guessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to allGuessed is a name known to all
Default

When you copy styles, you get everything about those styles in the document. This includes the visibility, priority etc.

You can either configure the styles exactly as you want in the templates so that the settings get imported OR you can run more code to undo the settings you didn't want after you import the styles. For instance, you can hide any styles by setting their .Visibility property to True (yes, it is backwards - .Visibility = True means that the style doesn't appear in your styles list)

Normal is a special built-in style but "underline" and "No-spacing" would be either custom styles (or aliases added to a built-in style depending on your settings).

Note that running two consecutive lines to import styles from two different templates has some gotchas. The following lines would give you all the style definitions from ThisDocument + the aliases and custom styles from NormalTemplate. If the custom styles in NormalTemplate don't exist in ThisDocument BUT they are based on styles that do, then those styles may not appear the same as they do in NormalTemplate.

.CopyStylesFromTemplate (NormalTemplate.FullName)
.CopyStylesFromTemplate (ThisDocument.FullName)

Fiddling the priority settings only make sense if you have chosen to display the styles in that order (by setting options to display list order 'As Recommended'). If you are showing styles in alphabetical order then the style's priority setting is ignored. Also, any style with the same priority level will fall into alphabetical order anyway so just setting Normal to 99 doesn't mean much if all the other styles are also set to 99.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 10-27-2020, 03:38 PM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

Thank-you Andrew,
Quote:
Note that running two consecutive lines to import styles from two different templates has some gotchas.
Yes, in my ignorance I had initially run the two codes as though they were one. But I had figured it out and was running them separately with similar results.

Many thanks for your other comments as well, which I'll consider and reply to as soon as I can.
Reply With Quote
  #13  
Old 10-27-2020, 06:24 PM
Charles Kenyon Charles Kenyon is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 6,996
Charles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to behold
Default

Quote:
Originally Posted by John 4 View Post
I don't mean any offence, but a note should really have been left letting me know that. (the thread had been split)
I am the someone who split the thread. Obviously you found it. I thought that if you tried to respond to my response, you would see the new thread in the general Word forum. The original post was about applying styles from the normal template to all files in a folder using vba. This was answered but you had questions about deleting styles and about how templates work. Those do not seem to be vba questions and might better be answered in the general thread.

Answers to those questions might be more easily found by others with similar questions looking in the future in that thread. I understand that it has to do with the same problem in your work with Word, but it really did not seem like a vba question.

I put a link back to this thread in case anyone wanted the context in which you were asking.

I hope I answered your questions about templates, or at least gave you the resources to understand how they work in the new thread.
Reply With Quote
  #14  
Old 11-10-2020, 01:22 AM
John 4 John 4 is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2013
Advanced Beginner
Applying heading styles from normal template to all files in a folder
 
Join Date: Oct 2019
Posts: 56
John 4 is on a distinguished road
Default

My apologies for taking so long to reply. I've been busy reading some of the many links kindly provided by Charles, among other things.
Quote:
I am the someone who split the thread. Obviously you found it.
I only found it (after thinking for many hours that it had been deleted because someone didn't like it) because Paul informed me, but we'll not argue about it.

Adding the codes you've provided to push the "Normal" and "No-spacing" styles down the styles list has worked well. The "underline" style (I remember now) I added a few years ago myself when i was experimenting. If there's a problem document, I've found that Charles' advice in one of his articles to copy and paste to a new document based on the desired template (in my case usually the Normal template), works well for getting rid of undesired styles and the like.

I'll keep studying so that i have to bother you as little as possible, but I'll likely be back before long. In the meantime, thanks again to all of you for your help.
Reply With Quote
  #15  
Old 11-13-2020, 12:11 PM
Charles Kenyon Charles Kenyon is offline Applying heading styles from normal template to all files in a folder Windows 10 Applying heading styles from normal template to all files in a folder Office 2019
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 6,996
Charles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to beholdCharles Kenyon is a splendid one to behold
Default

You are welcome.

I apologize. I am still learning this moderator business. I have learned, through this experience with you, to first move the thread and then respond. That way you get a message about the new location. I thought it was automatic but was mistaken.

There are five separate Word forums here, as well as forums on other Office applications. There is no requirement that posts go in something other than the general Word forum but using the specialized ones can get more focused responses. On the other hand, posting a question about mail merge in the tables forum is likely to get the question moved.

Usually when you want to ask a new, even if tangentially related, question it is a good idea to start a new thread. That way it will get the most attention from those able to answer. There are a host of good experienced users who answer questions here. Many read the titles of threads to decide if they can help. Even if you put a different title on your post in the old thread, they will not see it.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Applying heading styles from normal template to all files in a folder Normal.dot Relationship of document and template. Durability of Styles when doc sent. crw1@y7mail.com Word 6 07-29-2020 03:08 PM
Keeping or restoring "Start at" value for heading list after updating styles from a template WordUser789 Word VBA 9 04-15-2019 12:16 AM
Styles and not applying correctly GinnyBcore Word 1 10-02-2018 03:18 PM
Applying a new theme to a new template in 2016 moves graphics in the template around dianahbr PowerPoint 0 02-27-2018 11:04 AM
Applying heading styles from normal template to all files in a folder Movable Word template with linked files and hidden folder Elmobram22 Word 6 11-15-2013 02:11 PM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 02:43 PM.


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