Share via

Translate Word document from VBA

Anonymous
2024-04-08T22:51:04+00:00

Hi all,

I need to write a piece of VBA code that perform the complete document translation as I would do using Review > Translate > Translate Document command in Word interface.

I tried to find some piece of code in internet but they all try to force me to use Google translator. I DO NOT WANT TO USE GOOGLE TRANSLATOR. I need to use the translation function present in my Word application.

If I try recording a macro, the macro recorder records no action when I translate the document.

May someone please tell me which property or method may I use or redirect me to the correct forum?

Thank you.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2024-04-08T23:39:47+00:00

    Glossed right over the most important bit.

    How about an example of the code to actually perform the translation using YOUR preferred translation method or service.

    I am sure that the OP would be more than happy with that.

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-04-08T23:33:02+00:00

    Hi Paolone,

    I am an independent advisor, trying to help other users in the Community with my experience in Microsoft products. Please note that I am a user like you and don't work directly for Microsoft.

    Unfortunately, recording a macro doesn’t capture the translation action directly. However, you can try to achieve this using VBA code.

    Open your Word document that contains the text you want to translate. Press Alt + F11 to open the Visual Basic for Applications (VBA) editor. Insert a new module by clicking on “Insert” > “Module.” Copy and paste the modified VBA code below into the module:

    Public Sub TranslateToItalian() Dim selectedText As String Dim translatedText As String

    ' Get the currently selected text in the active document selectedText = Selection.Text

    ' Translate the selected text to Italian translatedText = TranslateText(selectedText, "auto", "it")

    ' Replace the selected text with the translated version Selection.Text = translatedText End Sub

    Function TranslateText(ByVal textToTranslate As String, ByVal fromLang As String, ByVal toLang As String) As String ' You can customize this function to handle translation ' using your preferred method or service ' For now, let's return a placeholder translated text TranslateText = "Placeholder translation to Italian: " & textToTranslate End Function

    Customize the TranslateText function within the code to perform the actual translation. Replace the placeholder logic with your preferred translation method or service. Close the VBA editor and return to your Word document. Select the text you want to translate. Run the macro by pressing Alt + F8, selecting “TranslateToItalian,” and clicking “Run.” The selected text will be replaced with the translated Italian text. Remember to adapt the TranslateText function to your specific translation needs.

    I would appreciate your feedback.

    Kindly note that this is a user-to-user forum, we are users helping other users, we aren't Microsoft employees and neither are we Microsoft agents.

    Best regards, Amadeusz

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments