2016-03-09 32 views
0

我目前有以下代码无法给我一个对象变量或块变量未设置。想从excel网站提取文本012 vba

Set doc = IE.document 
Dim sDD4 As String 
sDD4 = Trim(doc.getElementById("productInfoContainter").getElementsByTagName("span")(0).innerText) 
Sheet1.Cells(i, 8) = sDD4 

的HTML即时试图从提取看起来像这样:

<div id="productInfoContainter" style="display: block;"> 
<table width="100%" style="display: none;" border="0" cellspacing="0" cellpadding="0"> 
    <tbody> 
     <tr> 
      <td valign="top"> 
       <div style="margin: 5px;"> 
        <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"> 
         <span class="google-src-text" style="text-align: left; direction: ltr;"> 
          <b>text i need 1</b> 
         </span> 
          <b>text i need 2</b> 
        </span> 
       </div> 
      </td> 
     </tr> 
     <tr> 
      <td valign="top"> 
       <div style="margin: 10px 10px 20px;" itemprop="description"></div> 
      </td> 
     </tr> 
    </tbody> 
</table> 

我也试图getElementbyClassName( “谷歌-SRC-文本”)(0).innerText

出现与同样的错误。

基本上我想翻译在谷歌网站翻译拿出译文

任何帮助赞赏

+0

我打赌'productInfoContainter'是一个错字。 – Jeeped

+0

是前几行代码的简化版本。因为我没有看到你打开网站?或者设置doc.navigate,我发现你根据你的标签从excel中调用代码。您是否设置了使用Internet控件的参考? – izzymo

回答

0

的身体我希望的代码是这样的

Sub WebNavigate() 
    Set objIE = CreateObject("InternetExplorer.Application") 
    WebSite = "www.YourWebsite.com/Links" 
    With objIE 
     .Visible = True 
     .navigate WebSite 
     Do While .Busy Or .readyState <> 4 
      DoEvents 
     Loop 

     Set Element = .document.getElementById("productInfoContainter") 
     Sheet1.Cells(i, 8) = Element.innertext 
     End With 

End Sub