PDA

View Full Version : [SOLVED:] WINWORD.exe Maxes CPU on Close - AddIn problem



macdonnie
01-22-2016, 10:44 AM
So I am using Custom UI Editor to make an add-in. The add-in works except for when I close Word and I have executed this section of code to open a new document:


Application.ScreenUpdating = False
Set Doc = Documents.Open(FileName:=filePath, ReadOnly:=True)
Doc.SaveAs2 FileName:=tempFile
Doc.Close
Documents.Open FileName:=tempFile, ReadOnly:=False
Application.ScreenUpdating = True


Set Doc = Nothing


There seems to be an issue with the two documents running under the same WINWORD.exe Process when I close both word documents with the add-in.

On closing both document under the WINWORD.exe process my CPU usage maxes out one core in the task manager.

I tried the following:


Dim AnotherWord As Word.Application
Set AnotherWord = CreateObject("Word.Application")
AnotherWord.Visible = True

Set Doc = AnotherWord.Documents.Open(FileName:=filePath, ReadOnly:=True)


And that fixed my problem, however, the Doc Object is empty and therefore I cannot execute the following code:


Doc.SaveAs2 FileName:=tempFile
Doc.Close
Documents.Open FileName:=tempFile, ReadOnly:=False


1) Does anyone know why I can't run 2 documents under one WINWORD.exe process with the add-in and exit without my CPU core jumping to 100%?
2) Does anyone know how to access another document opened up in a separate WINWORD.exe process? (To execute my last code above)


Some additional info that may help:
1) I am using a callback that executes when the document is opened:




...In a Module:

Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub

... In another sub in a Class:

Option Explicit


Public WithEvents oApp As Word.Application


Private Sub oApp_DocumentOpen(ByVal Doc As Document)

...Code

End Sub

2) As with typical Custom UI I am using callbacks:


Public Sub SetFlag()
Dim mbResult As Integer
mbResult = MsgBox("Do you want to disable some controls on the Ribbon?", vbYesNo)
If mbResult = vbYes Then
TrapFlag = True
Else:
TrapFlag = False
End If
End Sub


'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
'MsgBox "onLoad"
Set Rib = ribbon
End Sub


'I use this Callback for disabling some Controls:
' ViewSlideSorterView
' ViewNotesPageView
' ViewSlideShowReadingView
' ViewSlideMasterView
' ViewHandoutMasterView
' ViewNotesMasterView
' WindowNew
Sub EnableControl(control As IRibbonControl, ByRef returnedVal)
returnedVal = Not TrapFlag 'TrapFlag = True indicates the Application is running.
'MsgBox ("GetEnabled for " & control.Id)
'Debug.Print control.Id & " enabled = " & CStr(returnedVal)
Call RefreshRibbon(control.ID)
End Sub


'I use this Callback for disabling/hiding some tab groups:
' GroupMasterViews
' GroupPresentationViews
Sub VisibleGroup(control As IRibbonControl, ByRef returnedVal)


Dim visibleState As Boolean


If control.ID = "Review_Group" Then
visibleState = True
Else
visibleState = True
End If


returnedVal = visibleState 'Not TrapFlag 'TrapFlag = True indicates the Application is running.
'MsgBox "GetVisible for " & control.Id
Debug.Print control.ID & " enabled = " & CStr(returnedVal)
Call RefreshRibbon(control.ID)
End Sub


Sub CallbackGetPressed(control As IRibbonControl, ByRef pressed)
' Callback getPressed
' Dim documentName As String
' documentName = ActiveDocument.Name
'
' If InStr(documentName, "Book") > 0 And InStr(documentName, "SubSystem") > 0 And InStr(documentName, "ProcedureNumber") > 0 Then
' pressed = pressedState
' Else
' pressed = pressedState
' End If
'
' Call RefreshRibbon(control.ID)


End Sub




Sub RefreshRibbon(ID As String)
xmlID = ID
'MsgBox "Refreshing ribbon for " & ID, vbInformation
If Rib Is Nothing Then
Debug.Print "Error, Save/Restart Word"
Else
Rib.Invalidate
End If
End Sub


Repeated in case you missed it:
1) Does anyone know why I can't run 2 documents under one WINWORD.exe process with the add-in and exit without my CPU core jumping to 100%?
2) Does anyone know how to access another document opened up in a separate WINWORD.exe process? (To execute my last code above)

Thanks,
-Don

SamT
01-22-2016, 11:59 AM
You have an endless loop some where.


See if t it is in the first code sample above

