Missing keystrokes on custom document

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
 
C

Cindy M -WordMVP-

Hi Edward,

Before we try to analyze this, could you please put
Option Explicit

at the beginning of ALL your modules (standard, class, form - all of them).
Then Debug/Compile. Fix any and all things the compiler hits on (such as
missing variable declarations for thisTable, thisRow, etc.) Then, if that
doesn't track down the problem for you, post again with the corrected code.
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

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
E

Edward Backstrom

Cindy,

Thank you for looking into this. I am replying from another email account.

The code has Option Explicit defined. I forgot to add the code from the
"Macros" module. I removed parts of the code in the examples that are not
relevant. The application compiles fine and runs fine - except when more
than one document is open. We are using Office XP and 2003, I don't have
the various version numbers here. I know at least one of the testing
machines is SP2 but it still exhibits the same behavior. Below is code from
the Macros module:

Option Explicit

Dim thisTable As Integer
Dim thisRow As Integer
Dim thisCol As Integer

Type cellLocation
tableNum As Integer
colNum As Integer
rowNum As Integer
End Type

Function getCellLoc() As cellLocation

Dim lngRow As Long, lngCol As Long

getCellLoc.tableNum = -1

If Selection.Information(wdWithInTable) Then
lngCol = Selection.Information(wdStartOfRangeColumnNumber)
lngRow = Selection.Information(wdStartOfRangeRowNumber)

getCellLoc.tableNum = ActiveDocument.Range(0,
Selection.Tables(1).Range.End).Tables.Count
getCellLoc.colNum = CInt(lngCol)
getCellLoc.rowNum = CInt(lngRow)
End If

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top