ハードコードされた URL に対して機能するコードがここにあります。これは、1 つの URL と 1 つのテキスト ファイルに対してのみ機能します。
Sub saveUrl_Test()
Dim FileName As String
Dim FSO As Object
Dim ieApp As Object
Dim Txt As String
Dim TxtFile As Object
Dim URL As String
URL = "www.bing.com"
FileName = "C:\mallet\bing.com.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TxtFile = FSO.OpenTextFile(FileName, 2, True, -1)
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.Navigate URL
While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Wend
Txt = ieApp.Document.body.innerText
TxtFile.Write Txt
TxtFile.Close
ieApp.Quit
Set ieApp = Nothing
Set FSO = Nothing
End Sub
私がやりたいのは、列 B で URL を検索し (おそらく InStr(variable, "http://") をブール値として使用して)、各 Web ページを個別のテキスト ファイルとして保存することです。URL 文字列の一部を使用してテキスト ファイルに名前を付ける方法はありますか? また、Web ページを開かずにテキスト ファイルとして保存する方法はありますか? ウェブページを開くのは、多くの時間を無駄にします。
@MikeD の提案に基づいてこの追加のサブを作成しましたが、while エラーなしで wend を取得します。
Sub url_Test(URL As String, FileName As String)
Dim FSO As Object
Dim ieApp As Object
Dim Txt As String
Dim TxtFile As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TxtFile = FSO.OpenTextFile(FileName, 2, True, -1)
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.Navigate URL
While ieApp.Busy Or ieApp.ReadyState <> 4
DoEvents
Wend
Txt = ieApp.Document.body.innerText
TxtFile.Write Txt
TxtFile.Close
ieApp.Quit
Set ieApp = Nothing
Set FSO = Nothing
End Sub
Sub LoopOverB()
Dim myRow As Long
myRow = 10
While Cells(myRow, 2).Value <> ""
If InStr(1, Cells(myRow, 2).Value, "http:\\", vbTextCompare) Then Call url_Test(Cells(myRow, 2).Value, "C:\mallet\test\" & Cells(myRow, 1).Value & ".txt")
myRow = myRow + 1
Wend
End Sub