2017-04-22 8 views
0

このコードは機能しますが、一貫性がありません。ここでExcel VBAでXeroレポートを取得する

For Each objElement In htmlDoc.GetElementsByTagName("td") 

をコードである:多くの場合、この行に失敗し

Dim AppIE 
Dim htmlDoc 
Dim nRow As Integer 
Dim nColNum As Integer 
Dim objElement 
Dim nColCount As Integer 
Set AppIE = CreateObject("InternetExplorer.Application") 
AppIE.Visible = True 'login may be required 
AppIE.Navigate myString 'from clipboard 
While AppIE.Busy 'wait for IE to open 
    DoEvents 
Wend 
While AppIE.ReadyState <> 4 'wait for login 
    DoEvents 
Wend 
While AppIE.Busy 'pause put in by Donald 
    DoEvents 
Wend 
Set htmlDoc = AppIE.Document 
nRow = 0 
nColCount = 0 
nColNum = 0 
For Each objElement In htmlDoc.GetElementsByTagName("td") 
    'If IsNull(objElement.classname) And IsNull(objElement.innertext) Then 
    'Else 
     If objElement.classname = "RowHeader2 MenuLeft" Then 
      nColCount = nColCount + 1 
      Cells(nRow, nColCount).Value = objElement.innertext 
     Else 
      If nColCount >= 1 Then 
       nColNum = nColNum + 1 
       Cells(nRow, nColNum).Value = objElement.innertext 
       If nColNum = nColCount Then 
        nColNum = 0 
        nRow = nRow + 1 
       End If 
       If Len(objElement.classname) = 0 Then 
        nColNum = 0 
        nRow = nRow + 1 
       End If 
      Else 
       nRow = nRow + 1 
       Cells(nRow, 1).Value = objElement.innertext 
      End If 
     End If 
    'End If 
Next 
AppIE.Visible = False 
MsgBox "Done" 
AppIE.Quit 
Set AppIE = Nothing 
+0

失敗した場合は、最初の要素か他の要素にありますか?どのようなエラーコードがありますか? –

+0

こんにちは。失敗すると、最初の要素ですぐに失敗します。エラーは発生せず、ゼロ要素を見つけてすぐにForループを終了する必要があります。 –

答えて

0

このコードは、IEがXeroなどレポートへのナビゲーションを完了することができているようですか...?

'wait for IE to complete the navigaiton to the Xero report ---------------------------------------------------------------------------------------------------------------------------- 
n = 0 
While n = 0 
    For Each objElement In htmlDoc.GetElementsByTagName("td") 
     n = n + 1 
    Next 
    DoEvents 
Wend 
+0

これは実際にVBAの中で最も強力な部分ではありませんが、私は検索していました。要素を取得する前に、readyとstate = 4の両方を探すループを試してみるかもしれません。So:AppIE.BusyまたはAppIE.ReadyState <> 4:DoEvents:wend –

関連する問題