请问怎样批量把一个txt文件中的“001”,“002”,“003”……变成“第一章”,“第二章”,“第三章”…

最好用word,用别的方法也可以。注意一定要是批量,不能挨个选,因为有几百个章节!万分感谢。

用Word打开该txt文件,Alt+F11打开VBA宏代码编辑器,将如下代码粘贴进去后,按F5运行即可。
'====代码开始===
Sub NumToHanzi()
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
l = InputBox("请输入待查找数字字符串的长度,为大于0的正整数。", "消息", "3")
l = Trim(Str(Int(Val(l))))
If Val(l) < 1 Then l = "3"
Pos = -1
Post = -1
Do Until Pos = Selection.Start
With Selection.Find
.Text = "[0-9]{" + l + "}"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Again:
Selection.Find.Execute
If Post = -1 Then
Post = Selection.Start
ElseIf Post = Selection.Start Then
Exit Do
End If
Selection.MoveRight
Ch = Selection.Text
If InStr("0123456789", Ch) Then
GoTo Again
Else
Selection.MoveLeft , Val(l)
Selection.Find.Execute
Selection.MoveLeft , 2
Ch = Selection.Text
If InStr("0123456789", Ch) Then
Selection.MoveRight , l + 1
GoTo Again
Else
Selection.MoveRight
Selection.Find.Execute
End If
End If
With Selection.Find
MyNum = Trim(Str(Val(Selection.Text)))
mystr = ""
MyUnit = ""
ln = Len(MyNum)
If ln = 2 Then
MyUnit = "十"
ElseIf ln = 3 Then
MyUnit = "百"
ElseIf ln = 4 Then
MyUnit = "千"
ElseIf ln = 5 Then
MyUnit = "万"
End If
Do Until ln = Len(MyNum)
ln = Len(MyNum)
MyNum = Replace(MyNum, "00", "0")
Loop
If ln = Len(MyNum) Then MyUnit = ""
For i = 1 To Len(MyNum)
Mych = Mid(MyNum, i, 1)
Select Case Mych
Case "0": mystr = mystr + "〇"
Case "1": mystr = mystr + "一"
Case "2": mystr = mystr + "二"
Case "3": mystr = mystr + "三"
Case "4": mystr = mystr + "四"
Case "5": mystr = mystr + "五"
Case "6": mystr = mystr + "六"
Case "7": mystr = mystr + "七"
Case "8": mystr = mystr + "八"
Case "9": mystr = mystr + "九"
End Select
Next
mystr = mystr + MyUnit
.Replacement.Text = "第" + mystr + "章"
End With
With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
Pos = Selection.Start
.Find.Execute
Selection.MoveLeft
End With
Loop

ActiveDocument.Save
MsgBox "处理完毕!", vbInformation, "消息"

End Sub

'===代码结束====
本代码由mkw007提供。追问

太谢谢您了。不过来要劳驾您稍微改一下,运行的时候,出现一个对话框:“编译错误:缺少函数或变量”,第九行“Post = -1”蓝色高亮显示。点击对话框“确定”之后, 第一行“Sub NumToHanzi()”黄色高亮显示,左侧有一个黄色箭头。我用的是word2003.真是麻烦您了,谢谢!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-11-07
可以采用Word的VBA宏代码来实现。追问

请问具体怎么做呢?不懂宏代码。谢谢

追答

代码已经提供了,有关文件批量处理的需求 ,可以试试我开发的 文件批量处理百宝箱V8.0 中的相关功能。
我用的是Word2000,没有Word2003,无法调试修改。在Word2000下,以上宏代码没问题!
要不干脆将第九行删除即可。

本回答被提问者采纳