You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VBA实现Pons网站文本翻译搜索?报错求助

Fixing Your VBA Script for Pons Translation

Let's get your script working properly—here's what went wrong and how to fix it:

1. The Root Cause of Your Error

The ID text-translation-video-ad you're targeting isn't the input box—it's actually a container for a video ad on the page. That's why you're getting the "Object doesn't support this property or method" error: the element with that ID has no .Value property to set.

The real input box on the Pons text translation page uses the ID source—that's the element you need to interact with.

2. Corrected & Improved Script

I've rewritten your code with fixes, plus added better waiting logic to handle modern dynamic pages (just checking ReadyState = 4 isn't always enough for async-loaded content):

Sub PonsTranslation()
    Dim objIE As Object
    Dim inputBox As Object
    Dim translateBtn As Object
    
    ' Initialize Internet Explorer
    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
        .Top = 0
        .Left = 0
        .Width = 800
        .Height = 600
        .AddressBar = 0
        .StatusBar = 0
        .Toolbar = 0
        .Visible = True
        .Navigate "https://pl.pons.com/tłumaczenie-tekstu"
    End With
    
    ' Wait for page to load (with 10-second timeout to avoid infinite loops)
    Dim timeout As Double
    timeout = Now + TimeValue("00:00:10")
    Do
        DoEvents
        ' Check if input box exists (handles async loading)
        On Error Resume Next
        Set inputBox = objIE.Document.GetElementByID("source")
        On Error GoTo 0
    Loop Until (objIE.ReadyState = 4 And Not inputBox Is Nothing) Or Now > timeout
    
    ' Exit if timeout hits
    If Now > timeout Then
        MsgBox "Page took too long to load!"
        objIE.Quit
        Set objIE = Nothing
        Exit Sub
    End If
    
    ' Enter the word to translate
    inputBox.Value = "piłka"
    
    ' Find and click the translate button (ID: "translate-btn")
    Set translateBtn = objIE.Document.GetElementByID("translate-btn")
    If Not translateBtn Is Nothing Then
        translateBtn.Click
    Else
        MsgBox "Translate button not found!"
        objIE.Quit
        Set objIE = Nothing
        Exit Sub
    End If
    
    ' Wait for translation results to load
    timeout = Now + TimeValue("00:00:05")
    Do
        DoEvents
    Loop Until objIE.ReadyState = 4 Or Now > timeout
    
    ' Optional: Extract and display translation results
    Dim resultContainer As Object
    Set resultContainer = objIE.Document.GetElementById("target")
    If Not resultContainer Is Nothing Then
        MsgBox "Translation Result:" & vbCrLf & resultContainer.innerText
    End If
    
    ' Uncomment below to auto-close IE after running
    ' objIE.Quit
    ' Set objIE = Nothing
End Sub

3. Key Improvements Explained

  • Correct Element Targeting: Uses the actual IDs for the input box (source) and translate button (translate-btn) instead of the ad container ID.
  • Timeout Protection: Prevents the script from getting stuck if the page or elements load too slowly.
  • Async Load Handling: Checks for the input box's existence before interacting with it, since some elements load after the initial page ready state.
  • Result Extraction: Includes optional code to pull the translation text from the right-side output area (target container) and show it in a message box.

Troubleshooting Tips

  • If elements still can't be found, right-click the input box/translate button in your browser and select "Inspect" to confirm the current IDs (websites sometimes update their HTML structure).
  • Ensure Internet Explorer isn't running in compatibility mode for the Pons site, as this can break DOM access.

内容的提问来源于stack exchange,提问作者Arkadiusz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:08:36