Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vbs to transfer TXT to HTM and HTML

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use vbs to achieve TXT to HTM, HTML", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use vbs to achieve TXT to HTM, HTML" this article.

Among many text editors, Microsoft's Word is very powerful. And XP PRO itself supports VBS to call Word document objects, so it's best to use VBS. The idea is as follows: VBS opens Word,Word, opens TXT, saves as web page, and exits.

1. VBS calls Word.Application component

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

Set objWord = CreateObject ("Word.Application")

ObjWord.Visible = True

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

Document objects opened by Document object

Selection object default selection object

However, these two subsets also contain subsets, so a lot of features are available. Word document object itself is a component of VBA, and there is a clear boundary between VBS and VBA, so although VBS can call Word document object, how to use it is unknown. Fortunately, we can get a general idea of how to use it from some words and short stories from Microsoft.

two。 Open TXT using a 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 that you have opened a TXT, Format=5 means that TXT is Unicode:

WdOpenFormatAllWord 6 is a backward compatible Microsoft Office Word format with previous 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 unencoded text format.

WdOpenFormatUnicodeText 5 Unicode text format.

WdOpenFormatWebPages 7 HTML format.

WdOpenFormatXML 8 XML format.

WdOpenFormatAllWordTemplates 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.5lb after Word opens TXT, this value is very small in the web page, and the appropriate value is 13015lb, so you need to change the font size, and you also need to change the default western font. Choose-- 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 behavior is a blank line, then use the editor to make it not empty. In fact, it can also be implemented in VBS, but the amount of code increases, which is troublesome and disadvantageous to 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

ObjDoc.Close

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 newline characters retained.

WdFormatEncodedText 7 encoded text format.

HTML format filtered by wdFormatFilteredHTML 10.

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 with newline characters retained.

WdFormatUnicodeText 7 Unicode text format.

WdFormatWebArchive 9 Web file 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 exits:

ObjWord.Quit

3. Complete code

The copy code is as follows:

'*

'TXT turns to the web page for conversion demonstration only

'Created By City of Thousand feathers / 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

ObjDoc.Close

ObjWord.Quit

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

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

The above is all the contents of the article "how to use vbs to transfer TXT to HTM and HTML". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • What is the HTML comment tag?

    This article mainly introduces the relevant knowledge of "what is the HTML comment tag". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "what is the HTML comment tag" can help you solve the problem. HTML comment:

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report