返回列表 上一主題 發帖

[發問] CreateObject("InternetExplorer.Application") 執行完有時會無法跳出??

循還的範圍
.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
        Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
        Set Element = .document.getelementsbytagname("table")
        With Sheets("SHEET1")
           .Cells.Clear
           K = 0
            'For s = 0 To element.Length - 1 '不知此網頁的table內容:請先行測試網頁的完整table內容
            For S = 2 To 2                    '已找出網頁的table內容在 0-3 中
                For i = 0 To Element(S).Rows.Length - 1
                    K = K + 1
               '  For j = 0 To Element(S).Rows(i).Cells.Length - 1   '資料的欄位共6位
                  j = 2
                         .Cells(K, j + 1) = Element(S).Rows(i).Cells(j).innerText
                        
                '    Next
               Next
            Next
     '       .Cells.EntireColumn.AutoFit            '自動調整欄寬
      '      .Cells.EntireRow.AutoFit               '自動調整列高
        End With

TOP

本帖最後由 GBKEE 於 2014-1-15 19:33 編輯

回復 21# t8899
ThisWorkbook 程式碼
  1. Option Explicit
  2. Dim Ie As Object, Msg As Boolean
  3. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  4.     Ie.Quit
  5. End Sub
  6. Private Sub Workbook_Open()
  7.     Set Ie = CreateObject("InternetExplorer.Application")
  8.     Ie.Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
  9.     Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop
  10.     Msg = False
  11.     timestock
  12. End Sub
  13. Private Sub timestock()
  14.     Dim i As Integer, S As Integer, K As Integer, j As Integer
  15.     Dim Element
  16.     If Msg = True Then Ie.Quit:   End
  17.     Application.OnTime Time + #12:00:30 AM#, "ThisWorkbook.timestock"
  18.     Application.ScreenUpdating = False
  19.     Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop
  20.     Set Element = Ie.document.getelementsbytagname("table")
  21.     With Sheets("sheet5")
  22.         .Cells.Clear
  23.         For S = 2 To 3                    '已找出網頁的table內容在 0-3 中
  24.             For i = 0 To Element(S).Rows.Length - 1
  25.                 K = K + 1
  26.                 For j = 0 To Element(S).Rows(i).Cells.Length - 1   '資料的欄位共6位
  27.                     .Cells(K, j + 1) = Element(S).Rows(i).Cells(j).innerText
  28.                 Next
  29.             Next
  30.         Next
  31.      '       .Cells.EntireColumn.AutoFit            '自動調整欄寬
  32.       '      .Cells.EntireRow.AutoFit               '自動調整列高
  33.     End With
  34. End Sub
  35. Private Sub The_End()  '停止的程式
  36.     Msg = True
  37. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復  t8899
ThisWorkbook 程式碼
GBKEE 發表於 2014-1-15 16:31

請問如何停止??

TOP

回復  t8899
ThisWorkbook 程式碼
GBKEE 發表於 2014-1-15 16:31

30秒時間想由K1儲存格來控制,要如何改??

Dim i As Integer, S As Integer, K As Integer, j As Integer
     Dim Element
     Dim RNG
    If Sheet3.Range("K1").Value = 1 Then Set RNG = #12:01:00 AM#
    If Sheet3.Range("K1").Value = 2 Then Set RNG = #12:02:00 AM#
    If Sheet3.Range("K1").Value = 3 Then Set RNG = #12:00:30 AM#
    If Sheet3.Range("K1").Value = 4 Then Set RNG = #12:00:10 AM#

    Application.OnTime Time + RNG, "timestock"

----------------------------------------------------
#12:01:00 AM# 需要物件 ???

TOP

30秒時間想由K1儲存格來控制,要如何改??

Dim i As Integer, S As Integer, K As Integer, j As Inte ...
t8899 發表於 2014-1-15 20:36

抱歉,打擾.......已解決....................
If Sheet3.Range("K1").Value = 1 Then Application.OnTime Time + #12:01:00 AM#, "timestock"
    If Sheet3.Range("K1").Value = 2 Then Application.OnTime Time + #12:02:00 AM#, "timestock"
    If Sheet3.Range("K1").Value = 3 Then Application.OnTime Time + #12:00:30 AM#, "timestock"
    If Sheet3.Range("K1").Value = 4 Then Application.OnTime Time + #12:00:10 AM#, "timestock"

TOP

回復  t8899
此網頁的程式碼
GBKEE 發表於 2014-1-13 17:40


是否少了 Set Element = Nothing 這一行??
應插在34或36行前面??

TOP

回復 26# t8899
  1. Private Sub timestock()
  2.     Dim i As Integer, S As Integer, K As Integer, j As Integer
  3.     Dim Element
  4.     'timestock這程序內的Dim宣告 是為timestock這程序的私用變數
  5.     'timestock這程序執行完畢所有的私用變數會自動釋放
  6.     '無須 Set Element = Nothing
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 t8899 於 2014-2-10 14:59 編輯
回復  t8899
GBKEE 發表於 2014-1-16 07:25


請問今天連了兩了小時出現錯誤在這一行
Do While Ie.Busy Or Ie.ReadyState <> 4: DoEvents: Loop


如何解決?
1直接跳出
2.不理會錯誤
此兩種狀況,各如何處理?

TOP

        靜思自在 : 閒人無樂趣,忙人無是非。
返回列表 上一主題