Application.ScreenUpdating = False
Set Doc = Documents.Open(FileName:=filePath, ReadOnly:=True)
Doc.SaveAs2 FileName:=tempFile

MsgBox "Closing old" 'Add for troubleshooting
Doc.Close

MsgBox"Opening New" 'Add for troubleshooting
Documents.Open FileName:=tempFile, ReadOnly:=False

Application.ScreenUpdating = True


Set Doc = Nothing

macdonnie
01-25-2016, 09:31 AM
Thanks SamT for the response. I'm under the impression that you are correct.

Let me be a little bit clearer: My CPU isn't jumping up to 100% when I use the following line: Doc.Close The issue arises when I physically select close out of Word(The X in the right hand corner).

I added the Msgboxes where you suggested, and then also added them to every sub in my code that I use. However, I still see the issue when I exit out of Word without seeing any message boxes. However, I'm not sure is a messagebox would appear once I exit Word and only the process exist.

Please note that 25% is one full CPU core:
See my CPU:
15279

macdonnie
01-25-2016, 09:55 AM
So I converted the Msgboxes to my own function that uses FileSystem to write to an external file. My hope was to capture any Sub/Functions that are running after close. Long story short the Log file looks like it stops after closing:

Code:

Sub WriteToFile(toWrite As String)


Const fsoForAppend = 8


Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")


'Open the text file
Dim objTextStream As Object
Set objTextStream = objFSO.OpenTextFile("C:\ddurm\Projects_Code_Related\Dev\Procedure_Tool\Log.txt", fsoForAppend)


'Display the contents of the text file
objTextStream.WriteLine toWrite


'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing


End Sub

Log:
Each Sub has a different number:
AutoExec
15
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
17
18
11
4
3
4
1
1
4
2
6
oApp_Open
7
oApp_Close
17
18
8
oApp_Open
9
oApp_Close

macdonnie
01-25-2016, 01:26 PM
Ok I think I might have figured it out. But there is no logic to why it is being fixed!

I'm really wondering what is happening!:banghead::bug::dunno:banghead:

So the following code works:


Set Doc = Nothing
Set MainDoc = Nothing

rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing


Set MainDoc = ActiveDocument


Set Doc = Documents.Open(FileName:="https://modspops.jsc.nasa.gov/MOD/COMET/COMET/Published ECLSS/2883/2883.docx", ReadOnly:=True)
Doc.SaveAs2 FileName:=tempFile
Doc.Close
Documents.Open FileName:=tempFile, ReadOnly:=False


Set Doc = Nothing
Set MainDoc = Nothing


But the following doesn't work:


'Set Doc = Nothing
'Set MainDoc = Nothing

rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing


Set MainDoc = ActiveDocument


WriteToFile "6"
Set Doc = Documents.Open(FileName:="https://modspops.jsc.nasa.gov/MOD/COMET/COMET/Published ECLSS/2883/2883.docx", ReadOnly:=True)
WriteToFile "7"
Doc.SaveAs2 FileName:=tempFile
Doc.Close
WriteToFile "8"
Documents.Open FileName:=tempFile, ReadOnly:=False
WriteToFile "9"



Set Doc = Nothing
Set MainDoc = Nothing


The only difference is the bottom version that doesn't work has the first instance of the Set = Nothing statements commented out:
'Set Doc = Nothing
'Set MainDoc = Nothing

...

So you may be thinking that Doc and MainDoc must be set to something at this point of the code. NOPE! See below:
15283

What the heck! By setting the objects Doc and MainDoc equal to nothing the code doesn't crash on close(But they already equaled nothing to begin with)! It just don't make no sense!

Any ideas for my learning?

-Don

macdonnie
01-25-2016, 03:48 PM
I figured it out! I believe the connection(conn) and recordset(rs) are tied to a particular document(Which ever you opened them in). So I have to set rs and conn = Nothing before opening up in another document:


Set Doc = Nothing
Set MainDoc = Nothing


rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing


Set MainDoc = ActiveDocument


WriteToFile "6"
Set Doc = Documents.Open(FileName:=filePath, ReadOnly:=True)
WriteToFile "7"
Doc.SaveAs2 FileName:=tempFile
Doc.Close
WriteToFile "8"
Documents.Open FileName:=tempFile, ReadOnly:=False
WriteToFile "9"
'Application.ScreenUpdating = True


Set Doc = Nothing
Set MainDoc = Nothing


If I open a new document first and then set them equal to Nothing then they still exist in the Main Document(This is a guess).

Thanks!

SamT
01-25-2016, 04:28 PM
Your guess is better than mine, but it makes sense, in a MicroSoft kinda way. :banghead: