错误类型在定义范围的值上不匹配

时间:2018-09-03 06:32:47

标签: excel vba

我在objie.document下的rng.value上遇到错误类型不匹配。我想做的是根据所定义范围的值进行Google新闻搜索,并遍历每个单元格。

Sub SearchBot()

    Dim objie As InternetExplorer
    Dim aEle As HTMLLinkElement
    Dim y As Integer
    Dim result As String
    Dim form As Variant, button As Variant
    Dim rng As Range
    Dim i As Integer
    Dim lastrow As Long
    lastrow = Sheets("sheet1").Range("A" & Rows.Count).End(xlUp).Row
    Set objie = New InternetExplorer
    Set rng = Range("A2:A" & lastrow)
    user = Environ("username")
    objie.Visible = True
    For Each cell In rng
    rng.Select
    Range(ActiveCell, ActiveCell.End(xlDown)).Select
    objie.Navigate "https://www.google.com.sg/search?q=(fraud)&tbm=nws&spf=1495542183367&cad=h"

    Do While objie.Busy = True Or objie.ReadyState <> 4: DoEvents: Loop

    objie.Document.getElementById("lst-ib").Value = _
    rng.Value & " (fraud)"

    Set form = objie.Document.body.getElementsByTagName("form")(0)
    Set button = form.getElementsByTagName("button")(0)
    button.Click
    Do While objie.Busy = True Or objie.ReadyState <> 4: DoEvents: Loop

    TimeOutWebQuery = 5
    TimeOutTime = DateAdd("s", TimeOutWebQuery, Now)
    Do Until objie.ReadyState = 4
        DoEvents
        If Now > TimeOutTime Then
            objie.Stop
            GoTo ErrorTimeOut
        End If
    Loop
    objie.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
    Call PDFPrint("C:\Users\" & user & "\Desktop\" & "Screening_" & rng.Value & " " & rng.Offset(0, 1).Value & ".pdf")
ErrorTimeOut:
    Set objie = Nothing
    Next cell
End Sub

1 个答案:

答案 0 :(得分:0)

rng.cells.Count大于1。这意味着您无法访问范围的属性值。您必须在一个单元中。您可能是

objie.Document.getElementById("lst-ib").Value = _
cell.Value & " (fraud)"