gmaxey
02-09-2013, 08:48 AM
I have a template dotm file (uploaded a a docm that hopefully you can rename with a .dotm extension) that illustrates a problem that I've encountered with the StatusBar in Word 2010. The problem does not occur with the same code used in a .dot file with Word 2003.
The template is used as a Global Add-In and it contains data required for the add-in to work. So when the add-in is loaded it is opened as a document so Word can extract the data then closed.
All that works well. However if the add-in is loaded on startup or loaded manually after first starting Word then after the add-in loads, the StatusBar is displaying the add-in file name. This display appears to be "stuck" as the status bar does not display document statistics (e.g., Page No., Line No. Words, etc). as the user types.
Oddly enough, I can get it out of this stuck situation by typing in a URL e.g, www.google.com (http://www.google.com) and then mousing over the URL. After this all seems well.
I've managed a fix shown in the comments lines below. Still I would like to understand why this occurs and if there is generally a better way to fix or avoid the situation altogether. Thanks.
Sub AutoExec()
LoadAddInData
lbl_Exit:
Exit Sub
End Sub
Sub LoadAddInData()
Dim oTmp As Template, oDoc As Word.Document, oTbl As Word.Table
Dim lngIndex As Long, arrData() As String
Dim bClose As Boolean
bClose = True
Set oTmp = ThisDocument.AttachedTemplate
'This keeps the Add-In from crashing Word if loaded on start-up. Better way?
If Documents.Count = 0 Then Documents.Add
'Get access to template table.
If Not ActiveDocument.Name = oTmp.Name Then
Application.ScreenUpdating = False
Set oDoc = oTmp.OpenAsDocument
Else
Set oDoc = ActiveDocument
bClose = False
End If
Set oTbl = oDoc.Tables(1)
'Load currency data from template table.
ReDim arrData(oTbl.Rows.Count - 2, 2)
For lngIndex = 2 To oTbl.Rows.Count
arrData(lngIndex - 2, 0) = GetCellText(oTbl.Rows(lngIndex).Cells(1))
arrData(lngIndex - 2, 1) = GetCellText(oTbl.Rows(lngIndex).Cells(2))
arrData(lngIndex - 2, 2) = GetCellText(oTbl.Rows(lngIndex).Cells(3))
Next lngIndex
If bClose Then oDoc.Close wdDoNotSaveChanges
'Uncomment this code to fix.
'If Documents.Count = 1 Then
'Documents.Add
'Documents(1).Close wdDoNotSaveChanges
'End If
Set oTbl = Nothing
Set oDoc = Nothing
Set oTmp = Nothing
Application.ScreenUpdating = True
Exit Sub
End Sub
Function GetCellText(ByRef oCell As Word.Cell) As String
GetCellText = Left(oCell.Range.Text, Len(oCell.Range.Text) - 2)
lbl_Exit:
Exit Function
End Function
The template is used as a Global Add-In and it contains data required for the add-in to work. So when the add-in is loaded it is opened as a document so Word can extract the data then closed.
All that works well. However if the add-in is loaded on startup or loaded manually after first starting Word then after the add-in loads, the StatusBar is displaying the add-in file name. This display appears to be "stuck" as the status bar does not display document statistics (e.g., Page No., Line No. Words, etc). as the user types.
Oddly enough, I can get it out of this stuck situation by typing in a URL e.g, www.google.com (http://www.google.com) and then mousing over the URL. After this all seems well.
I've managed a fix shown in the comments lines below. Still I would like to understand why this occurs and if there is generally a better way to fix or avoid the situation altogether. Thanks.
Sub AutoExec()
LoadAddInData
lbl_Exit:
Exit Sub
End Sub
Sub LoadAddInData()
Dim oTmp As Template, oDoc As Word.Document, oTbl As Word.Table
Dim lngIndex As Long, arrData() As String
Dim bClose As Boolean
bClose = True
Set oTmp = ThisDocument.AttachedTemplate
'This keeps the Add-In from crashing Word if loaded on start-up. Better way?
If Documents.Count = 0 Then Documents.Add
'Get access to template table.
If Not ActiveDocument.Name = oTmp.Name Then
Application.ScreenUpdating = False
Set oDoc = oTmp.OpenAsDocument
Else
Set oDoc = ActiveDocument
bClose = False
End If
Set oTbl = oDoc.Tables(1)
'Load currency data from template table.
ReDim arrData(oTbl.Rows.Count - 2, 2)
For lngIndex = 2 To oTbl.Rows.Count
arrData(lngIndex - 2, 0) = GetCellText(oTbl.Rows(lngIndex).Cells(1))
arrData(lngIndex - 2, 1) = GetCellText(oTbl.Rows(lngIndex).Cells(2))
arrData(lngIndex - 2, 2) = GetCellText(oTbl.Rows(lngIndex).Cells(3))
Next lngIndex
If bClose Then oDoc.Close wdDoNotSaveChanges
'Uncomment this code to fix.
'If Documents.Count = 1 Then
'Documents.Add
'Documents(1).Close wdDoNotSaveChanges
'End If
Set oTbl = Nothing
Set oDoc = Nothing
Set oTmp = Nothing
Application.ScreenUpdating = True
Exit Sub
End Sub
Function GetCellText(ByRef oCell As Word.Cell) As String
GetCellText = Left(oCell.Range.Text, Len(oCell.Range.Text) - 2)
lbl_Exit:
Exit Function
End Function