首页 > 技术文章 > vbs小脚本02-批量修改文件夹内word文件

yuknight 2022-05-19 23:04 原文

批量改变当前目录下的word文件

  • 脚本必须在需要批量修改的文件夹下
  • 脚本需要做如下修改
    • 其范围为7-14,
    • 修改的内容为 CR00000
        objWord.Selection.setRange 7,14
    	objWord.Selection.TypeText "CR00000" 
    
  • 将代码区域内代码粘贴至本地txt文件,并将文件后缀名更改为.vbs
'author:胖头鱼
'time:2022/0421
'选择修改范围时(setRange),对应wps左下角位置-1
Dim files,cpath
Dim wShell,objWord

i = 1

'得到当前文件路径
Set wShell = WScript.CreateObject("WScript.Shell")
cpath = wShell.currentDirectory

'得到word对象
Set objWord = CreateObject("Word.Application")
objWord.Visible = false

'得到当前路径下的文件
set files = getFiles()

For Each mfile In files
	
	If mfile.Name <> "修改.vbs" Then
	
		objWord.Documents.open mfile.path
		objWord.Selection.setRange 7,14
		objWord.Selection.TypeText "CR00000" 
		objWord.ActiveDocument.Save
		objWord.ActiveDocument.Close
		
	End If
	
Next

objWord.quit


set wShell = nothing
set objWord = nothing

MsgBox ("任务结束")

'得到文件对象
Function getFiles()

	dim fsoForFile,fs
    Set fsoForFile = WScript.CreateObject("scripting.filesystemobject")
    Set fs = fsoForFile.getfolder(cpath)
	set getFiles = fs.files
	
End Function

推荐阅读