当前位置: 代码迷 >> VBA >> vbA操作word文档自动生成txt文件 ,求,多谢
  详细解决方案

vbA操作word文档自动生成txt文件 ,求,多谢

热度:8582   发布时间:2013-02-26 00:00:00.0
vbA操作word文档自动生成txt文件 ,求,谢谢
一个word文档有很多行数据(由英语和汉字组成),要每9行数据生成一个txt文件,txt文件名依次按生成顺序名命为“文件1”“文件2”...... 。



(注:如果最后的数据不够9行的,也要把剩下的数据自动生成一个txt文件) 谢谢各位

------解决方案--------------------------------------------------------
把fs拉出循环体了

VB code
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
  相关解决方案