Results 1 to 6 of 6

Thread: [vba] how to call event: show tooltip of footnote?

  1. #1

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine on fire (country of slaves)
    Posts
    750

    [vba] how to call event: show tooltip of footnote?

    Hi,

    I need to show tooltip of note programmatically.
    (it'll be excellent, if it's been possible without delay). As you know, to show tooltip of note, you should move mouse pointer to note reference and wait ~ 1 sec.

    I tried to imitate mouse move using SetCursorPos, but it work on MS Word 2003 only. On 2008 and newer it work not always. Also there is a delay.
    Maybe, there is a way to call FootNote_MouseMouse Event directly?

    Here is my try (code for ThisDocument module):
    Code:
    
    Option Explicit
    
    Private WithEvents appWord As Word.Application
    
    Private Type RANGE_POSITION
        Left    As Long
        Top     As Long
        Width   As Long
        Height  As Long
    End Type
    
    Private Enum DIRECTION
        DIR_FORWARD
        DIR_BACKWARD
    End Enum
    
    Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    
    Dim oKey1 As KeyBinding
    Dim oKey2 As KeyBinding
    
    Private Sub appWord_WindowActivate(ByVal Doc As Word.Document, ByVal Wn As Word.Window)
        Call BindKey
    End Sub
    
    Private Sub Document_Close()
        On Error Resume Next
        oKey1.Clear
        oKey2.Clear
    End Sub
    
    Private Sub Document_Open()
        Set appWord = Word.Application
        Call BindKey
    End Sub
    
    Private Sub BindKey()
        With Application
            'Goto Note by hotkey Alt + Ctrl + Num4 (or Num6) 
            .CustomizationContext = ThisDocument
            Set oKey2 = .KeyBindings.Add( _
                KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyNumeric4), _
                KeyCategory:=wdKeyCategoryCommand, _
                Command:="GotoNoteBack")
            Set oKey2 = .KeyBindings.Add( _
                KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyNumeric6), _
                KeyCategory:=wdKeyCategoryCommand, _
                Command:="GotoNoteNext")
        End With
    End Sub
    
    Public Sub GotoNoteNext()
        FindNote DIR_FORWARD
    End Sub
    Public Sub GotoNoteBack()
        FindNote DIR_BACKWARD
    End Sub
    
    Private Sub FindNote(dir As DIRECTION)
        Dim i       As Long
    
        If ActiveDocument.Footnotes.Count = 0 And ActiveDocument.Endnotes.Count = 0 Then
            MsgBox "Document has no notes.", vbInformation
            Exit Sub
        End If
    
        If ActiveDocument.Footnotes.Count > 0 Then
            If dir = DIR_FORWARD Then
                For i = 1 To ActiveDocument.Footnotes.Count
                    If CheckNote(ActiveDocument.Footnotes(i).Reference, dir) Then Exit Sub
                Next
            Else
                For i = ActiveDocument.Footnotes.Count To 1 Step -1
                    If CheckNote(ActiveDocument.Footnotes(i).Reference, dir) Then Exit Sub
                Next
            End If
            If MsgBox("Search has been completed. Start again?", vbYesNo) = vbYes Then
                Selection.Start = IIf(dir = DIR_FORWARD, 0, ActiveDocument.Content.End)
                FindNote dir
            End If
        End If
    End Sub
    
    Private Function CheckNote(RA As Range, dir As DIRECTION) As Boolean
        Dim RP      As RANGE_POSITION
        Dim cX As Long, cY As Long
        If IIf(dir = DIR_FORWARD, RA.Start > Selection.Start, RA.Start < Selection.Start) Then
            Selection.Start = RA.Start
            Selection.End = RA.Start
            Selection.Range.Select
            Call ActiveWindow.GetPoint(RP.Left, RP.Top, RP.Width, RP.Height, RA)
            cX = RP.Left + RP.Width \ 2
            cY = RP.Top + RP.Height \ 2
            SetCursorPos cX, cY
            CheckNote = True
        End If
    End Function
    
    Searching of notes is on hotkeys: Ctrl + Alt + Num4 (Num6).
    You should create several notes in document first.

    Thank you in advance,
    Alex.
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [vba] how to call event: show tooltip of footnote?

    do you need to display the actual tip as displayed by word? or could you display the footnote in an alternative container?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine on fire (country of slaves)
    Posts
    750

    Re: [vba] how to call event: show tooltip of footnote?

    I need an actual tip.

    But, if it's impossible, I also considered to make a container, like shape that visually looks like the same as original tip.
    It's not hard to make it, but in this case I'll need also an event to destroy shape when user move cursor out of note reference (or after several seconds). So, to emulate the same behavior as original tip.
    Actually, I already created a P-O-C (template) of such shape:
    Code:
    
    Public Sub Sample()
        Dim objShape As Shape
        Dim posX As Long, posY As Long
    
        posX = Selection.Information(wdHorizontalPositionRelativeToPage)
        posY = Selection.Information(wdVerticalPositionRelativeToPage)
    
        Set objShape = ActiveDocument.Shapes.AddTextbox _
            (Orientation:=msoTextOrientationHorizontal, _
            Left:=posX, Top:=posY, Width:=118, Height:=20)
    
        With objShape
            .Fill.ForeColor = RGB(255, 255, 225)
            .Line.ForeColor = RGB(227, 227, 227)
            .TextFrame.TextRange.Font.Color = wdColorBlack
            .TextFrame.TextRange.Font.Name = "Arial"
            .TextFrame.TextRange.Font.Size = 6
            .TextFrame.TextRange.Text = "HelloWorld" & vbCrLf & "HelloWorld"
            .TextFrame.MarginLeft = 1
            .TextFrame.MarginRight = 1
            .TextFrame.MarginTop = 1
            .TextFrame.MarginBottom = 1
            .TextFrame.AutoSize = True
        End With
    End Sub
    
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [vba] how to call event: show tooltip of footnote?

    I'll need also an event to destroy
    you could use the WindowSelectionChange event and /or a timer
    not quite the same as the default method, but probably would not be noticable to most users
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine on fire (country of slaves)
    Posts
    750

    Re: [vba] how to call event: show tooltip of footnote?

    Thank you, westconn1.
    Another problem with shape is that the shape cannot be displayed out of the client area of window unlike original tip, when note reference located at the end of the line.
    So, I think I have 2 options:
    1. check position of note reference. If (current pos.) + (width of shape needed) > (client area width), move a shape to the left, corresponding to the difference.
    2. Use CreateWindow API instead of shape.
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  6. #6
    gibra
    Guest

    Re: [vba] how to call event: show tooltip of footnote?

    Search on Planet Source Code

    VB ExTooltip by MArio Flores G (from psc cd)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width