找回密码
 加入
搜索
查看: 2092|回复: 7

[AU3基础] [已解决]求高手修改下这个word文档合并的VBS代码

[复制链接]
发表于 2017-8-23 17:11:35 | 显示全部楼层 |阅读模式
本帖最后由 king8462 于 2017-8-25 21:24 编辑

vbs代码如下:

Call CombinAllDocFiles("D:\doc", "D:\Combined.doc")
Sub CombinAllDocFiles(strRootPath, strTargetFileName)
    On Error Resume Next
    Dim oApp, fso, oFolder, oFile
    Set oApp = CreateObject("Word.Application")
    oApp.Visible = True
   
    oApp.Documents.Add
    oApp.ActiveWindow.View.Type = 2         ' wdOutlineView
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(strRootPath)
    For Each oFile In oFolder.Files
        oApp.ActiveDocument.Subdocuments.AddFromFile oFile.Path
    Next
    oApp.ActiveDocument.Subdocuments.Delete
    oApp.ActiveWindow.View.Type = 3         ' wdPrintView
   
    oApp.Selection.GoTo wdGoToPage, , 1
    oApp.Selection.Bookmarks("\Page").Range.Delete
   
    oApp.ActiveDocument.SaveAs strTargetFileName
    oApp.Quit True
    Set oApp = Nothing
    MsgBox "完成!"
End Sub
 楼主| 发表于 2017-8-23 17:13:34 | 显示全部楼层
我自己瞎改了一部分,但有几句不会搞:
#include <Word.au3>
$hb="d:\1.doc"

Global $word = ObjCreate("word.application")
$word.visible = True
    
    $word.Documents.Add
    $word.ActiveWindow.View.Type = 2         ' wdOutlineView
    Global $fso = ObjCreate("Scripting.FileSystemObject")
    Global $oFolder = $fso.GetFolder("d:\doc")
    For Each oFile In $oFolder.Files
        $word.ActiveDocument.Subdocuments.AddFromFile oFile.Path
    Next
    $word.ActiveDocument.Subdocuments.Delete
    $word.ActiveWindow.View.Type = 3         ' wdPrintView
    
        $word.Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1" '跳转到第1页
    $word.Selection.Bookmarks("\Page").Range.Delete
    
    $word.ActiveDocument.SaveAs $hb
    $word.Quit True
 楼主| 发表于 2017-8-23 17:18:35 | 显示全部楼层
@kevinch 老师看看
 楼主| 发表于 2017-8-23 17:18:37 | 显示全部楼层
本帖最后由 king8462 于 2017-8-25 09:09 编辑

懂word对象的高手帮忙看看啊。
发表于 2017-8-25 16:09:07 | 显示全部楼层
Func  CombinAllDocFiles($strRootPath, $strTargetFileName)
    Local     $wdGoToPage=1
    Dim $oApp, $fso, $oFolder, $oFile
    $oApp = ObjCreate("Word.Application")
    $oApp.Visible = True
    
    $oApp.Documents.Add
    $oApp.ActiveWindow.View.Type = 2         ; wdOutlineView
    $fso = ObjCreate("Scripting.FileSystemObject")
    $oFolder = $fso.GetFolder($strRootPath)
    For  $oFile In $oFolder.Files
        $oApp.ActiveDocument.Subdocuments.AddFromFile($oFile.Path)
    Next
    $oApp.ActiveDocument.Subdocuments.Delete
    $oApp.ActiveWindow.View.Type = 3         ; wdPrintView
   
     
    $oApp.Selection.GoTo($wdGoToPage, Default, 1)
    $oApp.Selection.Bookmarks("\Page").Range.Delete
    
    $oApp.ActiveDocument.SaveAs($strTargetFileName)
    $oApp.Quit(True)
    $oApp = 0
    MsgBox(0,"", "完成!")
EndFunc
经测试通过
发表于 2017-8-25 16:10:17 | 显示全部楼层
wdGoToPage 的值为1

评分

参与人数 1金钱 +10 收起 理由
king8462 + 10 谢谢!

查看全部评分

 楼主| 发表于 2017-8-25 21:23:44 | 显示全部楼层
回复 5# h20040606

高手,多谢了!
发表于 2019-3-6 23:41:19 | 显示全部楼层

合并后页眉的格式丢失
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-23 22:00 , Processed in 0.073649 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表