Get the selection in an active note by VBA

  • 1
  • Question
  • Updated 6 years ago
  • Answered
How to find out with a basic script, if in an active note some text is selected?
Photo of Chris Drontheim

Chris Drontheim

  • 5 Posts
  • 0 Reply Likes

Posted 6 years ago

  • 1
Photo of Nick Duffill

Nick Duffill, Champion

  • 512 Posts
  • 170 Reply Likes
Hello Chris

There are no methods or properties for reading the user selection in the topic notes. The methods and properties that refer to the selection and the cursor in the topic notes are only for subsequent editing commands through the API, and are not the same as the user selection. The cursor or highlighted selection are not rendered.

What is it that you need to be able to do? Maybe there is another way to solve it.
Photo of Chris Drontheim

Chris Drontheim

  • 5 Posts
  • 0 Reply Likes
Hello Nick,

thank you for reply. I want to build something like this:

1. select a string (~word) in a note
2. Right-Click the macro in the context-menu: "Paste Topic-Name from Selection"
-> Rename the Topic with the selected word

I would be glad, if you could help. Thank you very much.

Viele Grüße

Chris
Photo of Nick Duffill

Nick Duffill, Champion

  • 512 Posts
  • 170 Reply Likes
Chris

As there is no way to read the selected text through the API, you could do it in two steps instead of one:

- Right-click and Copy, then
- Right-click and run the macro "Set topic name from Clipboard".

The macro would need to find the currently selected topic, then replace the topic text with the text from the clipboard. You could add a safety check to ensure that the clipboard is not empty or more than (say) 120 characters. You could also empty the clipboard after pasting it to the topic text. This is all possible with the API and a macro. The macro can be attached to the Notes Context Menu through the dialogue at View > Macros > Organize Macros.
Photo of Chris Drontheim

Chris Drontheim

  • 5 Posts
  • 0 Reply Likes
Hi Nick,

thank you very much. I guess, I need the handle of the notes-window? How can I get it?

Thank you.

Viele Grüße
Photo of Nick Duffill

Nick Duffill, Champion

  • 512 Posts
  • 170 Reply Likes
Hello Chris

You should not need the handle to the Notes window to do this. (I don't think it is available anyway). If the command that launches the macro is in the Notes Context menu, then you will know that the user is editing Topic Notes, as this menu is not shown anywhere else. You can get the currently edited topic from

ActiveDocument.Selection.PrimaryTopic

if the user has already used the Copy command then the required text will be on the Clipboard.
Photo of Chris Drontheim

Chris Drontheim

  • 5 Posts
  • 0 Reply Likes
Hi Nick,

thanks for guiding me. Below is what I have. It works. But the extra-step (copy first) is really annoying me. Is there no workaround? Could I get the cursor-position in a note somehow? If I try to achieve this (n.cursorposition), I only get "1". Thanks. Best

Private DataObj As MSForms.DataObject 'Reference: fm20.dll

Sub Main

Dim t As Topic
Dim strClip As String

Set t=ActiveDocument.Selection.PrimaryTopic
Set DataObj=New MSForms.DataObject

DataObj.GetFromClipboard
strClip = DataObj.GetText

t.Text=strClip

End Sub
Photo of Nick Duffill

Nick Duffill, Champion

  • 512 Posts
  • 170 Reply Likes
Chris

Unfortunately, the cursor position property is a private cursor belonging to the API, and is not the user's cursor. Reading it will only read back what you have written, not what the user has selected.

It might be possible to automate the Copy stage by sending Ctrl+C to MindManager from the script, using the SendKeys command. However you might encounter some Windows security issues with this. It also assumes that something is already selected.
Photo of Chris Drontheim

Chris Drontheim

  • 5 Posts
  • 0 Reply Likes
Nick,

below the current version, I hope, keybd_event prevents me from any security issues.
One for you: You can get the handle of an active notes window by a its classname: "Ter32ClassMM".

One for me: what is the name of that right-click-menu with the dictionary-entries, that wants me to correct my text in the notes window? How can I put my Macro-Entry in there?

Thanks, best

'####

Option Explicit

Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function MapVirtualKey Lib "user32.dll" Alias _
"MapVirtualKeyA" ( _
ByVal wCode As Long, _
ByVal wMapType As Long) As Long

Private Const VK_CONTROL As Long = &H11
Private Const KEYEVENTF_KEYUP As Long = &H2
Private DataObj As MSForms.DataObject 'Reference: fm20.dll

Sub Main()

Dim t As Topic
Dim strClip As String

Set t = ActiveDocument.Selection.PrimaryTopic
Set DataObj = New MSForms.DataObject

'// Copy selected text

'strg +C drücken

keybd_event VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), 0, 0
keybd_event Asc("C"), MapVirtualKey(Asc("C"), 0), 0, 0
keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
DoEvents

DataObj.GetFromClipboard
strClip = DataObj.GetText
Set DataObj = Nothing

t.Text = strClip

End Sub
Photo of Nick Duffill

Nick Duffill, Champion

  • 512 Posts
  • 170 Reply Likes
Hello Chris

I can only see an enumeration for the Notes Context menu (20), so it looks like the text context menu is not exposed in the API as a separate menu. Maybe they are the same menu, but get different commands depending on where they are clicked, and custom commands are only shown in the Notes context menu.

But at least right-clicking in the white space on the text for the Notes context menu does not seem to cancel the selection.

This conversation is no longer open for comments or replies.