VBA实现批量修改Word文档的页脚内容
2012-07-05 15:14
543 查看
功能示例:
有很多个doc文档,页脚的电话变了,如原电话是4007339339,现在变成4007168339了,要实现批量替换,可使用此程序。
使用说明:
1、 复制下面程序代码到VBA里后,点“工具”-“宏”-“宏”-“change”-“运行”
2、 输入目录(不要输入根目录,要不速度会很慢)
3、 输入要查找的内容
4、 输入的替换成你要的内容
--------------------------------------------
'下面是程序代码,复制到Word的VBA里
'此子程序放在Word对象里
Option Explicit
Sub change()
Dim s As String
Dim wb As Object
Dim i As Long
Dim load As String
Dim find As String
Dim change As String
load = InputBox("输入要修改页脚的文件夹路径,自动扫描子文件夹-------------垃圾桶丁2009-3-8") '要变更的目录
find = InputBox("输入要查找的页脚内容") '查找的内容
change = InputBox("请问要替换成什么内容?") '替换的内容
Set wb = Application.FileSearch
With wb
.NewSearch
.LookIn = load
.SearchSubFolders = True
.FileName = "*.doc"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
On Error Resume Next
s = .FoundFiles(i)
Call Macro1(s, find, change)
Next i
End If
End With
End Sub
'此子程序放在模块里
Option Explicit
Sub Macro1(s As String, find As String, change As String)
Documents.Open FileName:=s, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.find.ClearFormatting
Selection.find.Replacement.ClearFormatting
With Selection.find
.Text = find '查找的内容
.Replacement.Text = change '替换的内容
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute Replace:=wdReplaceAll
ActiveWindow.Close (wdSaveChanges)
End Sub
有很多个doc文档,页脚的电话变了,如原电话是4007339339,现在变成4007168339了,要实现批量替换,可使用此程序。
使用说明:
1、 复制下面程序代码到VBA里后,点“工具”-“宏”-“宏”-“change”-“运行”
2、 输入目录(不要输入根目录,要不速度会很慢)
3、 输入要查找的内容
4、 输入的替换成你要的内容
--------------------------------------------
'下面是程序代码,复制到Word的VBA里
'此子程序放在Word对象里
Option Explicit
Sub change()
Dim s As String
Dim wb As Object
Dim i As Long
Dim load As String
Dim find As String
Dim change As String
load = InputBox("输入要修改页脚的文件夹路径,自动扫描子文件夹-------------垃圾桶丁2009-3-8") '要变更的目录
find = InputBox("输入要查找的页脚内容") '查找的内容
change = InputBox("请问要替换成什么内容?") '替换的内容
Set wb = Application.FileSearch
With wb
.NewSearch
.LookIn = load
.SearchSubFolders = True
.FileName = "*.doc"
.FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
On Error Resume Next
s = .FoundFiles(i)
Call Macro1(s, find, change)
Next i
End If
End With
End Sub
'此子程序放在模块里
Option Explicit
Sub Macro1(s As String, find As String, change As String)
Documents.Open FileName:=s, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.find.ClearFormatting
Selection.find.Replacement.ClearFormatting
With Selection.find
.Text = find '查找的内容
.Replacement.Text = change '替换的内容
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.find.Execute Replace:=wdReplaceAll
ActiveWindow.Close (wdSaveChanges)
End Sub
相关文章推荐
- asp.net中如何批量导出access某表内容到word文档
- 使用word中的宏实现选中指定类型表格,并且批量修改样式结构
- Excel表格内容批量添加到多个word文档中
- 从Word文档中修改Excel Chart的内容
- Python实现批量将word转html并将html内容发布至网站的方法
- 批量修改word文档并保存
- 从Word文档中修改Excel Chart的内容
- FckEditor中从word文档粘贴过来的内容在点击确定清除格式后直接清除而不是再弹出从MS WORD粘贴对话框的实现
- 在WORD中用VBA实现光标移动与内容选择
- 通过 poi 设置 word 文档 表格内容样式和 页眉页脚样式
- 批量导出access某表内容到word文档
- 在WORD中用VBA实现光标移动与内容选择(转)
- Python实现批量将word转html并将html内容发布至网站的方法
- word文档如何批量修改图片大小
- C#实现WORD文档的内容复制和替换
- springMvcZip下载 压缩流实现批量下载Word文档
- word文档分别批量修改中文与英文字体大小字号等格式
- 批量修改WORD文档密码
- C#修改Word文档内容
- C# 修改word文档里的内容