2017-03-01 39 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