Khalid Hussain
Khalid Hussain

Reputation: 355

Is there a faster way than looping through document paragraphs?

Complete novice. I created a Word document using Pandoc but the Arabic output required some modification. Scraping through online documentation and posts, I have got the following code:

Sub pandoc_RTL()
'
' pandoc_RTL Macro
'
'

Dim objPara As Paragraph

    For Each objPara In ActiveDocument.Paragraphs

        If objPara.Style = "body AR" Then
            objPara.Range.Select            
            Selection.RtlRun
            Selection.RtlPara
        End If

        If objPara.Style = "hadith AR" Then
            objPara.Range.Select
            Selection.RtlRun
            Selection.RtlPara
        End If

        If objPara.Style = "hadith in-list AR" Then
            objPara.Range.Select
            Selection.RtlRun
            Selection.RtlPara
        End If

        If objPara.Style = "athar AR" Then
            objPara.Range.Select
            Selection.RtlRun
            Selection.RtlPara
        End If

        If objPara.Style = "body AR" Then
            objPara.Range.Select
            Selection.RtlRun
            Selection.RtlPara
        End If

    Next

End Sub

My questions:

  1. Is Regex the only way to avoid repetition since all the required styles end with “AR”?
  2. Is there a faster way to do this besides looping through all the paragraphs?

Upvotes: 0

Views: 131

Answers (1)

macropod
macropod

Reputation: 13515

You need only modify the Styles. The radio buttons for this are accessible via Manage Styles>Modify>Format>Paragraph>Indents and Spacing in any document for which an RTL language has been enabled. In any event, using VBA:

Dim i As Long, ArrStl
ArrStl = Array("body AR", "hadith AR", "hadith in-list AR", "athar AR")
For i = 0 To UBound(ArrStl)
  ActiveDocument.Styles(ArrStl(i)).ParagraphFormat.ReadingOrder = wdReadingOrderRtl
Next

Done.

Upvotes: 1

Related Questions