By using "Replace()" to remove unwanted strings from Extracting Google Translate Examples Text success with innerhtml and outerhtml while innertext & outertext have big Spacing between Lines
Ranges A1 B1 Bad
Ranges A2 B2 Good
Ranges A1 B1 Bad
Ranges A2 B2 Good
VBA Code:
Sub Test()
Cells.Clear
ExtractExsamples "en", "ar", "man"
End Sub
Public Sub ExtractExsamples(FromLng, ToLng, StrText)
Dim IE As Object 'New InternetExplorer
Dim Url As String
Dim Doc As Object
Dim clipboard As Object
Set IE = CreateObject("InternetExplorer.Application")
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Url = "https://translate.google.com/?hl=en&tab=wT&authuser=0#view=home&op=translate&sl=" & FromLng & "&tl=" & ToLng & "&text=" & LCase(StrText)
With IE
.Visible = True
.navigate Url
While IE.ReadyState <> 4
DoEvents
Wend
Application.Wait (Now + TimeValue("0:00:01"))
Set Doc = IE.Document
Set TransExamples = Doc.querySelector(".gt-cd.gt-cd-mex") '.getElementsByTagName(".gt-ex-text")' how to use like this
''''''to cleanup
Set TransNone = Doc.querySelector(".gt-ex-info")
Set quotationmark = Doc.querySelector(".gt-ex-quote")
Set AutTrnsByGl = Doc.querySelector(".gt-ex-mt")
Set EXtp = Doc.querySelector(".gt-ex-top")
On Error Resume Next
Getinnertext = Replace(Replace(Replace(Replace(TransExamples.innertext, TransNone.innertext, ""), quotationmark.innertext, ""), AutTrnsByGl.innertext, ""), EXtp.innertext, "")
Getoutertext = Replace(Replace(Replace(Replace(TransExamples.outertext, TransNone.outertext, ""), quotationmark.outertext, ""), AutTrnsByGl.outertext, ""), EXtp.outertext, "")
[A1] = Getinnertext ''Range("A1")
[B1] = Getoutertext ''Range("B1")
Getinnerhtml = Replace(Replace(Replace(Replace(TransExamples.innerhtml, TransNone.innerhtml, ""), quotationmark.innerhtml, ""), AutTrnsByGl.innerhtml, ""), EXtp.innerhtml, "")
Getinnerhtml = "<html lang=""en""><head><body>" & Getinnerhtml & "</body></html>"
clipboard.SetText Getinnerhtml
clipboard.PutInClipboard
[A2].PasteSpecial ''Range("A2")
Getouterhtml = Replace(Replace(Replace(Replace(TransExamples.outerHTML, TransNone.outerHTML, ""), quotationmark.outerHTML, ""), AutTrnsByGl.outerHTML, ""), EXtp.outerHTML, "")
Getouterhtml = "<html lang=""en""><head><body>" & Getouterhtml & "</body></html>"
clipboard.SetText Getouterhtml
clipboard.PutInClipboard
[B2].PasteSpecial 'Range("B2")
.Quit
End With
End Sub