Creating a search function w/in word and hyperlinked documents

G

Guest

Hi,

This is my first posting- hope it is in the right area.

What I'm looking for is a search bar/function that I can place at the top of
the word document. I know there is CTRL+F but I'd rather it be stationary at
the top of the document for ease of use. I'd also like to know if it would be
possible for this search function to search docs and pdfs that are
hyperlinked the the word document.

Thanks for your help.
 
G

Guest

Hi espuma58,

No answers yet? You're not being ignored; no answers usually means that
no-one has what they regard as a satisfactory answer. I don't think I have
one either but ...

I don't know of anything that will do all that you want. If you just want
something that will stay somewhere on your screen and let you do basic
searches of a document, I can provide some macro code. I pretty much dislike
the size and jumpiness of Word's built-in Find dialog so recently I created a
VBA UserForm that I use instead. It's small and can be semi-transparent and
doesn't jump around. In it's current form it only lets you do basic searches.

If that sounds like it might be of any use, post back and I'll post the
code. If you do, please give us an indication of how familiar you are with
VBA and also what version of Word you're using.

Regards.

Ed
 
G

Guest

Ed,
While waiting for espuma58 to respond, I am also looking for the exact same
thing for word. I know little vba and i'm using word 2003.
Thanks for your help
 
G

Guest

Hi Bcfd-IT3 (& espuma58),

Here is the code for the mini find dialog. The dialog is very basic; its
purpose is just to allow you to perform basic searches in an open document,
keeping the dialog onscreen but unobtrusive. (It doesn't allow you to search
linked docs or anything.)

The EditFind macro intercepts Word’s Find commands and runs the mini find
dialog instead of Word’s Find dialog. (So you can use Edit>Find, Ctrl+F etc.)

It has four levels of transparency which you can cycle through by
double-clicking an empty area of the form. (One issue that I have on my PC
which may be to do with my particular setup is that if I invoke the mini find
dialog using a toolbar button the form remains opaque.)

You can drag the dialog to your preferred location on the screen and click
the Save button and next time it loads it should load to the saved location
and with the saved transparency level.

The textbox on the form should keep the focus after each find so you can
perform several searches without having to keep clicking the textbox.

You can if you want click in the document and work with the document while
keeping the dialog visible.

The Find button has its Default property set to true so you can activate it
by pressing Enter.

The Quit button has its Cancel property set to true so you can close the
form (when it has the focus) by pressing Esc.

The BuiltIn button invokes Word’s normal Find dialog.

The instructions that follow are for versions of Word from 97 to 2003.

Create a template to hold the VBA stuff as follows:

- Determine the location of Word's startup folder. Go to Tools>Options>File
Locations>Startup and click Modify. Sometimes the path is displayed in the
"Folder name" box at the bottom of the dialog, and sometimes you need to
click the "Look in" drop-down at the top of the dialog.

- Create a blank template and save it into the startup folder from the
previous step. For the purposes of these instructions, I’m using a template
name of “MyMacros.dotâ€.

Create the UserForm as follows:

- With MyMacros.dot open, open the Visual Basic Editor (VBE). To do this,
use Alt+F11 or go to Tools>Macro>Visual Basic Editor.

- In the VBE, make sure you can see the Project Explorer window. (If
necessary, click View>Project Explorer.)

- In the Project Explorer, select the MyMacros project.

- With MyMacros selected, click Insert>UserForm. A new userform appears
(possibly named “UserForm1â€).

- Ensure that you can see the Properties window. (If necessary, click
View>Properties Window.)

- Click the form to select it and in the Properties window, set the
following properties:
Name: frmFind
Caption: Mini-Find
Height: 78.75
Width: 177

Add the controls as follows:

- Click the form to select it and ensure that you can see the Control
Toolbox. (If necessary, click View>Toolbox.)

- From the Toolbox, drag a textbox onto the form and set these properties:
Name: txtFindText
Left: 6
Top: 6
Height: 18
Width: 162

- From the Toolbox, drag a CommandButton onto the form and set these
properties:
Name: cmdFind
Accelerator: F
Default: True
Left: 6
Top: 30
Height:18
Width: 36

- From the Toolbox, drag a CommandButton onto the form and set these
properties:
Name: cmdBuiltIn
Accelerator: B
Left: 48
Top: 30
Height:18
Width: 36

- From the Toolbox, drag a CommandButton onto the form and set these
properties:
Name: cmdSave
Accelerator: S
Left: 90
Top: 30
Height:18
Width: 36

- From the Toolbox, drag a CommandButton onto the form and set these
properties:
Name: cmdQuit
Accelerator: Q
Cancel: True
Left: 132
Top: 30
Height:18
Width: 36

Add the form code as follows:

