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:
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!
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 "ذهب".