vbaunicodems-wordarabic

VBA in Microsoft Word Not Replacing Arabic Words — Works with English Only


I’m experiencing a persistent issue with VBA in Microsoft Word. My macros work perfectly when handling English text, but they fail to detect or replace Arabic words — even though those same words are clearly present in the document.

Here’s what I have tried:

  1. Changed the Word interface language to Arabic.
  2. Enabled all macro settings in Trust Center, including “Trust access to the VBA project object model.”
  3. Wrote the macro inside a proper Module (not in ThisDocument).
  4. Tried both word-by-word loops (For Each Range In Document.Words) and Find.Execute.
  5. Confirmed that the VBA editor uses a font that supports Arabic (Courier New).
  6. Used Unicode via ChrW() to insert Arabic words into the macro.
  7. Replaced words like "ذهب" or "في" — but nothing happened.
  8. The same macro works perfectly when I test it with English words (e.g., “TEST” → “SUCCESS”).
  9. Saved the document as .docm and tested on new files.
  10. Verified that Arabic text appears fine in the document and editor, but VBA just doesn’t recognize or manipulate it.
    Sub ReplaceArabic()
        With ActiveDocument.Content.Find
            .Text = "ذهب"
            .Replacement.Text = "فهم"
            .Execute Replace:=wdReplaceAll
        End With
    End Sub

This code does nothing. But if I replace “ذهب” with “TEST” and “فهم” with “SUCCESS”, it works immediately.

My Question:

Is this a known limitation of Word VBA with Arabic text? Are there specific settings, Office versions, or system configurations required to enable proper Arabic text handling in VBA?

I’d be very grateful for any guidance or working solutions. Thank you!


Solution

  • The VBA editor in Office is based on VB6 and doesn't support Unicode; it only works with legacy encodings based on the Windows system locale setting ("Language for non-Unicode programs"). If you could display Arabic strings, your system must be set to Arabic system locale. On my system, if I try to copy your sample code and paste it into the editor, it pastes with these lines:

            .Text = "???"
            .Replacement.Text = "???"
    

    However, VBA itself does support Unicode, VBA string variables and string members in the Office object model are always Unicode. By creating string values in another way rather than quoting literals as you did, I can get it to work. For example, the following will find "ذهب" and replace it with "abc":

    Sub ReplaceArabic()
         mystr = ChrW(&H630) + ChrW(&H647) + ChrW(&H628)
         With ActiveDocument.Content.Find
             .Text = mystr
             .Replacement.Text = "abc"
             .Execute Replace:=wdReplaceAll
         End With
     End Sub
    

    Here, I've declared a string variable mystr and set its value concatenating characters obtained using the ChrW function with hexadecimal arguments that are the Unicode code points of the Arabic characters in "ذهب".