Word VBA宏中的内存泄漏
我有一个宏用于将多个Word文档转换为HTML格式:Word VBA宏中的内存泄漏,vba,ms-word,Vba,Ms Word,我有一个宏用于将多个Word文档转换为HTML格式: Sub macro1() Dim objCC As ContentControl Dim dataline As String Dim doc As Document Open "D:\Data\find1" For Input As #1 While Not eof(1) Line Input #1, dataline Debug.Print dataline Set doc = Documents.Open(dat
Sub macro1()
Dim objCC As ContentControl
Dim dataline As String
Dim doc As Document
Open "D:\Data\find1" For Input As #1
While Not eof(1)
Line Input #1, dataline
Debug.Print dataline
Set doc = Documents.Open(dataline)
Do While doc.ContentControls.Count > 0
For Each objCC In doc.ContentControls
objCC.Delete False
Next
Loop
doc.SaveAs ActiveDocument.Path + "/" + ActiveDocument.Name + ".html", wdFormatHTML
doc.Close
Wend
Close #1
End Sub
文档列表位于文件
find1
中。我有大约20000个文档,但在转换了大约1000个文件后,内存已满,系统挂起。有办法避免吗?也许你应该尝试使用FilesystemObject,例如
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(fileSpec, ForReading)
strContents = objFile.ReadAll
' and so on
objFile.Close
正如我在评论中提到的,这种内存泄漏与Office打开/关闭文件的方式有关-如果您打开并关闭一个文件,即使没有更改或保存它,某些数据仍会留在内存中,在不关闭应用程序的情况下无法转储 我怀疑(但无法确认)它源自某种“功能”,使重新打开文件的速度稍快一些 现在,正如我前面所说,您可以通过关闭应用程序来释放内存,所以,这就是我们要做的!如果我们延迟将Word绑定到另一个Office应用程序(Excel/Powerpoint/Outlook),那么我们可以关闭并重新打开它
Sub macro1()
Dim objCC As Object 'Late Binding, must be Object
Dim dataline As String
Dim doc As Object 'Late Binding, must be Object
Dim lineCounter AS Long: lineCounter = 0 'So that we can keep track of files!
Dim MSWord AS Object 'Late Binding, must be Object
Set MSWord = CreateObject("Word.Application") 'Create an instance of Word
'MSWord.Visible=True 'OPTIONAL LINE! Makes Word visible, default is False
Open "D:\Data\find1" For Input As #1
While Not eof(1)
Line Input #1, dataline
Debug.Print dataline
Set doc = MSWord.Documents.Open(dataline) 'Open with the correct Application
Do While doc.ContentControls.Count > 0
For Each objCC In doc.ContentControls
objCC.Delete False
Next objCC
Loop
doc.SaveAs MSWord.ActiveDocument.Path + "/" + MSWord.ActiveDocument.Name + ".html", wdFormatHTML
doc.Close
lineCounter = lineCounter +1 'Count processed documents
If (lineCounter mod 100) = 0 Then 'Every 100 documents - adjust as necessary
'We need to destroy any objects associated with Word to close it safely
Set objCC = Nothing
Set doc = Nothing
MSWord.Quit 'Close Word, to free the junk memory
DoEvents 'Check in with Windows - we haven't crashed, honest!
Set MSWord = CreateObject("Word.Application") 'Create a new instance of Word
'MSWord.Visible=True 'OPTIONAL LINE! Makes Word visible, default is False
End If
Wend
Close #1
Set objCC = Nothing
Set doc = Nothing
MSWord.Quit 'Close Word, for the final time
End Sub
可能您需要在
doc之后设置doc=nothing
。关闭如果您从循环中注释掉某些功能部分会发生什么情况(当循环时执行操作,将另存为)?独立于此,我会为每个
循环删掉整个,只使用doc.ContentControls(doc.ContentControls.Count-1)。删除
。这听起来与我在Excel中遇到的问题非常相似:当打开文档,然后再次关闭它时,Office不会释放所有内存。清除它的唯一方法是完全关闭Excel,然后再次打开它。(您可以通过以只读方式打开文档来挤出更多的内存?@Chronocidal,但如果以只读方式打开文件,我将无法删除内部循环中的ContentControl?@Cindymister否,插入set doc=Nothing
并启动后,已用内存线性增加约10MB/s。如前所述,该建议在问题上下文中没有意义。你能解释一下为什么你认为这是问题的答案吗?也许开放方法会导致内存泄漏。读过之后,这是一个松散的提议