VBA:将多个Word文件组合成一个后,Microsoft Word进程不会退出【JAVA教程】

!
也想出现在这里? 联系我们
信息

VBA:将多个Word文件组合成一个后,Microsoft Word进程不会退出,第1张

概述VBA:将多个Word文件组合成一个后,Microsoft Word进程不会退出

我正在尝试将多个Word文件合并为一个。 我正在MS Excel中使用VBA例程。 Word文件都在一个名为“文件”的文件夹中,我想在一个文件夹中创build一个新文件“combinedfile.docx”。 我面临的问题是在合并文件后(不pipe它是否在VBA函数执行后退出),Word过程的行为。 在一些机器上,这个过程工作正常(除了页面2和最后一页为空白),而在其他一些机器上,合并的文档包含一个空白页面,进程pipe理器显示由VBA函数启动的Word进程仍然是运行。

我不习惯于VBA编程,正如你可以在下面的代码中看到的,我不知道正确的方式来closures打开的文档,并退出一个开放的Word过程。 如果有人可以看看我已经做了什么,并build议一种方法来解决这个问题,这将是非常有益的。

我也有兴趣知道这是否合并几个Word文件的正确方法。 如果有更好的方法,请让我知道。

\’the flow: \’ start a word process to create a blank file \”combinedfile.docx\” \’ loop over all documents in \”files\” folder and do the following: \’ open the file,insert it at the end of combinedfile.docx,then insert pagebreak \’ close the file and exit the word process filesdir = ActiveWorkbook.Path + \”\” + \”files\” thisdir = ActiveWorkbook.Path + \”\” singlefile = thisdir + \”combinedfile.docx\” \’if it already exists,delete If fileExists(singlefile) Then SetAttr singlefile,vbnormal Kill singlefile End If Dim wordapp As Word.Application Dim singledoc As Word.document Set wordapp = New Word.Application Set singledoc = wordapp.documents.Add wordapp.Visible = True singledoc.SaveAs filename:=singlefile singledoc.Close \’i do both this and the line below (is it necessary?) Set singledoc = nothing wordapp.Quit Set wordapp = nothing Joinfiles filesdir + \”*.docx\”,singlefile Sub Joinfiles(alldocs As String,singledoc As String) Dim wordapp As Word.Application Dim doc As Word.document Set wordapp = New Word.Application Set doc = wordapp.documents.Open(filename:=singledoc) Dim filesdir As String filesdir = ActiveWorkbook.Path + \”\” + \”files\” docpath = Dir(alldocs,vbnormal) While docpath \”\” doc.Bookmarks(\”EndOfDoc\”).Range.Insertfile (filesdir + docpath) doc.Bookmarks(\”EndOfDoc\”).Range.InsertBreak Type:=wdPageBreak docpath = Dir Wend doc.Save doc.Close Set doc = nothing wordapp.Quit Set wordapp = nothing End Sub

是否有可能在linux上查看Excel文件

使用Apache POI打开.xlsx文件会导致出现NoClassDefFoundError InvalIDFormatExceptionexception

从excel文件读取cpu使用率达到100%?

用通用名称部分打开文件

运行Excel之前dynamic设置环境variables的正确方法是什么?

从两列随机生成独特的组合

从命令提示符中提取MS Excel电子表格中单个单元格的值

Excel保存不带换行符的制表符分隔文件(UNIX / Mac OS X)

在batch file中用批处理脚本与vbscript命令问题

我可以使用oleDbConnection Excel ACE驱动程序从不可查找的System.IO.Stream而不是文件读取Excel文件吗?

我建议按以下方式优化您的代码:

只打开WordApp一次,并将文件移入,而不关闭/重新打开

没有必要先杀掉combineddoc,它会被新文件覆盖

不需要Word.document对象,所有的都可以在Word.Application对象中完成

所以代码变得更简单了:

Sub Merge() Dim WordApp As Word.Application Dim filesDir As String,ThisDir As String,Singlefile As String,DocPath As String Dim FNArray() As String,IDx As Long,Jdx As Long \’ NEW 11-Apr-2013 filesDir = ActiveWorkbook.Path + \”\” + \”files\” ThisDir = ActiveWorkbook.Path + \”\” Singlefile = ThisDir + \”combinedfile.docx\” Set WordApp = New Word.Application \’ NEW 11-Apr-2013 START \’ read in into array IDx = 0 ReDim FNArray(IDx) FNArray(IDx) = Dir(filesDir & \”*.docx\”) do while FNArray(IDx) <> \”\” IDx = IDx + 1 ReDim Preserve FNArray(IDx) FNArray(IDx) = Dir() Loop ReDim Preserve FNArray(IDx – 1) \’ to get rID of last blank element BubbleSort FNArray \’ NEW 11-Apr-2013 END With WordApp .documents.Add .Visible = True \’ REMOVED 11-Apr-2013 DocPath = Dir(filesDir & \”*.docx\”) \’ REMOVED 11-Apr-2013 do while DocPath <> \”\” \’ REMOVED 11-Apr-2013 .Selection.Insertfile filesDir & DocPath \’ REMOVED 11-Apr-2013 .Selection.TypeBackspace \’ REMOVED 11-Apr-2013 .Selection.InsertBreak wdPageBreak \’ REMOVED 11-Apr-2013 DocPath = Dir \’ REMOVED 11-Apr-2013 Loop \’ NEW 11-Apr-2013 START For Jdx = 0 To IDx – 1 .Selection.Insertfile filesDir & FNArray(Jdx) .Selection.TypeBackspace .Selection.InsertBreak wdPageBreak Next Jdx \’ NEW 11-Apr-2013 END .Selection.TypeBackspace .Selection.TypeBackspace .Selection.document.SaveAs Singlefile .Quit End With Set WordApp = nothing End Sub \’ NEW 11-Apr-2013 START Sub BubbleSort(Arr) Dim strTemp As String Dim IDx As Long,Jdx As Long Dim VMin As Long,VMax As Long VMin = LBound(Arr) VMax = UBound(Arr) For IDx = VMin To VMax – 1 For Jdx = IDx + 1 To VMax If Arr(IDx) > Arr(Jdx) Then strTemp = Arr(IDx) Arr(IDx) = Arr(Jdx) Arr(Jdx) = strTemp End If Next Jdx Next IDx End Sub \’ NEW 11-Apr-2013 END

编辑2013年4月11日删除代码添加数组和原始注释bubblesort逻辑,以保证文件按字母顺序检索

总结

以上是内存溢出为你收集整理的VBA:将多个Word文件组合成一个后,Microsoft Word进程不会退出全部内容,希望文章能够帮你解决VBA:将多个Word文件组合成一个后,Microsoft Word进程不会退出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞74 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容