MindReaderOpenDirect.bas

From ActivityOwnerWiki
Jump to: navigation, search
Sub mindreaderopendirect(ByRef MM As MindManager.Application, ByRef doc As MindManager.Document, ByRef linktopic As MindManager.Topic, astr As String)
Dim lStr As String      'lower case of text
Dim maintopic As Topic  'one of main topics in mindreader.mmap
'linktopic = topic identified as maintopic of "links" branch
Dim defaultlink As Hyperlink
Dim t As Topic
Dim tt As Topic
Dim i As Integer        'counter used in linktopic search
Dim found As Boolean    'true if in-tray is found
Dim itopic As Topic     'in-tray topic
Dim ntopic As Topic     'topic added to in-tray
Dim Destdoc As MindManager.Document 'Destination MindManager.Document
Dim mtopic As Topic     'one of main topics in destination map
Dim DocCurrent As MindManager.Document 'One of currently open MindManager.Documents: used to search for deleteme map
Dim link As Hyperlink
Dim ismap As Boolean   'trap situation where non map keyword is used with queue
Dim d As Integer 'index of defaultmap branchb
Dim mfound As Boolean 'true if a link keyword match found
'
lStr = LCase(astr)
i = linktopic.AllSubTopics.Count
Set defaultlink = linktopic.AllSubTopics(1).Hyperlink 'default location of default map
mfound = False
While i > 0 And Not mfound
     Set t = linktopic.AllSubTopics(i)
     If InStr(lStr, LCase(t.Text)) > 0 Then
        If t.HasHyperlink Then
            mfound = True
            Set link = t.Hyperlink
        End If
     End If
    If Not mfound Then
        If t.AllSubTopics.Count > 0 Then
             For Each tt In linktopic.AllSubTopics(i).AllSubTopics
                 If InStr(lStr, LCase(tt.Text)) > 0 Then
                    If t.HasHyperlink Then
                        Set link = tt.Hyperlink
                        mfound = True
                    End If
                 End If
                 If InStr("defaultmap", LCase(tt.Text)) > 0 Then Set defaultlink = tt.Hyperlink
            Next
        End If
    End If
    If InStr("defaultmap", LCase(linktopic.AllSubTopics(i).Text)) > 0 Then Set defaultlink = linktopic.AllSubTopics(i).Hyperlink
    i = i - 1
Wend
If Not mfound Then Set link = defaultlink
'**************************
'if mindreader.mmap has a topic link, put it there instead of in in-tray
'**************************
'found=Len(linktopic.AllSubTopics(i).Hyperlink.TopicBookmarkGuid)>0
found = InStr(link.Address, "mj-map") = 1

'**************************
'Catch error if hyperlink doesn't work
'**************************

On Error Resume Next 'disable error checking when following mindmanager hyperlink
If found Then
    link.Follow
    Set Destdoc = MM.ActiveDocument
End If
'
On Error GoTo E2
If Not found Then
    If InStr(link.Address, "mmap") > 0 Then
        If link.Absolute Or InStr(link.Address, ":\") Or InStr(link.Address, "\\") Then
            Set Destdoc = MM.Documents.Open(link.Address)
        Else
            Set Destdoc = MM.Documents.Open(MM.GetPath(mmDirectoryMyMaps) & link.Address)
        End If
    Else
        ismap = False
        link.Follow
        Set Destdoc = MM.Documents.Add
    End If
End If

'**************************
'Error Handling
'If can't open mindreader.mmap or follow hyperlink, create a temp
'map to "catch" the incoming task from gyroQ
'**************************
If Err.Number > 0 Then
E1:    MsgBox "mindreader.mmap Open Error:" & Err.Description: GoTo X
E2:    MsgBox "Error trying to follow hyperlink in mindreader.mmap:" & Err.Description: GoTo X
   'E3: MsgBox "Error trying to save or close mindreader.mmap:"&Err.Description : GoTo X
X:  Err.Clear
  Set Destdoc = MM.Documents.Add
End If
On Error GoTo 0
 '*******************************
 'If topic hyperlink was found set that to destination topic
 '*******************************
 If found Then
    Set itopic = Destdoc.Selection.PrimaryTopic
 Else
  '************************
  'if not a topic hyperlink, search for in-tray
  '*************************
  For Each mtopic In Destdoc.CentralTopic.AllSubTopics
   If InStr(LCase(mtopic.Task.Categories), "in-tray*") > 0 Or mtopic.TextLabels.ContainsTextLabel("In-tray*") Then
    Set itopic = mtopic
    found = True
    Exit For
   End If
  Next
 End If
 '****************************
 'If in-tray not found, create one
 '****************************
 If Not found Then
   Set itopic = Destdoc.CentralTopic.AddSubTopic("In-Tray")
   itopic.Task.Categories = "In-tray*,process"
   itopic.Task.Complete = 0
 End If
 '************************
 'If "q" in use select intray for subsequent macro to add items to
 '*************************
            Set ntopic = itopic.AddSubTopic(astr)
            'Destdoc.Selection.Set (ntopic)
            Destdoc.Selection.Set ntopic
On Error GoTo 0
Destdoc.Activate
Set doc = Nothing
Set maintopic = Nothing
Set defaultlink = Nothing
Set t = Nothing
Set tt = Nothing
Set itopic = Nothing
Set ntopic = Nothing
Set Destdoc = Nothing
Set mtopic = Nothing
Set DocCurrent = Nothing
Set link = Nothing
End Sub