Option Explicit Dim fs As New FileSystemObject'* * * * * * * * * * * * * * * * * * * * * * '* 以下为 HTML 转换为 TXT 子过程 * '* * '* Parameters * '* Name Type Value * '*-----------------------------------------* '* Filename$ String * '* * '* Last updated by XiaoCai 03.09.2000 * '* * * * * * * * * * * * * * * * * * * * * * Sub StripText(FileName$) Dim a As TextStream, b As TextStream, t As String Dim is_tag As Integer, write2file As String, i As Integer Dim x1 As String, x2 As String, x3 As String, x4 As String x1 = """ x2 = "<" x3 = ">" x4 = " " On Error GoTo Err_Handler Screen.MousePointer = 11 Set b = fs.OpenTextFile(FileName$, ForReading, False) Set a = fs.CreateTextFile("e:\aa.txt", True) Do While b.AtEndOfStream <> True t = b.ReadLine write2file = "" For i = 1 To Len(t) Select Case Mid$(t, i, 1) Case "<" is_tag = True Case ">" is_tag = False Case Else If Not is_tag Then write2file = write2file & Mid$(t, i, 1) End Select Next write2file = Replace(write2file, x1, Chr(34), 1, -1, vbBinaryCompare) write2file = Replace(write2file, x2, "<", 1, -1, vbBinaryCompare) write2file = Replace(write2file, x3, ">", 1, -1, vbBinaryCompare) write2file = Replace(write2file, x4, Chr(32), 1, -1, vbBinaryCompare) a.WriteLine (write2file) Loop Exit_Sub: a.Close b.Close Set a = Nothing Set b = Nothing Screen.MousePointer = 0 Exit Sub Err_Handler: MsgBox "Error: " & Error$(Err) Resume Exit_Sub End Sub
呵呵~~~忘了加一句,得先饮用 "Microsoft Scripting RunTime"
我的原创!简单又实用!一个函数!不会可以问哦![email protected] Function HTMLtoText(Text As String)
Dim FinalText As String Dim In_tag As Boolean Dim i As Integer
i = 1 'For i = 1 To Len(Text) Do Until i = Len(Text) + 1
'Debug.Print Mid(Text, i, 1)
Select Case Mid$(Text, i, 1) Case "<" In_tag = True
Case ">" In_tag = False
Case Else
If In_tag = False Then FinalText = FinalText & Mid$(Text, i, 1) 'Debug.Print FinalText
End Select
i = i + 1 Loop
HTMLtoText = FinalText
End Function
怎么没看懂? 添加部件microsoft internet control 加一个WebBrowser到你的窗体,加入下面的代码 Private Sub Command1_Click() WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT End Sub Private Sub Form_Load() WebBrowser1.Navigate "C:\1.htm" End Sub C:\1.htm是你要转的文件 你运行一下试试看
不想跳出对话框你可以这样 Private Sub Command4_Click() Text = WebBrowser1.Document.body.innerText Open "D:\1.txt" For Binary As #1 Put #1, , Text Close #1 End Sub
Dim fs As New FileSystemObject'* * * * * * * * * * * * * * * * * * * * * *
'* 以下为 HTML 转换为 TXT 子过程 *
'* *
'* Parameters *
'* Name Type Value *
'*-----------------------------------------*
'* Filename$ String *
'* *
'* Last updated by XiaoCai 03.09.2000 *
'* * * * * * * * * * * * * * * * * * * * * *
Sub StripText(FileName$)
Dim a As TextStream, b As TextStream, t As String
Dim is_tag As Integer, write2file As String, i As Integer
Dim x1 As String, x2 As String, x3 As String, x4 As String
x1 = """
x2 = "<"
x3 = ">"
x4 = " "
On Error GoTo Err_Handler
Screen.MousePointer = 11
Set b = fs.OpenTextFile(FileName$, ForReading, False)
Set a = fs.CreateTextFile("e:\aa.txt", True)
Do While b.AtEndOfStream <> True
t = b.ReadLine
write2file = ""
For i = 1 To Len(t)
Select Case Mid$(t, i, 1)
Case "<"
is_tag = True
Case ">"
is_tag = False
Case Else
If Not is_tag Then write2file = write2file & Mid$(t, i, 1)
End Select
Next
write2file = Replace(write2file, x1, Chr(34), 1, -1, vbBinaryCompare)
write2file = Replace(write2file, x2, "<", 1, -1, vbBinaryCompare)
write2file = Replace(write2file, x3, ">", 1, -1, vbBinaryCompare)
write2file = Replace(write2file, x4, Chr(32), 1, -1, vbBinaryCompare)
a.WriteLine (write2file)
Loop
Exit_Sub:
a.Close
b.Close
Set a = Nothing
Set b = Nothing
Screen.MousePointer = 0
Exit Sub
Err_Handler:
MsgBox "Error: " & Error$(Err)
Resume Exit_Sub
End Sub
Dim FinalText As String
Dim In_tag As Boolean
Dim i As Integer
i = 1
'For i = 1 To Len(Text)
Do Until i = Len(Text) + 1
'Debug.Print Mid(Text, i, 1)
Select Case Mid$(Text, i, 1)
Case "<"
In_tag = True
Case ">"
In_tag = False
Case Else
If In_tag = False Then FinalText = FinalText & Mid$(Text, i, 1)
'Debug.Print FinalText
End Select
i = i + 1
Loop
HTMLtoText = FinalText
End Function
添加部件microsoft internet control 加一个WebBrowser到你的窗体,加入下面的代码
Private Sub Command1_Click()
WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT
End Sub
Private Sub Form_Load()
WebBrowser1.Navigate "C:\1.htm"
End Sub
C:\1.htm是你要转的文件
你运行一下试试看
Private Sub Command4_Click()
Text = WebBrowser1.Document.body.innerText
Open "D:\1.txt" For Binary As #1
Put #1, , Text
Close #1
End Sub