2017-03-01 41 views
0

我習慣於VBA,但是我從未嘗試過VB .NET,我需要將2個網頁中的文本轉換爲文本文件。這是我正在使用的代碼,但我有問題!我必須做些什麼才能使其發揮作用?將網頁內容寫入文本文件

Public mIE As Object 
Public arrText(1) As String 
Public Const myFile As String = "C:\myTextFile.txt" 

Public Sub Main() 
    Dim arrURL(1) As String 
    Dim i As Byte 

    On Error Resume Next 
    Kill (myFile) 

    ' Define URL 
    arrURL(0) = "http://URL1" 
    arrURL(1) = "http://URL2" 

    For i = 0 To 1 
     'Spawn Internet Explorer 
     mIE = CreateObject("InternetExplorer.Application") 

     arrText(i) = openWebPage(arrURL(i)) 

     mIE.Quit() 
     mIE.Close() 
     mIE = Nothing 
    Next 

    Call saveToTextFile 
End Sub 

Public Function openWebPage(myURL As String) As String 
    With mIE 
     .Top = 0 
     .Left = 0 
     .Height = 800 
     .Height = 600 
     .AddressBar = 0 
     .StatusBar = 0 
     .Toolbar = 0 
     .Visible = True 
     .navigate (myURL) 
    End With 

    openWebPage = mIE.document.body.innerText 
End Function 

Public Sub saveToTextFile() 
    Dim oWriter As New System.IO.StreamWriter(myFile) 
    Dim i As Byte 

    For i = 0 To UBound(arrText) 
     oWriter.WriteLine (arrText(i)) 
    Next 

    oWriter.Close() 
End Sub 

問候, 埃利奧·費爾南德斯

+0

你有什麼問題? –

+0

我有的問題是在主程序的for循環中。當i = 1時,arrText(i)= openWebPage(arrURL(i))返回'Nothing'。它應該返回第二個URL頁面的內容。 –

回答

0

我想你可能有都沒有找到,像「fichSGA的論點。他們可能在你沒有提供的代碼中。您可以在Visual Studio中添加'斷點',然後通過在標準鍵盤上點擊F10進行添加。然後,通常可以將鼠標懸停在變量或項目上,並顯示它的值。如果不存在,那可能是問題的一部分。這裏有一個超級簡單的Vb.NET控制檯應用程序,用StreamWriter打印出一個文件。在這個例子中,沒有錯誤捕獲,所以只要確保你有C:\ Test文件夾存在。

Imports System.IO 

Public Sub WriteTextToFile(text As String, location As String) 
    Using sw = New StreamWriter(location) 
     sw.Write(text) 
    End Using 
    End Sub 

    Sub Main() 
    Dim someText = "I am just some text" 
    Dim someMoreText = "I am more text" 

    Dim combined = someText + Environment.NewLine + someMoreText 

    WriteTextToFile(combined, "C:\Test\Test.txt") 
    End Sub