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


Among many text editors, Microsoft word is very powerful. Moreover, XP Pro itself supports VBS to call word document objects, so using VBS is the best. The idea is as follows: open word in VBS, open TXT in word, save as a 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 allows word to be opened visually. On MSDN’s page{ http://msdn.microsoft.com/en-us/library/kw65a0we (VS.80). On 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 there is an obvious boundary between VBS and VBA. Therefore, although VBS can call word document object, how to use it is an unknown field. Fortunately, I can get a general idea of the usage from some Microsoft phrases and short stories.

2. open TXT using word instance

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

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

Now a TXT has been opened. Format=5 means that TXT is Unicode:

Wdopenformatalword 6 is a Microsoft Office Word format that is backward compatible with previous versions of word.
Wdopenformatauto 0 existing format.
Wdopenformatdocument 1 word format.
Wdopenformatencodedtext 5 encodes the text format.
Wdopenformartf 3 RTF format.
Wdopenformattemplate 2 is used as a word template.
Wdopenformattext 4 uncoded text format.
Wdopenformatunicodetext 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.
Wdopenformatxmldocument 9 XML document format.
Wdopenformatxmldocumentmacroenabled 10 enables the XML document format of 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. Therefore, it is necessary to change the font size and the default Western Font. Here, select –times New Roman:

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

Then, the “title one” style is applied to the first line. If the first line is empty, the editor is used to make it not empty. In fact, it can also be implemented in VBS. However, the amount of code increases, which is very troublesome and unfavorable for debugging:

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

When the changes are complete, save:

objDoc.SaveAs oPath & “.htm”, 10

The following 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 preserved.
Wdformatencodedtext 7 encoded text format.
Wdformatfilteredhtml 10 filtered HTML format.
Wdformathtml 8 standard HTML format.
Wdformartf 6 RTF format.
Wdformattemplate 1 word template format.
Wdformattext 2 Microsoft Windows text format.
Wdformattextlinebreaks 3 windows text format with line breaks preserved.
Wdformatunicodetext 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 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 of 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 codes are as follows:

‘ ********************************************************************************
‘txt to web page for conversion demonstration only
‘created by thousand feather 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 = objWord. ActiveDocument. Styles (“title 1”)

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

objDoc.SaveAs oPath & “.htm”, 10


If you want to realize batch multi text conversion, it is recommended to generate a TXT file list, and then call the contents with FSO to convert them in turn.

Finally: the quality of this method is relatively high, but it also has some disadvantages, that is, there are a lot of junk code and the generated web page files are very large.

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]