返回列表 上一主題 發帖

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

回復 1# t8899

表格中按下滑鼠右鍵

   
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 3# t8899
  1. Option Explicit
  2. Sub TWSE_交易資訊()
  3.     Dim YMD_day As String, N As Name, webURL As String
  4.     YMD_day = InputBox("輸入 民國年度 : 102/10/07", "下載特定日期的資料", Format(Date - 1, "E/MM/DD")) '民國年度 "102/10/07"
  5.     With ActiveSheet  '可指定工作表
  6.         '當日沖銷交易標的及統計
  7.         webURL = "URL;http://www.twse.com.tw/ch/trading/exchange/TWTB4U/TWTB4U.php?input_date=" & YMD_day & "&login_btn=查詢.dat"
  8.         '鉅額交易日成交資訊
  9.         'webURL = "URL; http://www.twse.com.tw/ch/trading/block/BFIAUU/BFIAUU.php?input_date=" & YMD_day & "&login_btn=查詢.dat"
  10.         'webURL = "URL; http://www.twse.com.tw/ch/trading/block/BFIAUU/BFIAUU.php?input_date=" & YMD_day & "&login_btn=%ACd%B8%DF.dat"
  11.         If .QueryTables.Count = 0 Then
  12.             .QueryTables.Add Connection:=webURL, Destination:=.Range("A1")
  13.         Else
  14.             .QueryTables(1).Connection = webURL
  15.         End If
  16.         .Cells.Clear
  17.         With .QueryTables(1)
  18.             .WebSelectionType = xlSpecifiedTables
  19.             .WebFormatting = xlWebFormattingNone
  20.            ' .WebTables = "data_table"  '鉅額交易日成交資訊     :資料匯入的Tables
  21.             .WebTables = "9,11"         '當日沖銷交易標的及統計 :資料匯入的Tables
  22.             .Refresh BackgroundQuery:=False
  23.         End With
  24.     End With
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 8# t8899
  1. Option Explicit
  2. Sub 集中市場大盤資訊()
  3.     Dim x, ie As Object, Sh As Worksheet
  4.     Set ie = CreateObject("InternetExplorer.Application")
  5.    ' ie.Visible = True          '是否顯示 IE
  6.     ie.Navigate "about:Tabs"    '空白的網頁
  7.     Set Sh = ActiveSheet        '設定工作表
  8.     Sh.Cells.Clear
  9.     Application.ScreenUpdating = False
  10.     With CreateObject("InternetExplorer.Application")
  11.     '    .Visible = True        '是否顯示 IE
  12.         .Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
  13.         Do While .readyState <> 4
  14.             DoEvents
  15.         Loop
  16.         For Each x In .document.getElementsByTagName("table")
  17.             If x.ID <> "logo" Then
  18.                 With ie
  19.                     .document.body.innerHTML = x.outerHTML
  20.                     Do While .readyState <> 4 Or .busy
  21.                         DoEvents
  22.                     Loop
  23.                     .execwb 17, 2       '  網頁文字 Select All
  24.                     .execwb 12, 2       '  網頁文字 Copy
  25.                     With Sh
  26.                         If .UsedRange.Rows.Count = 1 Then
  27.                             .Range("A1").Select
  28.                         Else
  29.                             .Range("A" & .UsedRange.Rows.Count + 1).Select
  30.                         End If
  31.                         .PasteSpecial Format:="HTML"
  32.                     End With
  33.                 End With
  34.             End If
  35.         Next
  36.         .Quit
  37.     End With
  38.     ie.Quit
  39.     With Sh
  40.         .Columns.WrapText = False
  41.         .Columns.AutoFit
  42.         .[a1].Select
  43.     End With
  44.     Application.ScreenUpdating = True
  45. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 2# t8899
此網頁的程式碼
  1. Option Explicit
  2. Sub timestock()
  3. Application.ScreenUpdating = False
  4.     Dim i As Integer, S As Integer, K As Integer, j As Integer
  5.     Dim Element
  6.     With CreateObject("InternetExplorer.Application")
  7.         '.Visible = True           '可顯示網頁
  8.        .Navigate "http://newmis.twse.com.tw/stock/group.jsp?ex=tse&ind=TIDX#STATISTICS"
  9.         Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
  10.         Set Element = .document.getelementsbytagname("table")
  11.         With Sheets("SHEET5")
  12.            .Cells.Clear
  13.             'For s = 0 To element.Length - 1 '不知此網頁的table內容:請先行測試網頁的完整table內容
  14.             For S = 1 To 3                    '已找出網頁的table內容在 0-3 中
  15.                 For i = 0 To Element(S).Rows.Length - 1
  16.                     K = K + 1
  17.                  For j = 0 To Element(S).Rows(i).Cells.Length - 1   '資料的欄位共6位
  18.                          .Cells(K, j + 1) = Element(S).Rows(i).Cells(j).innerText
  19.                     Next
  20.                Next
  21.             Next
  22.      '       .Cells.EntireColumn.AutoFit            '自動調整欄寬
  23.       '      .Cells.EntireRow.AutoFit               '自動調整列高
  24.         End With
  25.        .Quit
  26.     End With
  27.     Set Element = Nothing
  28.     MsgBox "0K"
  29. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2014-1-14 13:57 編輯

回復 15# t8899
一直出現"ok"訊息視窗??  你是中學生應該會關閉這"ok"視窗.有問題試著自己解決,VBA才會進步.
附上檔案看看
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 18# t8899
這方向是對的,有進步

  1. For S = 2 To 3                    '找出網頁的table 內容在 0-3 中
  2.   'j=2
  3. IF S=2 Then j=?                '寫下你的條件
  4. IF S=3 Then j=?
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

回復 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

        靜思自在 : 是非當教育,讚美作警惕。
返回列表 上一主題