Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2020, 07:54 PM
melan melan is offline Format text based on Tag Windows 10 Format text based on Tag Office 2019
Novice
Format text based on Tag
 
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
  #2  
Old 04-23-2020, 08:36 PM
Guessed's Avatar
Guessed Guessed is offline Format text based on Tag Windows 10 Format text based on Tag Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
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
  #3  
Old 04-23-2020, 08:54 PM
gmayor's Avatar
gmayor gmayor is offline Format text based on Tag Windows 10 Format text based on Tag Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If the styles exist in the document and the document is laid out as shown, try the following
Code:
Sub TextChanger()
Dim i As Long
Const StrFind As String = "ALB,TRI,QIU,FSF"
    On Error Resume Next
    Application.ScreenUpdating = False
    For i = 0 To UBound(Split(StrFind, ","))
        With ActiveDocument.Range.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Format = True
            .Wrap = wdFindContinue
            .MatchWildcards = True
            .Text = "\@" & Split(StrFind, ",")(i) & " = (*^13)"
            .Replacement.Style = Split(StrFind, ",")(i)
            .Replacement.Text = "\1"
            .Execute Replace:=wdReplaceAll
        End With
    Next i
    Application.ScreenUpdating = True
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Format cell and add a comment based on another tab Catluvr Excel Programming 8 08-03-2018 09:54 AM
Format text based on Tag Format Multiple Textboxes Based On the Contents of One Text Box dmcgettigan Word VBA 1 02-27-2017 08:50 PM
Format text based on Tag Format Text based on use elsewhere Rtowey Excel 1 07-21-2015 06:05 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:59 PM.


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