E
Edward Backstrom
I have a custom Word document, from a template, that catches
WindowSelectionChange to modify data that is input by the user into table
cells. All works well except when more than one Word document are open.
The problem exists whether the other file is based on the template or not.
When switching between docs, the first keystroke is lost. This behavior is
also repeated when changing cells as long as two or more documents are open.
Below is the relevant code:
Option Explicit
Public WithEvents App As Word.Application
Dim thisTable As Integer
Dim thisRow As Integer
Dim thisCol As Integer
Dim lastTable As Integer
Dim lastRow As Integer
Dim lastCol As Integer
Private Sub Document_New()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
keepAlive
End Sub
Private Sub Document_Open()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
keepAlive
End Sub
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim myRange As Range
Dim mySel As Selection
Dim myText As String
If Not ActiveDocument.AttachedTemplate = ThisDocument Then Exit Sub
thisTable = Macros.getCellLoc.tableNum
thisRow = Macros.getCellLoc.rowNum
thisCol = Macros.getCellLoc.colNum
If (Not Selection.Information(wdSelectionMode) = 0) _
Or (thisTable = lastTable And thisRow = lastRow And thisCol = lastCol) _
Or (thisTable < 4) Then
Exit Sub
End If
If lastTable = 0 Then
lastTable = thisTable
lastRow = thisRow
lastCol = thisCol
Exit Sub
End If
If lastRow > 2 _
And lastCol <= ActiveDocument.Tables(lastTable).Columns.Count Then
Set myRange = ActiveDocument.Tables(lastTable).Cell(lastRow,
lastCol).Range
myRange.End = myRange.End - 1
Select Case lastTable
' Removed
End Select
End If
Select Case thisTable
' Removed
End Select
lastTable = thisTable
lastRow = thisRow
lastCol = thisCol
End Sub
Function isEditable(row As Integer) As Boolean
If row < 3 Then
Selection.Collapse
Selection.MoveDown Unit:=wdLine, Count:=3 - row
Selection.SelectRow
Selection.Collapse
isEditable = False
Else
isEditable = True
End If
End Function
Sub keepAlive()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="keepAlive",
Tolerance:=0
End Sub
WindowSelectionChange to modify data that is input by the user into table
cells. All works well except when more than one Word document are open.
The problem exists whether the other file is based on the template or not.
When switching between docs, the first keystroke is lost. This behavior is
also repeated when changing cells as long as two or more documents are open.
Below is the relevant code:
Option Explicit
Public WithEvents App As Word.Application
Dim thisTable As Integer
Dim thisRow As Integer
Dim thisCol As Integer
Dim lastTable As Integer
Dim lastRow As Integer
Dim lastCol As Integer
Private Sub Document_New()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
keepAlive
End Sub
Private Sub Document_Open()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
keepAlive
End Sub
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
Dim myRange As Range
Dim mySel As Selection
Dim myText As String
If Not ActiveDocument.AttachedTemplate = ThisDocument Then Exit Sub
thisTable = Macros.getCellLoc.tableNum
thisRow = Macros.getCellLoc.rowNum
thisCol = Macros.getCellLoc.colNum
If (Not Selection.Information(wdSelectionMode) = 0) _
Or (thisTable = lastTable And thisRow = lastRow And thisCol = lastCol) _
Or (thisTable < 4) Then
Exit Sub
End If
If lastTable = 0 Then
lastTable = thisTable
lastRow = thisRow
lastCol = thisCol
Exit Sub
End If
If lastRow > 2 _
And lastCol <= ActiveDocument.Tables(lastTable).Columns.Count Then
Set myRange = ActiveDocument.Tables(lastTable).Cell(lastRow,
lastCol).Range
myRange.End = myRange.End - 1
Select Case lastTable
' Removed
End Select
End If
Select Case thisTable
' Removed
End Select
lastTable = thisTable
lastRow = thisRow
lastCol = thisCol
End Sub
Function isEditable(row As Integer) As Boolean
If row < 3 Then
Selection.Collapse
Selection.MoveDown Unit:=wdLine, Count:=3 - row
Selection.SelectRow
Selection.Collapse
isEditable = False
Else
isEditable = True
End If
End Function
Sub keepAlive()
If App Is Nothing Then
Set App = ThisDocument.Application
End If
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="keepAlive",
Tolerance:=0
End Sub