word文档批处理 用vba

数百份文档,所有文档内容开头增加一段相同内容。求用vba批处理,,,成功后追加100
不是这,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,我要代码

2.你提供的不能用啊,老是显示语句未结束

VBA代码如下:
Sub AddStr()
Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
With myDialog
.Filters.Clear
.Filters.Add "所有 WORD 文件", "*.doc", 1
.AllowMultiSelect = True
If .Show <> -1 Then Exit Sub
For Each oFile In .SelectedItems
Set oDoc = Documents.Open(FileName:=oFile, Visible:=False, PasswordDocument:=OldPW)
With oDoc
.Content.InsertBefore "这是加入的文本" & vbCr
.Close True '关闭文档
End With
Next oFile
End With
End Sub

说明:使用该方法的好处是灵活,不用指定固定的目录来搜寻目标文档,直接运行代码,会自动弹出选择目录或哪些文档的对话框,非常方便。只需修改要加入的内容即可。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-12-07
Private Sub test()
Dim strFile As String
Dim mypath As String

mypath = "C:\work\" '这里设定的是你说的数百份文档所在的文件夹,注意最后的\,一定要有

strFile = Dir(mypath, vbNormal)
Do While strFile <> ""
If Right(strFile, 4) = ".doc" Then
Dim objDoc As Document
Set objDoc = Documents.Open(mypath & strFile)
Selection.TypeText Text:="input what you want" '这里写你想要在开头增加的内容
Selection.TypeParagraph
objDoc.Close (True)
End If

strFile = Dir
Loop

End Sub

新建一个word文档,把这段代码放进去,执行即可

那你留个邮箱 我给你发过去
第2个回答  2010-12-06
利用word中的宏,先录制宏,首先操作一遍,宏会记录这个过程,然后运行这个宏就OK了。