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
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