How I can re-apply all styles to a document programmatically?

A

aushknotes

Hi,

I need to re-apply all styles to a document programmatically after copying
all styles from a template? Is there a single method to do so? If not, how
can I do it with the shortest VBA codes? I tried range.select & then send
sendkeys CTRL+Q or CTRL+Space but both did not work as required.

Many thanks in advance!
 
G

Graham Mayor

With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = "D:\Path\Template.dot"
End With

The above will load your template and update the styles of the same name. If
you want to go beyond that and remove manual formatting, then the following
should do that and adds the facility to pick the template.

Neither function will do anything about stylenames that don't match.

Sub ChangeMyTemplate()
Dim oRng As Range
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.Title = "Select Template to Attach and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User", , "Select Template"
Exit Sub
End If
sTemplate = fDialog.SelectedItems.Item(1)
End With
With ActiveDocument
.UpdateStylesOnOpen = True
.AttachedTemplate = sTemplate
End With
For Each oRng In ActiveDocument.StoryRanges
Do Until oRng Is Nothing
With oRng
.WholeStory
.Font.Reset
End With
Set oRng = oRng.NextStoryRange
Loop
Next
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

aushknotes

Many thanks for your suggestion. However I have been using the same method as
suggested. I also tried the organizercopy function to copy individual style
across.

The problem I am having is that once I copy the styles from the attached
template, some paragraph styles have changed unwantedly. For example, I have
some paragrahs formatted with "Heading x" without hanging indent, but once I
update styles from template, some remains without indented (which is what I'm
expecting), but some paragraghs (only some) changed to indented (which is not
I'm expecting). I checked the style definitions in the template & they all
look fine to me.

This is why I am posting the question to see if there is any VBA method that
can re-apply all the styles in the document AFTER I copy or update the styles
from the attached template.

Hope the additional information clarifies what exactly what I'm after.

Have a nice weekend, guys!
 
G

Graham Mayor

The method suggested should work, however to doubly enforce the point add
the line

..ParagraphFormat.Reset
immmediately after
..Font.Reset

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

aushknotes

Hi Graham,

I'm really confused now because I did tried .ParagraphFormat.Reset last
Friday without success. However it seems OK today. Puzzled???

FYI, .font.reset won't work as it reset all my bold, italic or underline.

Anyway, I think I have to leave this issue aside.

Many thanks for your precious suggestions & wish you all the best.
 
G

Graham Mayor

aushknotes said:
FYI, .font.reset won't work as it reset all my bold, italic or
underline.

Of course it will - that's what happens when you use manual formatting
instead of styles.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top