Web 抓取宏错误

Web 抓取宏错误

我正在尝试抓取作为关键作品搜索结果的多个页面。我编写了此代码,但运行时出现错误。错误:

Application \-defined or object-defined error. 

当我单击“调试”时,它会指向以下行:

With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$A$1"))

代码:

Sub adds()

For x = 1 To 3

Worksheets("sheet1").Select

Worksheets("sheet1").Activate

mystr = "URL;http://apps.webofknowledge.com.ezproxy1.lib.asu.edu/Search.do?product=WOS&SID=1BzJbqm9xDW5Yzj4rhB&search_mode=GeneralSearch&prID=44dc4b3a-138a-4532-8429-772860bea673"

mystr = Cells(x, 1)

Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = x

With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$A$1"))

'.CommandType = 0

.Name = _
        "http://apps.webofknowledge.com.ezproxy1.lib.asu.edu/Search.do?product=WOS&SID=1BzJbqm9xDW5Yzj4rhB&search_mode=GeneralSearch&prID=44dc4b3a-138a-4532-8429-772860bea673"

.FieldNames = True

.RowNumbers = False

.FillAdjacentFormulas = False

.PreserveFormatting = True

.RefreshOnFileOpen = False

 .BackgroundQuery = True

.RefreshStyle = xlInsertDeleteCells

.SavePassword = False

.SaveData = True

.AdjustColumnWidth = True

.RefreshPeriod = 0

.WebSelectionType = xlEntirePage

.WebFormatting = xlWebFormattingNone

.WebPreFormattedTextToColumns = True

.WebConsecutiveDelimitersAsOne = True

.WebSingleBlockTextImport = False

.WebDisableDateRecognition = False

.WebDisableRedirections = False

.Refresh BackgroundQuery:=False

End With

Next x

End Sub

答案1

mystr您在此行中使用变量-

With ActiveSheet.QueryTables.Add(Connection:=mystr, Destination:=Range("$A$1"))

mystr = "URL;http://apps.webofknowledge.com.ezproxy1.lib.asu.edu/Search.do?product=WOS&SID=1BzJbqm9xDW5Yzj4rhB&search_mode=GeneralSearch&prID=44dc4b3a-138a-4532-8429-772860bea673"

但后来你重新定义在方法中调用它querytables.add-

mystr = Cells(x, 1)

所以,现在是

With ActiveSheet.QueryTables.Add(Connection:=Cells(x, 1), Destination:=Range("$A$1"))

除非 cells(x,1) 具有该方法的正确格式querytables.add,否则它将不知道该做什么。

您似乎还没有声明任何变量,所以我不确定它们是什么。

相关内容