PDA

View Full Version : Paste to navigation search box from userform



JPG
10-30-2020, 07:40 AM
I am using a form in a VBA macro in Word.

In Word 2010 there is a navigation pane.
At the top you can type something to search.

I want to paste data I have in the clipboard to that navigation pane search box.
I can't seem to activate that navigation search box with what I have tried.

So my form lists out the result of a regex search into a textbox. I triple click to select the line I want to now locate in the Word document and press a button on my form.
The msgbox shows with the correct data, so that is good so far.
It is just the next step that I need. (I have commented out the SendKeys part). How to select that search box and then I can use the SendKeys part of the code.


Private Sub CommandButton48CopyTextInTextbox_Click()
TextBox8refList.SetFocus
TextBox8refList.Copy

Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard

strPaste = DataObj.GetText(1)
ActiveWindow.DocumentMap = True
ActiveWindow.SetFocus


ActiveDocument.Activate
'SendKeys "^(f){backspace}^{v}{Enter}"
MsgBox (strPaste)
End Sub

thanks in advance for any assistance.
Jon

JPG
11-11-2020, 06:13 AM
Private Sub CommandButton48CopyTextInTextbox_Click()

If selection.Type = wdSelectionIP Then InSelection = True


selection.HomeKey wdStory
TextBox8refList.SetFocus


SendKeys "{Home}"
SendKeys "+{End}"
DoEvents 'This was the key to this working. Macro was going ahead of itself
TextBox8refList.SetFocus


TextBox8refList.Copy


Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard


strPaste = DataObj.GetText(1)


Dim dlg As Dialog
Set dlg = Application.Dialogs(wdDialogEditFind)
dlg.Find = strPaste
SendKeys "%(f){tab}{ENTER}"
dlg.Show

''''''This section added, and it makes it use the navigation window
If CheckBox17NavigationPane.Value = True Then
SendKeys "^f"
SendKeys "^v"
SendKeys "{ENTER}"
End If


''''''''''''''''''''''''''''''''''''''
End Sub

Well after much trial and error I found this works and with the checkbox option I can have it use either the regular search box or the navigation pane.

JPG
11-17-2020, 04:30 PM
I was having some issues and could not figure it. This seems to have fixed things. If there are any improvements please say. This is all a bit new territory for me.



Private Sub CommandButton48CopyTextInTextbox_Click()On Error Resume Next
If selection.Type = wdSelectionIP Then InSelection = True
selection.Find.MatchWildcards = False 'don't forget to set this


selection.HomeKey wdStory
TextBox8refList.SetFocus


SendKeys "{Home}"
SendKeys "+{End}"
DoEvents 'This was the key to this working. Macro was going ahead of itself
TextBox8refList.SetFocus
CloseClipboard 'hmm looks like this was another key part
DoEvents
TextBox8refList.Copy


Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
'DoEvents
strPaste = DataObj.GetText(1)
'DoEvents


Dim dlg As Dialog
Set dlg = Application.Dialogs(wdDialogEditFind)
dlg.Find = strPaste
SendKeys "%(f){tab}{ENTER}"
'dlg.Show
''''''This section added, and it makes it use the navigation window
'On Error Resume Next
'ActiveWindow.SetFocus
'DoEvents
If CheckBox17NavigationPane.Value = True Then
SendKeys "^f"
SendKeys "^v"
SendKeys "{ENTER}"
End If


''''''''''''''''''''''''''''''''''''''






End Sub

JPG
11-18-2020, 03:11 AM
This is odd. The above does not work now since I restarted Word.....
Time for a rethink I suppose. I guess I have missed resetting something... but what?

JPG
11-18-2020, 03:20 AM
Rolled back to this and it works.


Private Sub CommandButton48CopyTextInTextbox_Click()


If selection.Type = wdSelectionIP Then InSelection = True


selection.HomeKey wdStory
TextBox8refList.SetFocus


SendKeys "{Home}"
SendKeys "+{End}"
DoEvents 'This was the key to this working. Macro was going ahead of itself
TextBox8refList.SetFocus


TextBox8refList.Copy


Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard


strPaste = DataObj.GetText(1)


Dim dlg As Dialog
Set dlg = Application.Dialogs(wdDialogEditFind)
dlg.Find = strPaste
SendKeys "%(f){tab}{ENTER}"
dlg.Show
''''''This section added, and it makes it use the navigation window
'On Error Resume Next
'ActiveWindow.SetFocus
'DoEvents
If CheckBox17NavigationPane.Value = True Then
SendKeys "^f"
SendKeys "^v"
SendKeys "{ENTER}"
End If


''''''''''''''''''''''''''''''''''''''






End Sub