View Single Post
 
Old 04-23-2020, 08:36 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The most obvious error would be the Paragraph styles not existing. Since you are opening a txt file it will contain only built-in stylenames and the styles you are trying to apply are not built-in names.

So your macro should either create those styles or import them from a template that does contain them. I've added a line to import the styles from the attached template - this will be Normal.dotm unless you have configure the attached template differently.

You don't need wildcards if the styles are Paragraph styles - the entire paragraph will get the style if you apply it this way.
Code:
Sub TextChanger()
  Dim arrFind() As String, i As Long
  
  'Application.ScreenUpdating = False
  ActiveDocument.UpdateStyles     'import styles from attached template
  arrFind = Split("ALB,TRI,QIU,FSF", ",")
  
  With ActiveDocument.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Format = True
    .Wrap = wdFindContinue
    .MatchWildcards = False
    For i = LBound(arrFind) To UBound(arrFind)
      .Text = "@" & arrFind(i)
      .Replacement.Style = arrFind(i)
      .Execute Replace:=wdReplaceAll
    Next i
  End With
  'Application.ScreenUpdating = True
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote