[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 如何对word文档内容进行批量添加处理?

D:\作业\ 路径文件夹下有一堆word文档,文件类型为docx

我想给这堆文档批量添加以下内容,

文档内容第10行添加内容 批处理之家   段落右对齐

文档内容第132行添加内容 批处理之家当天日期(月日)   段落左对齐

文档内容第56行添加内容 批处理之家   段落两段对齐,缩进2个字符

望老师给予帮助!!

不知道使用vbs命令如何实现

TOP

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <package xmlns="http://schemas.microsoft.com/WindowsScriptHost">
  3.   <job>
  4.     <script language="VBScript">
  5.       <![CDATA[
  6. ' 指定段落添加内容并格式化
  7. On Error Resume Next
  8. Const wdReplaceAll = 2
  9. Const wdStory = 6
  10. Const wdRed = 6
  11. Const wdAuto = 0
  12. Const wdCharacter = 1
  13. Const wdCollapseEnd = 0
  14. Const wdCollapseStart = 1
  15. Const wdAlignParagraphLeft = 0
  16. Const wdAlignParagraphCenter = 1
  17. Const wdAlignParagraphRight = 2
  18. Const wdAlignParagraphJustify = 3
  19. Const wdAlignParagraphDistribute = 4
  20. Const wdAlignParagraphJustifyMed = 5
  21. Const wdAlignParagraphJustifyHi = 7
  22. Const wdAlignParagraphJustifyLow = 8
  23. Const wdAlignParagraphThaiJustify = 9
  24. Const conExtension = "|docx|"
  25. Const conSrcDir = "d:\test\word\作业" ' 作业目录
  26. Const conRecurse = 0 '是否递归子目录,是=1,否=0
  27. ' 强制以cscript.exe运行 WSH
  28. RunasScriptHost "cscript.exe"
  29. WScript.StdOut.WriteLine "脚本执行中,请稍等...,不要重复执行脚本."
  30. Dim dtm,strDtm,wordApp,fso,srcDir,dstDir
  31. dtm = Now
  32. Set fso = CreateObject("Scripting.FileSystemObject")
  33. Set wordApp = CreateObject("Word.Application")
  34. wordApp.DisplayAlerts = False
  35. If Err.Number <> 0 Then
  36.   MsgBox Err.Description
  37.   WScript.Quit 1
  38. End If
  39. srcDir = fso.GetAbsolutePathName(conSrcDir)
  40. If Not fso.FolderExists(srcDir) Then
  41.   WScript.Echo "目录: " & srcDir & " 不存在."
  42. Else
  43.   GenFolder fso.GetFolder(srcDir)
  44. End If
  45. wordApp.Quit
  46. MsgBox "Done."
  47. Set wordApp = Nothing
  48. Set fso = Nothing
  49. Sub GenFolder(oFolder)
  50.   On Error Resume Next
  51.   Dim oFile,oSubFolder
  52.   For Each oFile In oFolder.Files
  53.     If InStr(1,conExtension, "|" & fso.GetExtensionName(oFile.Name) & "|", vbTextCompare) > 0 Then
  54.       GenFile oFile
  55.     End If
  56.   Next
  57.   If conRecurse = 1 Then
  58.     For Each oSubFolder In oFolder.SubFolders
  59.       GenFolder oSubFolder
  60.     Next
  61.   End If
  62. End Sub
  63. Sub GenFile(oFile)
  64.   On Error Resume Next
  65.   Dim docSrc,orange,oPara
  66.   Set docSrc = wordApp.Documents.Open(oFile.Path)
  67.   If docSrc Is Nothing Then Exit Sub
  68.   Set oPara = docSrc.Paragraphs(10)
  69.   if not oPara is nothing then
  70.   With opara
  71.     .Range.InsertBefore "批处理之家"
  72.     .Alignment = wdAlignParagraphRight
  73.   End With
  74.   end if
  75.   set opara = nothing
  76.   set opara = docSrc.Paragraphs(132)
  77.   if not opara is nothing then
  78.   With opara
  79.     Set orange = .Range
  80.     orange.Collapse wdCollapseStart
  81.     orange.InsertBefore "批处理之家"
  82.     orange.Collapse wdCollapseEnd
  83.     orange.InsertDateTime "yyyy-MM-dd", False
  84.     .Alignment = wdAlignParagraphLeft
  85.   End With
  86.   end if
  87.   set opara = nothing
  88.   set opara = docSrc.Paragraphs(56)
  89.   if not opara is nothing then
  90.   With opara
  91.     .Range.InsertBefore "批处理之家"
  92.     .Alignment = wdAlignParagraphDistribute
  93.     .FirstLineIndent = 0
  94.     .IndentFirstLineCharWidth 2
  95.   End With
  96.   end if
  97.   set opara = nothing
  98.   docSrc.Save
  99.   docSrc.Close
  100. End Sub
  101.   Sub RunasScriptHost(strWSH)
  102.     ' runas cscript.exe or wscript.exe
  103.     On Error Resume Next
  104.     If IsNull(strWSH) Or Not (StrComp(strWSH,"cscript.exe",vbTextCompare) = 0 Or StrComp(strWSH,"wscript.exe",vbTextCompare) = 0) Then
  105.       Exit Sub
  106.     End If
  107.     Dim fso
  108.     Set fso = CreateObject("Scripting.FileSystemObject")
  109.     If StrComp(fso.GetFileName(WScript.FullName),strWSH,vbTextCompare) <> 0  Then
  110.       Dim str,arg,shell
  111.       Set shell = CreateObject("Shell.Application")
  112.       str = ""
  113.       For Each arg In WScript.Arguments
  114.         str = str & " """ & arg & """"
  115.       Next
  116.       shell.ShellExecute strWSH,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "open", 1
  117.       Set shell = Nothing
  118.       Set fso = Nothing
  119.       WScript.Quit
  120.     Else
  121.       Set fso = Nothing
  122.     End If
  123.   End Sub
  124. ]]>
  125.     </script>
  126.   </job>
  127. </package>
复制代码
以utf8编码 保存为 a.wsf
微信:flashercs
QQ:49908356

TOP

返回列表