vbA操作word文档自动生成txt文件 ,求,谢谢
一个word文档有很多行数据(由英语和汉字组成),要每9行数据生成一个txt文件,txt文件名依次按生成顺序名命为“文件1”“文件2”...... 。
(注:如果最后的数据不够9行的,也要把剩下的数据自动生成一个txt文件) 谢谢各位
[解决办法]
把fs拉出循环体了
Sub test() Dim fs As Object Dim a Dim doc As Document Dim txtPath, txtFullName As String Dim i, docLines As Long txtPath = "d:\" '文档保存路径 Set doc = Documents("11.doc") Selection.HomeKey Unit:=wdStory docLines = doc.BuiltInDocumentProperties(wdPropertyLines).Value Set fs = CreateObject("Scripting.FileSystemObject") For i = 1 To Round(docLines / 9 + 0.5) txtFullName = txtPath & "文件" & i & ".txt" Set a = fs.CreateTextFile(txtFullName, True) Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=9 * (i - 1) + 1, Name:="" Selection.MoveDown Unit:=wdLine, Count:=9, Extend:=wdExtend a.WriteLine (Selection.Text) a.Close Set a = Nothing Next Set fs = Nothing End Sub