View Single Post
 
Old 06-11-2022, 10:39 PM
Bikram Bikram is offline Windows 10 Office 2007
Advanced Beginner
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default Importing styles using Templates only if style is not present already

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
Here, It seems that in order to search for a style in a document, the style should be present on Activedocument otherwise it encounters error 5834 "Item with the specified name does not exist". I tried to handle it with "on error goto exx" but it only works for the first item and for the second item again throws the same error. Any thought on how I could resolve this issue would be of great help.
Thanks,
Bikram
Reply With Quote