\"Writing.Com
*Magnify*
Printed from https://www.writing.com/main/view_item/item_id/2349667-ToWritingML-Word-Macro
Item Icon
Rated: E · Article · Other · #2349667

ToWritingML Word Macro


Sub FormatToWrtingML()
'
' FormatToWrtingML Macro
'
'
    Application.ScreenUpdating = False
    ConvertItalic
    ConvertCenter
    ConvertBold
    ParaChange
    ' Copy to clipboard
    ActiveDocument.Content.Copy
    Application.ScreenUpdating = True
End Sub

Private Sub ConvertItalic()
    ActiveDocument.Select
   
    With Selection.Find
   
        .ClearFormatting
        .Font.Italic = True
        .Text = ""
       
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
       
        .Forward = True
        .Wrap = wdFindContinue
       
        Do While .Execute
            With Selection
                If InStr(1, .Text, vbCr) Then
                    ' Just process the chunk before any newline characters
                    ' We'll pick-up the rest with the next search
                    .Font.Italic = False
                    .Collapse
                    .MoveEndUntil vbCr
                End If
                                     
                ' Don't bother to markup newline characters (prevents a loop, as well)
                If Not .Text = vbCr Then
                    .InsertBefore "{i}"
                    .InsertAfter "{/i}"
                End If
               
                .Font.Italic = False
            End With
        Loop
    End With
End Sub

Private Sub ConvertCenter()
    ActiveDocument.Select
   
    With Selection.Find
   
        .ClearFormatting
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        '.Font.Italic = True
        .Text = ""
       
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
       
        .Forward = True
        .Wrap = wdFindContinue
       
        Do While .Execute
            With Selection
                If InStr(1, .Text, vbCr) Then
                    ' Just process the chunk before any newline characters
                    ' We'll pick-up the rest with the next search
                    .ParagraphFormat.Alignment = wdAlignParagraphLeft
                    '.Font.Italic = False
                    .Collapse
                    .MoveEndUntil vbCr
                End If
                                     
                ' Don't bother to markup newline characters (prevents a loop, as well)
                If Not .Text = vbCr Then
                    .InsertBefore "{center}"
                    .InsertAfter "{/center}"
                End If
                .ParagraphFormat.Alignment = wdAlignParagraphLeft
                '.Font.Italic = False
            End With
        Loop
    End With
End Sub
Private Sub ConvertBold()
    ActiveDocument.Select
   
    With Selection.Find
   
        .ClearFormatting
        .Font.Bold = True
        .Text = ""
       
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
       
        .Forward = True
        .Wrap = wdFindContinue
       
        Do While .Execute
            With Selection
                If InStr(1, .Text, vbCr) Then
                    ' Just process the chunk before any newline characters
                    ' We'll pick-up the rest with the next search
                    .Font.Bold = False
                    .Collapse
                    .MoveEndUntil vbCr
                End If
                                     
                ' Don't bother to markup newline characters (prevents a loop, as well)
                If Not .Text = vbCr Then
                    .InsertBefore "{b}"
                    .InsertAfter "{/b}"
                End If
               
                .Font.Italic = False
            End With
        Loop
    End With
End Sub

Sub ParaChange()
'
' ParaChange Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^p"
        .Replacement.Text = "^l^l^t"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

© Copyright 2025 Max Griffin 🏳️‍🌈 (mathguy at Writing.Com). All rights reserved.
Writing.Com, its affiliates and syndicates have been granted non-exclusive rights to display this work.
Printed from https://www.writing.com/main/view_item/item_id/2349667-ToWritingML-Word-Macro