How to modify XML Element

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
I'm tring to modify TransSynonyms : Set TransSynonyms = Doc.querySelector(".gt-cd.gt-cd-mss")
VBA Code:
 Sub Test()

Cells.Clear

ExtractExsamples "en", "ar", "man"
End Sub

Private Sub ExtractExsamples(FromLng, ToLng, StrText)

Dim IE As Object 'New InternetExplorer
Dim Url As String
Dim Doc As Object
Dim clipboard As Object
Dim rel As Object
'Dim Elem As Variant
Dim Synonymsresult 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:02"))
    
    Set Doc = IE.Document
  '
    Set TransSynonyms = Doc.querySelector(".gt-cd.gt-cd-mss")
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Modify TransSynonyms nodes
    For Each listNode In TransSynonyms.ChildNodes
        For Each fieldNode In listNode.ChildNodes
            
            Debug.Print "[" & fieldNode.Text & "]"
        
        Next
    Next
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
   Set TransResult = Doc.querySelector(".tlid-result.result-dict-wrapper")
   SinglReult = TransResult.querySelector(".tlid-translation.translation").outertext
 
     
     Set ExtenCount = TransSynonyms.querySelector(".cd-expand-button")
    
    On Error Resume Next
   
    Getinnertext = Replace(TransSynonyms.innerText, ExtenCount.innerText, "")
    Getinnertext = Replace(Replace(Replace(Getinnertext, Chr(10) & Chr(13), Chr(13)), Chr(13), ""), Chr(10), "", 1, 1)
    
    Getoutertext = TransSynonyms.outertext
    Getoutertext = Replace(Replace(Replace(Getoutertext, Chr(10) & Chr(13), Chr(13)), Chr(13), ""), Chr(10), "", 1, 1)
    
    [A1] = Getinnertext ''Range("A1") no Change
    [B1] = Getoutertext ''Range("B1") no Change
    
  '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   'VVVVVVVVVVVVVVVVVVVVVVVVVVVV
   ' To be Like this
    Getinnerhtml = Replace(Replace(Replace(Replace(TransSynonyms.innerHTML, ExtenCount.innerHTML, ""), "</span><span", "</span><span>]  [</span><span"), "span""><span", "span""><span>[</span><span"), "</span></span>", "</span><span>]</span></span>")
    Getinnerhtml = "<html lang=""en""><head><body>" & Getinnerhtml & "</body></html>"
    clipboard.SetText Getinnerhtml
    clipboard.PutInClipboard
    [A2].PasteSpecial  ''Range("A2")
    
    Getouterhtml = Replace(Replace(Replace(Replace(TransSynonyms.outerHTML, ExtenCount.outerHTML, ""), "</span><span", "</span><span>]  [</span><span"), "span""><span", "span""><span>[</span><span"), "</span></span>", "</span><span>]</span></span>")
    Getouterhtml = "<html lang=""en""><head><body>" & Getouterhtml & "</body></html>"
    clipboard.SetText Getouterhtml
    clipboard.PutInClipboard
    [B2].PasteSpecial 'Range("B2")
  '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   'VVVVVVVVVVVVVVVVVVVVVVVVVVVV
.Quit
End With
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top