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

vfpbu 2011-12-15 05:54:07
一个word文档有很多行数据(由英语和汉字组成),要每9行数据生成一个txt文件,txt文件名依次按生成顺序名命为“文件1”“文件2”...... 。



(注:如果最后的数据不够9行的,也要把剩下的数据自动生成一个txt文件) 谢谢各位
...全文
417 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
vfpbu 2011-12-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 asyn_fl 的回复:]

把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……
[/Quote]


谢谢谢谢
asyn_FL 2011-12-17
  • 打赏
  • 举报
回复
Sub test()
Dim fs As Object
Dim a
Dim doc As Document
Dim txtPath, txtName, txtFullName As String
Dim i, docLines As Long

txtPath = "d:\" '文档保存路径
Set doc = Documents("源文档.doc")
Selection.HomeKey Unit:=wdStory
docLines = doc.BuiltInDocumentProperties(wdPropertyLines).Value

For i = 1 To Round(docLines / 9 + 0.5)
Set fs = CreateObject("Scripting.FileSystemObject")
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
Set fs = Nothing
Next

End Sub
asyn_FL 2011-12-17
  • 打赏
  • 举报
回复
把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
vfpbu 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abclm 的回复:]
这个...是可以的
[/Quote]

可以帮帮忙吗
vfpbu 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abclm 的回复:]
这个...是可以的
[/Quote]

我也知道是可以的,但是我不会弄
  • 打赏
  • 举报
回复


这个...是可以的

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