新代码略作修正如下:-
- on error Resume next
- '/*/////////////////////配置信息////////////////////////////////////
- FontSize=16 '字体大小
- FontName="黑体" '字体名称
- Bold=True '是否粗体:是则为True,否为false
- TextColumnNum=1 'Word页面栏数
- LineSpacing = 21 '行距固定值
- TopMargin =2 '上边距
- BottomMargin =2 '下边距
- LeftMargin =2 '左边距
- RightMargin =2 '右边距
- isHeader=false '是否设置页眉,是为True,否为false
- '/*////////////////////////////////////////////////////////////////
- msg="注意:程序运行期间,请不要操作word!" & vbcrlf & vbcrlf
- msg=msg & "使用方法:将本程序拷贝到待处理txt文件所在目录,运行即可!"
- msg=msg & vbcrlf & vbcrlf & "开始处理?"
- CH=msgbox(msg,vbokcancel,"Txt2Word")
- if CH<>1 then wscript.quit
- Const ForReading = 1, ForWriting = 2
- Set FSO = CreateObject("Scripting.FileSystemObject")
- set FF=FSO.getfolder(".")
- set FC=FF.files
- set WordApp=CreateObject("word.application")
- WordApp.visible=Visible
- WordApp.Documents.Add
- set MyWord=WordApp.Activedocument
- MyWord.Sections(1).Footers(1).PageNumbers.Add.Alignment=1 '页脚居中对齐
- Myword.Sections(1).Headers(1).Range.ParagraphFormat.Alignment = 1 '页眉居中对齐
- '/*////////////////////对word格式进行设置////////////////////
- With MyWord.Content.Font
- .Size = FontSize
- .Name = FontName
- .Bold = Bold
- End With
- With MyWord.PageSetup
- .TopMargin =TopMargin * 28.35
- .BottomMargin =BottomMargin * 28.35
- .LeftMargin =LeftMargin * 28.35
- .RightMargin =RightMargin * 28.35
- End With
- MyWord.PageSetup.TextColumns.SetCount TextColumnNum
- With MyWord.Content.ParagraphFormat
- .LineSpacingRule = 4
- .LineSpacing = LineSpacing
- End With
- '/*//////////////////////////////////////////////////////////
- FolderPath=FF.path
- For each fl in FC
- ext=Lcase(fso.GetExtensionName(fl))
- if ext="txt" then
- Set f = fso.OpenTextFile(fl, ForReading)
- FirstLine=f.ReadLine
- if isHeader=True then
- Myword.Sections(1).Headers(1).Range.text=FirstLine
- else
- WordApp.ActiveWindow.ActivePane.View.SeekView =9
- WordApp.ActiveWindow.ActivePane.View.SeekView =0
- end if
- FileContent=FirstLine & vbcrlf & f.readall
- FileName=split(fl.name,".")
- f.close
- MyWord.Content.text=FileContent
-
- MyWord.SaveAs FolderPath & "\" & FileName(0) & ".doc"
- wscript.sleep 1000
- end if
- Next
- WordApp.quit(0)
- msgbox "恭喜你,转换完成!",vbokonly+vbinformation,"Txt2Doc"
- set FC=nothing
- set FF=nothing
- Set FSO=nothing
复制代码
|