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