Navigate to previous cursor locations

W

Wingman

Hi,

I want to be able to navigate (i.e set the cursor) to previous
selected positions in my word document because i use hyperlinks for
words that need explanation, and i want to be able to return so users
can keep reading without having to scroll all the way back. I have
figured out how to make a macro listen to click events, executing the
following code:

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
Dim r As Range
Set r = Sel.Range
Module1.AddRange (r)
End Sub

Module 1 contains the following:

Dim Ranges(1 To 10) As Range
Dim index As Integer

Public Function AddRange(ByVal r As Range)
index = index + 1
If index = 11 Then index = 1
Ranges(index) = r
End Function

My problem is:

I get an error message at Module1.AddRange(r) saying: compile error:
Type mismatch.

When I change the Function AddRange r parameter to Variant (and the
other stuff in module1 to variant) it all works fine, except for the
values stored in my Ranges array contain empty strings ("") and no
Range objects. What am I doing wrong here?

I include all the code I use for clarification:
create a class 'class1' in normal.dot with the following code:
Option Explicit
Public WithEvents oApp As Word.Application

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
Dim r As Range
Set r = Sel.Range
Module1.AddRange (r)
End Sub

And in module1, (also in normal.dot) put the code:
Option Explicit

Dim oAppClass As New class1

Dim Ranges(1 To 10) As Range
Dim index As Integer

'This sub get's auto-executed when Word starts.
'When editing code, make sure this sub
'is executed after editing so the events are handled
Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub

Public Function AddRange(ByVal r As Range)
index = index + 1
If index = 11 Then index = 1
Ranges(index) = r
End Function

'call this macro to make cursor select previous location
Public Sub PreviousRange()
Dim r As Range
GetPreviousIndex
r = Ranges(index)
If Not r is Nothing Then 'TODO: compilation error...how to check is r
is nothing? irritating...
r.Select
End If

Private Sub GetPreviousIndex()
index = index - 1
If index < 1 Then index = 10
End Sub
 
H

Helmut Weber

Hi Wingman,

interesting exercise.
Impossible to get it all straight by a few lines.

First, your function AddRange hasn't got a return value.
Pretty bad, or at least unusual.

Second, you are trying to assign a range
to an integer. Pretty bad, too.

I got as far as that:

Module1:

Option Explicit

Dim oAppClass As New Class1
Dim Ranges(1 To 10) As Range
Dim index As Integer

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

Sub Test222(r As Range)
MsgBox "x1 = " & r.start & Chr(13) _
& "x2 = " & r.End
End Sub

In class1:

Option Explicit
Public WithEvents oApp As Word.Application

Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
Test222 Selection.Range
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

Wingman,

Not sure I follow the Ranges (1 to 10) bit but first if the procedure
is intended to go back to any previsously selected text or the IP
when

in run then this seems to work:

In the Module:
Option Explicit
Dim oAppClass As New Class1
Dim Ranges(1 To 10) As Range
Dim index As Integer
Public Sub AutoExec()
Set oAppClass.oApp = Word.Application
End Sub
Sub AutoNew()
index = 1
Set Ranges(1) = Selection.Range
End Sub
Sub AutoOpen()
index = 1
Set Ranges(1) = Selection.Range
End Sub
Public Sub AddRange(ByVal r As Range)
index = index + 1
If index = 11 Then index = 1
Set Ranges(index) = r
End Sub
Public Sub PreviousRange()
Dim r As Range
GetPreviousIndex
Set r = Ranges(index)
If Not r Is Nothing Then
r.Select
End If
End Sub
Private Sub GetPreviousIndex()
index = index - 1
If index < 1 Then index = 10
End Sub

in the Class:

Option Explicit
Public WithEvents oApp As Word.Application
Private Sub oApp_WindowSelectionChange(ByVal Sel As Selection)
Dim r As Range
Set r = Selection.Range
Module1.AddRange r
End Sub

I added the AutoOpen and AutoNew so the would be a defined range to
return to on first use.
 

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