Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-11-2022, 10:39 PM
Bikram Bikram is offline Importing styles using Templates only if style is not present already Windows 10 Importing styles using Templates only if style is not present already Office 2007
Advanced Beginner
Importing styles using Templates only if style is not present already
 
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
  #2  
Old 06-12-2022, 07:04 AM
macropod's Avatar
macropod macropod is offline Importing styles using Templates only if style is not present already Windows 10 Importing styles using Templates only if style is not present already Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-12-2022, 10:02 PM
Bikram Bikram is offline Importing styles using Templates only if style is not present already Windows 10 Importing styles using Templates only if style is not present already Office 2007
Advanced Beginner
Importing styles using Templates only if style is not present already
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

Thank you very much, Macropod.
Reply With Quote
  #4  
Old 06-15-2022, 03:56 AM
JingleBelle JingleBelle is offline Importing styles using Templates only if style is not present already Windows 10 Importing styles using Templates only if style is not present already Office 2016
Novice
 
Join Date: Nov 2020
Posts: 18
JingleBelle is on a distinguished road
Default

Thank you!

I tried the code, and it worked like a charm. This will be very helpful, indeed. Thank you, again.
Reply With Quote
Reply

Thread Tools
Display Modes


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 using Templates only if style is not present already Importing Styles causing odd formatting problems. Red Pill Word 3 06-12-2012 06:19 AM
Importing styles using Templates only if style is not present already Importing Templates in Word 2007 rbilleaud Word 4 06-29-2011 05:51 PM

Other Forums: Access Forums

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