2012-08-13 57 views
0

我要尋找一個解決以下VB批量.html文件/ URL。我想保存要在Microsoft Word中打開的.html文件。的VBScript批量HTML轉換爲Word

這是我一起工作的代碼,但我需要它爲多個.html文件工作:

Option Explicit 
'Just change these two lines 
Const HTMLFileIn="http://www.example.com" 
Const DocFileOut="H:\Word" 

Dim MyWord,oIE 
Set MyWord=CreateObject("Word.Document") 
Set oIE=CreateObject("InternetExplorer.Application") 
oIE.Navigate HTMLFileIn 
Attend 
oIE.document.body.createTextRange.execCommand("Copy") 
Attend 
MyWord.Content.Paste 
MyWord.SaveAs DocFileOut 
MyWord.Close 
oIE.Quit 
Set oIE=Nothing 
Set MyWord=Nothing 
MsgBox HTMLFileIn & " is now saved as " & DocFileOut 

Sub Attend 
    Wscript.Sleep 500 
    While oIE.busy 
     Wscript.Sleep 1000 
    Wend 
    While oIE.Document.readyState <> "complete" 
     Wscript.Sleep 1000 
    Wend 
End Sub 
+0

在哪裏存儲的聯繫?代碼將如何決定如何命名新的單詞文件? – 2012-08-13 15:43:33

+0

鏈接來自我的網站,我有一個1000頁轉換成單詞,可以將輸入集中到一個文件而不是多個文件? – user1595805 2012-08-13 16:54:37

+0

這是可以理解的,但鏈接保存在哪裏。代碼應該循環檢索這些鏈接...例如文本文件或文件文件?另外你打算如何命名新的單詞文件? – 2012-08-13 16:56:16

回答

0

試試這個。 (久經考驗

'~~> Replace this with the text file which has the links 
Const hLinksFile As String = "C:\Temp\MyLinks.txt" 

Sub Sample() 
    Dim MyData As String, strData() As String 
    Dim i As Long 
    Dim ie As Object 

    '~~> Open text file in one go 
    Open hLinksFile For Binary As #1 
    MyData = Space$(LOF(1)) 
    Get #1, , MyData 
    Close #1 
    strData() = Split(MyData, vbCrLf) 

    Set ie = CreateObject("InternetExplorer.Application") 

    ie.Visible = True 

    Set XML = CreateObject("msxml2.serverxmlhttp.6.0") 

    For i = LBound(strData) To UBound(strData) 
     ie.Navigate "about:blank" 

     With XML 
      .Open "get", strData(i), False 
      .send 
      ie.Document.body.InnerHTML = .responsetext 
     End With 

     Do While ie.readystate <> 4: DoEvents: Loop 

     ie.Document.body.createtextrange.execCommand "Copy" 

     Selection.Paste 

     '~~> Move to the end for the next pasting 
     Selection.EndKey Unit:=wdStory 
     Selection.TypeParagraph: Selection.TypeParagraph 

     Doevents 
    Next i 
End Sub 
+0

謝謝Siddharth,會試試看。 – user1595805 2012-08-13 20:42:39

+0

我得到運行時錯誤:'-2147417848(80010108)對象調用的自動化錯誤已從客戶端斷開連接。調試器突出了以下幾點:ie.Document.body.createtextrange.execCommand「複製」 – user1595805 2012-08-13 20:51:42

+0

你在代碼中做出什麼樣的變化?你是否試圖在循環內退出'ie'? – 2012-08-13 20:53:49