txt to HTM, html"txt to the Web page" VBS implementation code _vbs

Source: Internet
Author: User
Microsoft Word is a powerful feature in many text editors. Plus, XP Pro itself supports VBS calls to Word document objects, so it's best to use VBS. Ideas are as follows: VBS OPEN WORD,WORD Open TXT, save as a Web page, exit.

1. VBS calls the Word.Application component

If Word is installed on your computer, it is easy to open Word with a VBS, as follows:

Set objword = CreateObject ("Word.Application")
Objword.visible = True

This enables you to open Word in a visual manner. On the MSDN page {http://msdn.microsoft.com/en-us/library/kw65a0we (vs.80). aspx} We can see that only a subset of two word can be called in the VBS:

Document object to open

Selection Object Default Selection

However, these two subsets also contain subsets, so many features can be used. The Word Document object itself is a VBA component, and the VBS and VBA have distinct boundaries, so although the VBS can invoke the Word document object, it is an unknown domain. Fortunately, some of Microsoft's words and some of the short stories can probably know usage.

2. Use Word instance to open txt

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 that a TXT has been opened, format=5 that TXT is Unicode:

─────────────────────────────────────────────────────────────────
Wdopenformatallword 6 Microsoft Office Word format that is backward compatible with earlier versions of Word.
wdOpenFormatAuto 0 Existing format.
wdOpenFormatDocument 1 Word format.
Wdopenformatencodedtext 5 encoded text format.
WDOPENFORMATRTF 3 RTF format.
wdOpenFormatTemplate 2 is used as a Word template.
wdOpenFormatText 4 Text format that is not encoded.
wdOpenFormatUnicodeText 5 Unicode Text format.
wdOpenFormatWebPages 7 HTML format.
Wdopenformatxml 8 XML format.
Wdopenformatallwordtemplates 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 for macros.
Wdopenformatxmltemplate XML template format.
Wdopenformatxmltemplatemacroenabled 12 enables the XML template format for macros.
─────────────────────────────────────────────────────────────────

Since word opens TXT with a default font size of 10.5 points, this value is small in the Web page, the appropriate value is 13~15 points, so you need to change the font size, and also need to change the default Western font, where you choose--times New Roman:

Set ORange = Objdoc.range ()
ORange.Font.Size = "13"
ORange.Font.Name = "Times New Roman"

Then the first line to apply the "heading One" style, if the first action blank line then use the editor to make it not empty, in fact, can also be implemented in the VBS, but the amount of code to increase, which is cumbersome to debug:

Set oselection = objword.selection
Oselection.homekey wdstory, WdMove
Oselection.style = ObjWord.ActiveDocument.Styles ("Heading 1")

After the change is complete, Save:

Objdoc.saveas OPath & ". htm", 10
Objdoc.close

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, and preserves line breaks.
Wdformatencodedtext 7 encoded text format.
wdformatfilteredhtml 10 Filtered HTML format.
wdFormatHTML 8 Standard HTML format.
WDFORMATRTF 6 RTF format.
wdFormatTemplate 1 Word template format.
Wdformattext 2 Microsoft Windows text format.
Wdformattextlinebreaks 3 Windows Text format, and preserves line breaks.
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 Word Default document file format. For Microsoft Office Word 2007, this is the DOCX format.
Wdformatpdf PDF format.
WDFORMATTEMPLATE97 1 Word 97 template format.
Wdformatxmldocument XML document format.
Wdformatxmldocumentmacroenabled 13 enables the XML document format for macros.
Wdformatxmltemplate XML template format.
Wdformatxmltemplatemacroenabled 15 enables the XML template format for macros.
Wdformatxps XPS format.
─────────────────────────────────────────────────────────────────

Word instance exit:

Objword.quit

3. Complete code
Copy Code code as follows:

' ********************************************************************************
' TXT goto page, only for conversion demo
' 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 ("Heading 1")

OPath = Split (strfile, ".") (0)

Objdoc.saveas OPath & ". htm", 10
Objdoc.close

Objword.quit

If you want to achieve a lot of text conversion, it is recommended to generate a TXT file list, and then use the FSO to invoke the contents, in turn.

Finally: the quality of the conversion of the method is relatively high, but also with some drawbacks, it is a lot of garbage code, the resulting Web page file is very large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.