View Single Post
 
Old 04-23-2020, 07:54 PM
melan melan is offline Windows 10 Office 2019
Novice
 
Join Date: Apr 2020
Posts: 1
melan is on a distinguished road
Default Format text based on Tag

Hello!

I have raw text in a .txt file that I am hoping to format using VBA. I haven't touched this stuff in over a decade so I am struggling very much and am looking for some pointers!

My raw text looks something like this:

Code:
@ALB = <Text that needs to be bolded>
@TRI = <Text that needs to be Italicized>
@QIU = <Text that needs to be Underlined>
@ALB = <Text that needs to be bolded>
@ALB = <Text that needs to be bolded>
@FSF = <Text that needs Font change>
In reality, the requirements of each line are slightly more complex than simple bolding or underlining. I have created appropriate Styles for each format, and they are using the same naming convention as the Tag they are associated with (ie. I've named the first style ALB, the second style TRI, etc).

After the text has been appropriately formatted, the code will also need to delete the tag portion (ie. "@ALB = ") so that only the formatted text remains, but I haven't gotten this far with my code.

Can you please let me know if I am on the right track? My code was working but began giving errors as my evening progressed and I'm currently getting error 5834.

Code:
Sub TextChanger()
Application.ScreenUpdating = False
Dim StrFind As String, StrStyle As String, i As Long
StrFind = "ALB,TRI,QIU,FSF"
StrStyle = "ALB,TRI,QIU,FSF"

With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Forward = True
  .Format = True
  .Wrap = wdFindContinue
  .MatchWildcards = True
  For i = 0 To UBound(Split(StrFind, ","))
    .Text = "@" & Split(StrFind, ",")(i) & " = " & "[A-Za-z]*$"
    .Replacement.Style = Split(StrStyle, ",")(i)
    .Execute Replace:=wdReplaceAll
  Next i
End With
Application.ScreenUpdating = True
End Sub
Thanks in advance!
Reply With Quote