- Click the form to select it and then click View>Code.

- In the code window that opens delete anything that’s there, then paste the
form code into the window.

- Add a module for the EditFind macro.

- Click Insert>Module.

- In the Project Explorer, select the module that’s just been created.

- Ensure that you can see the Properties window (if necessary, click
View>Properties Window).

- In the Properties window, change the module’s name to something meaningful
such as “modMainâ€.

- Click in the module’s code window and paste the code for the module.

- In the VBE, click the Save icon on the toolbar to save your work.

Close the template and re-start Word..

Test (e.g. by Ctrl+F).

------------------------------------------------------------------
This is the code to be pasted into the form’s code window:

Option Explicit

Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
Private Const GWL_EXSTYLE = (-20)
Private Const WS_EX_LAYERED = &H80000

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As
Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hWnd As Long, ByVal crKey As Long, _
ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String)
As Long
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long

Private Sub cmdBuiltIn_Click()
Dialogs(wdDialogEditFind).Show
End Sub

Private Sub cmdFind_Click()
Selection.Find.ClearFormatting

With Selection.Find
.Text = txtFindText.Text
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Selection.Find.Execute

txtFindText.SetFocus
txtFindText.SelStart = 0
txtFindText.SelLength = Len(txtFindText.Text)
End Sub

Private Sub cmdQuit_Click()
Unload Me
End Sub

Private Sub cmdSave_Click()
SaveSetting AppTitle, "Config", "Top", Me.Top
SaveSetting AppTitle, "Config", "Left", Me.Left
SaveSetting AppTitle, "Config", "Opacity", FormOpacity
End Sub

Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Select Case FormOpacity
Case 64
FormOpacity = 128
Case 128
FormOpacity = 182
Case 182
FormOpacity = 255
Case 255
FormOpacity = 64
End Select

SetOpacity
End Sub

Private Sub UserForm_Initialize()
SetOpacity
End Sub

Private Sub SetOpacity()
Dim hWnd As Long
Dim ret As Long

hWnd = FindWindow(vbNullString, Me.Caption)
ret = GetWindowLong(hWnd, GWL_EXSTYLE)
ret = ret Or WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, ret
ret = SetLayeredWindowAttributes(hWnd, 0, FormOpacity, LWA_ALPHA)

End Sub
------------------------------------------------------------------

------------------------------------------------------------------
This is the code to be pasted into the code module:

Public Const AppTitle As String = "Mini-Find"
Public FormOpacity As Variant

Private hWnd As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub EditFind()
Const DefaultFormTop As Integer = 20
Const DefaultFormLeft As Integer = 600
Const DefaultFormOpacity As Integer = 128
Dim MyForm As frmFind
Dim FormTop As Variant
Dim FormLeft As Variant

hWnd = FindWindow("ThunderDFrame", AppTitle)

If hWnd = 0 Then
FormOpacity = GetSetting(AppTitle, "Config", "Opacity")
If FormOpacity = "" Then FormOpacity = DefaultFormOpacity

Set MyForm = New frmFind

FormTop = GetSetting(AppTitle, "Config", "Top")
FormLeft = GetSetting(AppTitle, "Config", "Left")

If FormTop = "" Then FormTop = DefaultFormTop
If FormLeft = "" Then FormLeft = DefaultFormLeft

MyForm.Top = FormTop
MyForm.Left = FormLeft
MyForm.Caption = AppTitle

MyForm.Show vbModeless

MyForm.txtFindText.SetFocus
Set MyForm = Nothing
End If
End Sub
 
G

Guest

Hi Ed,
OK, I tried your macro code and I think I got it right. the things I don't
get are: I don't get the "four levels of transparency" that I can cycle
through by double clicking an empty area of the form. I've tried that and
don't get any response. Am I doing something wrong? I'm double clicking
inside the search box, correct?
Next, I don't get the dragging and dropping the search dialog box to my
preferred location. then it will save there next time. it just pops up when
i press ctrl+F each time.
Is it possible to put it on the toolbar? and if so, how do I do that?
when I envoke the ctrl+F or edit\search, i enter the word or words I'm
looking for in the document and press enter to start the search just as you
would in word's search tool (although there are other options i can choose),
but i must keep pressing enter for each successive search. Is that correct?
Just want to make sure i did everything right.
I really would like to put it out on a toolbar so that i can copy this
macro/template to other users computers so they can see that when they start
word up.
Thanks for your help
bcfd-IT3
 
G

Guest

Hi Bcfd-IT3,
I don't get the "four levels of transparency" that I can cycle through by double >clicking an empty area of the form.

- If it's working as it's supposed to (possibly a big if), you should be
able to alter the transparency by double-clicking a blank area of the form
itself, not the textbox or any of the buttons or the title bar, but, for
example, the bit of the form below the buttons.
I don't get the dragging and dropping the search dialog box to my
preferred location.

