I have written a sample code that opens the Google Translate webpage for Documents and automates click events to upload (Browse files) word/excel file, generate translated document and download it to local folder.
The problem is that i have to manually select the file from the Open File dialog that appears. Ultimately, i want to loop thru a folder and automate sending files in that folder for translation.
- Is there a way that the files are automatically selected in the filename textbox of the fileOpen dialog without user intervention?
- Is there a way to auto download the translated files to a specified folder?
Option Explicit
Sub TranslateAndDownload()
Dim IE As Object
Dim URL As String
Dim FileToTranslate As String
Dim TranslatedFileLink As String
Dim oHTML_Element
' URL of Google Translate with parameters for auto-detect source language and target English
URL = "https://translate.google.com/?sl=auto&tl=en&op=docs"
' Path to the file you want to upload for translation
FileToTranslate = "C:\Users\Doctor\French_Test.docx"
' Create a new Internet Explorer instance
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
' Navigate to the Google Translate URL
IE.Navigate URL
' Wait until IE finishes loading the page
Do While IE.busy Or IE.readyState <> 4
DoEvents
Loop
' Upload the file for translation
For Each oHTML_Element In IE.Document.getElementsByName("file")
If oHTML_Element.className = "ZdLswd" Then
oHTML_Element.Value = FileToTranslate
oHTML_Element.Click
Exit For
End If
Next oHTML_Element
' translate document
Application.Wait Now + TimeValue("00:00:05") ' Wait for 05 seconds
For Each oHTML_Element In IE.Document.getElementsByClassName("VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-Bz112c-M1Soyc nCP5yc AjY5Oe LQeN7 k2RlOb")
oHTML_Element.Click
Exit For
Next oHTML_Element
' download translated document
Application.Wait Now + TimeValue("00:00:05") ' Wait for 05 seconds
For Each oHTML_Element In IE.Document.getElementsByClassName("VfPpkd-LgbsSe VfPpkd-LgbsSe-OWXEXe-k8QpJ VfPpkd-LgbsSe-OWXEXe-Bz112c-M1Soyc nCP5yc AjY5Oe LQeN7 sWFiQe")
oHTML_Element.Click
Exit For
Next oHTML_Element
' Clean up and close IE
IE.Quit
Set IE = Nothing
Set oHTML_Element = Nothing
End Sub