![]() |
|
#1
|
|||
|
|||
|
Greetings to all, I am trying to copy styles from a template only if that style is not present in Activedocument. I am using following code: Code:
Function styleimporter(st1 As String, st2 As String)
Dim i As Integer
On Err.Number = 5834 GoTo exx
Dim a()
a = Array(st1, st2)
For i = LBound(a) To UBound(a)
On Error GoTo exx
With ActiveDocument.Range.find
.ClearFormatting
.Replacement.ClearFormatting
.Style = a(i)
.Execute
exx:
If .Execute = False Then
Application.OrganizerCopy _
Source:="C:\Users\aspKamal\AppData\Roaming\Microsoft\Templates\NecessaryStyles.dotm.docx", _
Destination:=ActiveDocument.FullName, _
name:=a(i), _
Object:=wdOrganizerObjectStyles
End If
End With
Next
End Function
Thanks, Bikram |
|
#2
|
||||
|
||||
|
Perhaps:
Code:
Sub AddStyles()
Application.ScreenUpdating = False
Dim DocSrc As Document, DocTgt As Document, Stl As Style, StrStls As String, i As Long
StrStls = "|"
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
.Title = "Select the source document containing the required Styles"
.AllowMultiSelect = False
If .Show = -1 Then
Set DocSrc = Documents.Open(.SelectedItems(1), ReadOnly:=True, AddToRecentFiles:=False)
Else
MsgBox "No source file selected. Exiting", vbExclamation
Exit Sub
End If
End With
With Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
.Title = "Select the document to be updated"
.AllowMultiSelect = False
If .Show = -1 Then
Set DocTgt = Documents.Open(.SelectedItems(1), ReadOnly:=False, AddToRecentFiles:=True)
Else
MsgBox "No target file selected. Exiting", vbExclamation
DocSrc.Close SaveChanges:=False
Set DocSrc = Nothing
Exit Sub
End If
End With
For Each Stl In DocSrc.Styles
If Stl.BuiltIn = False Then StrStls = StrStls & Stl.NameLocal & "|"
Next
For Each Stl In DocTgt.Styles
If Stl.BuiltIn = False Then StrStls = Replace(StrStls, "|" & Stl.NameLocal & "|", "|")
Next
For i = 1 To UBound(Split(StrStls, "|")) - 1
Application.OrganizerCopy Source:=DocSrc.Fullname, Destination:=DocTgt.Fullname, Name:=Split(StrStls, "|")(i), Object:=wdOrganizerObjectStyles
Next
DocTgt.Close SaveChanges:=True: DocSrc.Close SaveChanges:=False
Set DocSrc = Nothing: Set DocTgt = Nothing
Application.ScreenUpdating = True
MsgBox "Successfully added missing Styles"
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Thank you very much, Macropod.
|
|
#4
|
|||
|
|||
|
Thank you!
I tried the code, and it worked like a charm. This will be very helpful, indeed. Thank you, again. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Importing styles using Templates | Bikram | Word VBA | 2 | 08-31-2021 10:40 PM |
| Getting rid of Styles and Templates confusion | John 4 | Word | 1 | 10-27-2020 07:25 AM |
| Using Styles in Templates to help format text - Importing and working with Templates | daithy | Word | 2 | 01-03-2020 05:06 PM |
Importing Styles causing odd formatting problems.
|
Red Pill | Word | 3 | 06-12-2012 06:19 AM |
Importing Templates in Word 2007
|
rbilleaud | Word | 4 | 06-29-2011 05:51 PM |