- What should happen when you drag the form somewhere and click the Save
button is that it should "remember" where on the screen it was when you
clicked Save, and next time the form opens it should open at the same
location on the screen. (I added this because while I like to have it open at
the top right of the screen, somone else might prefer bottom-left or
somewhere else.) It will only remember the location if you click Save.
Is it possible to put it on the toolbar?

- It might already be on a toolbar :)

Because the macro which loads the form is called "EditFind" it should
intercept Word's Find command; this includes when the command is run by
Word's "binoculars" button. If the button with the binoculars icon is not on
any of your toolbars, one way you could add it as follows:

-- Open the template that contains the mini find stuff. (Either open it
through Word's File>Open dialog, or right-click it in Explorer and choose
Open.)
-- Click Tools>Customize to open the Customize dialog.
-- Ensure that the template containing the mini find stuff is selected in
the "Make toolbar available to" box.
-- Click the Toolbars tab.
-- Click New.
-- In the Toolbar name box, replace the suggested name with something
meaningful - e.g. My Macros.
-- Click the Commands tab.
-- In the Categories window, click Edit.
-- In the Commands window, scroll down until you can see the Find command
and, using the left mouse button, drag it onto your new toolbar.
-- Drag the new toolbar to where you want it.
-- Close the Customize dialog.
-- Save and close the template.
-- Restart Word. If the template is located in Word's startup folder the new
toolbar should be available.
i must keep pressing enter for each successive search. Is that correct?

- You do need to press Enter for each search (or click Find). I'm not sure
what you would like as an alternative to that. If you're looking for
something which finds all occurrences of the search text in one go, check out
the "Finder" utility at this link:
http://jay-freedman.info/

Regards.

Ed
 
G

Guest

Ed,
Thanks for the clarifications. I followed your instructions and I know now
that my macros is definately not working correctly. I will be working on
correcting it. I have the template in the word startup folder, but it is not
showing up correctly. I did not have the "binoculars" on my toolbar to start
out with and i created it in the toolbar in my template in the starup folder
and it did not show up when i restarted word. I had to add it again once i
opened word up. I will continue to work on it. I still can't get the form
to go transparent and I am double clicking in a blank area of it. Oh well.
I thank you very much for your code and your time. I'll keep working on it.
bcfd-IT3
 
G

Guest

Oops :)

Just noticed I got some steps in the wrong order.
-- Ensure that the template containing the mini find stuff is selected in
the "Make toolbar available to" box.
-- Click the Toolbars tab.
-- Click New.

should be ...

-- Click the Toolbars tab.
-- Click New.
-- Ensure that the template containing the mini find stuff is selected in
the "Make toolbar available to" box.

Cheers.

Ed
 
G

Guest

I figured it out. thanks though

Ed said:
Oops :)

Just noticed I got some steps in the wrong order.


should be ...

-- Click the Toolbars tab.
-- Click New.
-- Ensure that the template containing the mini find stuff is selected in
the "Make toolbar available to" box.

Cheers.

Ed
 
G

Guest

Hi again Bcfd-IT3,
I still can't get the form to go transparent and I am double clicking in a blank area >of it.

In an earlier reply I mentioned briefly that I couldn't get the form to go
transparent if I invoked it from a toolbar button but I could if I used other
ways such as Ctrl+F.

On the day on which I first created the form it consistently remained opaque
when I invoked it using a button, but when I used it today from a button it
consistently went transparent!

When the button problem was happening, once I'd loaded the form by means of
a button it would remain opaque even if I subsequently loaded it using some
other means such as Ctrl+F and I had to restart Word to allow it to become
transparent again.

I don't know what's going on there. I only got the problem when I used a
button so I was thinking of it as a button-related issue but maybe there is
some general scenario where the transparency doesn't work.

If you've used the Save button at least once, the transparency level
(actually the opacity level) is stored in the following registry key:

HKEY_CURRENT_USER\Software\VB and VBA Program
Settings\Mini-Find\Config\Opacity

Try setting that to 128 (roughly 50% transparent) and then, in a new Word
session, using Ctrl+F to see if it at least starts up transparent.

Cheers.

Ed
 
G

Guest

Ed,
I just wanted to let you know that the mini-find is working great! I'm
having no problems with it. I've got it transparent and if I double click in
a blank space, it will go darker, just like you said. I guess I just had to
close out of word completely to get it to work correctly. The only thing
that I can't get it to do which is no big deal at all, is to stay put on the
page. I don't really care about that though. As far as I'm concerned, it
works great! Thanks for your help! I even copied it to another machine and
it's working great on that machine too!
Bcfd-IT3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top