Using Hyperlinks within Userforms

  • Thread starter Thread starter assertec
  • Start date Start date
A

assertec

Hi all,

I have a Userform with various command buttons in place. What I'm
trying to do is allow users to be able to open various Word documents
when they click on these command buttons via the use of hyperlinks
embedded within the click event of each command button.

So if they click on one of the command buttons it opens one Word
document, and if they click another, it opens the second Word document
etc. etc.

It would be ideal to have the Userform to always remain displayed, so
the documents open in the background, and the Userform always has the
focus... then once the desired documents are all opened they can then
close the Userform if they wish.

Thanks for your help

Karen
 
Hello Karen,

Hyperlinks are not avaibale to the UserForm in VBA. Since you are
launching Word documents from the command button click event, you can
use an API call to have the system open the document. Using a few more
API calls, it is easy to get the Form to stay on top of all the other
documents. Add a VBA module to your workbbok and copy the code between
the lines into it.
________________________________________________________

'Returns the Window Handle of the Active Window
Declare Function GetActiveWindow _
Lib "user32.dll" () As Long

Private Declare Function SetWindowPos _
Lib "user32.dll" _
(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

Declare Function ShellExecute _
Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Public Sub OpenFile(ByVal File_Name As String)

Dim RetVal
RetVal = ShellExecute(0&, "open", File_Name, vbNullString,
vbNullString, 1)

End Sub

Public Sub KeepFormOnTop()

Dim hWnd As Long
Dim RetVal

Const HWND_NOTOPMOST = -2
Const HWND_TOPMOST = -1
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1

hWnd = GetActiveWindow()
RetVal = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE + SWP_NOSIZE)

End Sub
________________________________________________________

To the Command Button Click events add the following line of code:
(You must include the Directory path along with the file name)
Be sure to change the Directory and File name to what you are using.

Call OpenFile("C:\Documents and Settings\Karen\My Documents\Document
1.doc")


To the USerForm Activate event add this line of code:
Call KeepFormOnTop

Sincerely,
Leith Ross
 

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

Back
Top