不知道为什么"设置IE = shellwins.item(0)"没有工作

时间:2016-11-30 09:16:16

标签: excel vba excel-vba

编辑:我找到了解决问题的方法。该程序试图从另一个打开的窗口(我的Lotus Notes电子邮件客户端)中获取内容。首先启动电子邮件客户端窗口,然后是Internet Explorer。如果IE以外的所有应用程序都已关闭,那么该程序就像魅力一样。

但是,我仍然不明白为什么代码的以下部分不会强制使用' IE窗口进入shellwins.Item(0):     如果shellWins.Count> 0然后         '获取IE中的第一个选项卡         设置IE = shellWins.Item(0)       其他         '创建IE         设置IE = New InternetExplorer         IE.Visible = True       结束如果

欢迎任何评论。

原始问题声明: 我拼凑了一个vba脚本,从一个正在运行的Internet Explorer实例上的网站上获取日期,标题,文本和URL。我的想法是让我浏览网站上感兴趣的新闻,然后运行宏来捕捉上述信息,如果故事是相关的。

感兴趣的网站是www.channelnewsasia.com上与新加坡相关的任何文章。一个这样的网站的一个例子是: http://www.channelnewsasia.com/news/singapore/raffles-country-club-to-make-way-for-kl-singapore-high-speed/3412348.html

代码在" posttime"行引发错误。变量,试图抓住新闻故事的发布日期。以下是网站上的http代码

By <span class="stories-byline-link">
        <a href="/action/news/storiesby/storiesby/678458/storiesby.do?sortBy=latest&amp;bylineId=1585486&amp;pageNum=0">Lianne Chia</a> and <a href="/action/news/storiesby/storiesby/678458/storiesby.do?sortBy=latest&amp;bylineId=8756&amp;pageNum=0">Monica Kotwani</a></span>
    </li>
  <li class="news_posttime"><strong>Posted</strong> 04 Jan 2017 12:50</li>
  <li class="news_updatedtime"><strong>Updated</strong> 04 Jan 2017 22:23</li>
  </ul>

即使错误也不一致,一旦它显示错误91,对象未设置,现在我看到&#34;错误438,对象不支持属性或方法&#34;随着&#34; posttime&#34;变量

代码中是否存在缺陷?

Sub Get_CNA_SIN()

 Dim shellWins As ShellWindows
  Dim IE As InternetExplorer
  Dim sht As Worksheet
  Dim StartCell As Range
  Dim LastRow As Long
  Dim LastColumn As Long
  Dim NewRow As Long
  Dim posttime As String


  Set shellWins = New ShellWindows

  If shellWins.Count > 0 Then
    ' Get the first tab in IE
    Set IE = shellWins.Item(0)
  Else
    ' Create IE
    Set IE = New InternetExplorer
    IE.Visible = True
  End If

'  IE.Navigate "http://support.microsoft.com/kb/q176792/"
'Copy the URL from the selected tab

'Error occurs at this line, which tries to grab the story's posting time
posttime = IE.Document.GetElementsbyClassName("news_posttime")(0).InnerText


'Find LastRow
Set sht = Worksheets("sheet1")
Set StartCell = Range("A1")
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
NewRow = LastRow + 1

On Error Resume Next
Cells(NewRow, 4).Value = IE.LocationURL
Cells(NewRow, 2).Value = IE.Document.Title
Cells(NewRow, 1).Value = Trim(Mid(posttime, 7, 13))
Cells(NewRow, 3).Value = IE.Document.GetElementsbyClassName("news_brief hideBrief")(0).InnerText
Cells(NewRow, 5).Value = "Singapore"


  Set IE = Nothing
  Set shellWins = Nothing

sht.Range("A:A").NumberFormat = "dd-mmm-yy"

End Sub

0 个答案:

没有答案