VBS implementation code of “txt to HTML, HTML” txt to web “


Among many text editors, Microsoft word is very powerful. In addition, XP Pro supports VBS to call word document objects, so VBS is the best way. The idea is as follows: open word in VBS, open TXT in word, save as web page, and exit.

1. VBS calls word.application component

If word is installed in the computer, it is easy to open word with VBS, as follows:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

This opens word visually. On the MSDN page {http://msdn.microsoft.com/en-us/library/kw65a0we (vs.80). ASPX}, we can see that only two subsets of word can be called in VBS:

Document object open document object

Selection object default selection object

However, these two subsets also contain subsets, so many functions can be used. Word document object itself is a component of VBA, and VBS and VBA have obvious boundaries, so although VBS can call word document object, how to use it is an unknown field. Fortunately, I can get a general idea of usage from some Microsoft words and short stories.

2. Use word instance to open TXT

After creating a word instance obbword, we should use it to open TXT, as follows:

Set objDoc = objWord.Documents.Open(“e:\temp\1.txt“,Format=5)

Now that a TXT has been opened, format = 5 means that the txt is Unicode:

Wdopenformatalword 6 is a backward compatible Microsoft Office Word format with previous versions of word.
Wdopenformatauto 0 existing format.
Wdopenformatdocument 1 word format.
Wdopenformatencodedtext 5 encodes the text format.
Wdopenformatrtf 3 rich text format.
Wdopenformattemplate 2 is used as a word template.
Wdopenformattext 4 unencoded text format.
Wdopenformatcondetext 5 Unicode text format.
Wdopenformatwebpages 7 HTML format.
Wdopenformatxml 8 XML format.
Wdopenformatalwordtemplates 13 word template format.
Wdopenformatdocument97 1 Microsoft Word 97 document format.
Wdopenformattemplate97 2 word 97 template format.
Wdopenformatxldocument 9 XML document format.
Wdopenformatxmldocumentmacroenabled 10 enables the XML document format for macros.
Wdopenformatxmltemplate 11 XML template format.
Wdopenformatxmltemplatemacroenabled 12 enables the XML template format for macros.

Since the default font size is 10.5 points after word opens TXT, this value is very small in the web page, and the appropriate value is 13-15 points, so you need to change the font size, and also need to change the default Western font, here — Times New Roman:

Set oRange = objDoc.Range()
oRange.Font.Size = “13”
oRange.Font.Name = “Times New Roman”

Then the first line applies the “title one” style. If the first line is empty, then the editor can be used to make it not empty. In fact, it can also be implemented in VBS. However, with the increase of code volume, it is troublesome and disadvantageous to debug.

Set oSelection = objWord.Selection
oSelection.HomeKey wdStory, wdMove
Oselection. Style = obbword. Activedocument. Styles (“title 1”)

When the changes are complete, save:

objDoc.SaveAs oPath & “.htm“, 10

Here is a list of wdsaveformat constants:

Wdformatdocument 0 Microsoft Office Word format.
Wdformatdostext 4 Microsoft DOS text format.
Wdformatdostextlinebreaks 5 Microsoft DOS text format with line breaks reserved.
Wdformatencodedtext 7 encodes the text format.
Wdformatfilteredhtml 10 filtered HTML format.
Wdformatthtml 8 standard HTML format.
Wdformatrtf 6 rich text format.
Wdformattemplate 1 word template format.
Wdformattext 2 Microsoft Windows text format.
Wdformattextlinebreaks 3 windows text format with line breaks.
Wdformatcondetext 7 Unicode text format.
Wdformatwebarchive 9 web archive format.
Wdformatxml 11 extensible markup language (XML) format.
Wdformatdocument97 0 Microsoft Word 97 document format.
Wdformatdocumentdefault 16 word is the default document file format. For Microsoft Office Word 2007, this is the docx format.
Wdformatpdf 17 PDF format.
Wdformattemplate97 1 word 97 template format.
Wdformatxmldocument 12 XML document format.
Wdformatxmldocumentmacroenabled 13 enables the XML document format for macros.
Wdformatxmltemplate 14 XML template format.
Wdformatxmltemplatemacroenabled 15 enables the XML template format for macros.
Wdformatxps 18 XPS format.

Word instance exit:


3. Complete code

Copy codeThe code is as follows:
‘ ********************************************************************************
‘txt to web page for conversion demonstration only
‘created by Qianyu city / September 22, 2009
‘ http://hi.baidu.com/asnahu
‘ ********************************************************************************

Dim objWord,objDoc,oRange,oPath

Const wdStory = 6
Const wdMove = 0
Const wdOpenFormatUnicodeText = 5
Const wdFormatFilteredHTML = 10

StrFile = “E: \ temp \ 1. TXT” ‘the path must be a full path

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True

Set objDoc = objWord.Documents.Open(strFile, Format=5)
Set oRange = objDoc.Range()
oRange.Font.Size = “13”
oRange.Font.Name = “Times New Roman”

Set oSelection = objWord.Selection
oSelection.HomeKey wdStory, wdMove
Oselection. Style = obbword. Activedocument. Styles (“title 1”)

oPath = Split(strFile,”.”)(0)

objDoc.SaveAs oPath & “.htm”, 10


If you want to achieve batch multi text conversion, it is recommended to generate a list of txt files, and then call the contents with FSO, and then convert them in turn.

Finally: the quality of this method is high, but there are also some disadvantages, that is, a lot of junk code, the generated web file is very large.

Recommended Today

Java security framework

The article is mainly divided into three parts1. The architecture and core components of spring security are as follows: (1) authentication; (2) authority interception; (3) database management; (4) authority caching; (5) custom decision making; and;2. To build and use the environment, the current popular spring boot is used to build the environment, and the actual […]