Opening a hyperlink to Word Doc

J

JoAnn

I have a recipe database where the recipes are located in the same directory
as the database. I currently have hyperlinks to the Word documents of each
recipe which is ok, but I always get this annoying message about how opening
them could harm my computer...
I would like to put them in a list box where you click a button (View
Recipe) by writing Visual Basic code to open them. Every code I've tried has
failed. Can you help? I have the categories "Recipe Title" which is the
actual title and "Recipe" which is the actual hyperlink.

How can I make the "View Recipe" button open the word document that is
stored in the hyperlink by clicking on the button? If after figuring this
out, will it still give me that annoying message?
 
D

dymondjack

If you know how to refer to the listbox value that contains the hyperlink
(I'm not sure of your setup, so can't advise on that), you can use the
Application.FollowHyperlink method.

So, on the OnClick event of your button, set a variable to the hyperlink
address (from your listbox). It would look something like this:

Private Sub Button_Click()
Dim strAddress As String
strAddress = Me.ListBox 'Assuming the hyperlink is the bound column
Application.FollowHyperlink strAddress
End Sub

As far as the warning message, I think it is because you have this saved
info as a hyperlink. If you save the filepath as regular text (instead of a
hyperlink address), I think this should go away. (you will need the full
path: C:\MyRecipies\Cheescake.Doc yumm :) )

The Application.FollowHyperlink will work with a full path as well.


Another approach would be to use the ShellExecute API.

http://www.mvps.org/access/api/api0018.htm

This is a little more advanced, though I believe it works better.


--
Jack Leach
www.tristatemachine.com

- "First, get your information. Then, you can distort it at your leisure."
- Mark Twain
 
J

JoAnn

I tried this and get runtime error 490. Any suggestions? Here's the code I
put in:



Private Sub ViewButton_Click()
Dim Recipe As String
Recipe = Me.ListBox 'Assuming the hyperlink is the bound column
Application.FollowHyperlink Recipe
End Sub

Any suggestions?
 
D

dymondjack

Here's a few things to check:

In design view, open the ListBox properties of the list box that contains
your recipes. On the 'Other' tab, make sure Multi Select is set to None.
Also, under the Other tab, verify the Name property. You will need the Name
for your code. (I use Access '03, I'm not sure if these are in the same
location in '07).

Make sure that the Me.ListBox line in your code is Me.<Nameoflistbox>

So if you list box Name was MyRecipies, that part of the code would look like:

Me.MyRecipies

So make sure that's right in your code, and we'll put in one extra line that
will tell you what the value of the ListBox is. So, pretending that the Name
of your listbox is MyRecipies, your code would look like this:

Private Sub ViewButton_Click()
Dim Recipe As String
Recipe = Replace(Me.MyRecipies, "#", "")
Debug.Print Recipe
'Application.FollowHyperlink Recipe
End Sub

(the line in your code that comes up as green, with the apostrohpe before
it, is a comment, and is not processed by VBA).

Then, in your Code Window, open the Immediate Window (View -> Immediate
Window), and you should see a value that has been printed there (the
Debug.Print line in the code should print the value of Recipe). This value
should be the same as your hyperlink to the recipe thats stored in the table.

If it's NOT the hyperlink stored in your table, then go back to the List Box
properties, and under the Data tab (in access '03), check the Bound Column.
It usually reads 1. This is what field from the ListBox's source is being
held. You may need to change that, or the RowSource of your list box, until
the BoundColumn is reading the field that contains your hyperlink. You can
test this by clicking the View button and reading the line in the immediate
window.

When the line in the immediate window is the same as the hyperlink in your
table, you are getting the correct information. Then remove the apostrophe
from the Application.Followhyperlink line (to make it actual Code instead of
a Comment) and see what you get.

See if this works...



--
Jack Leach
www.tristatemachine.com

- "First, get your information. Then, you can distort it at your leisure."
- Mark Twain
 
A

AlexM

Dim appWord As Object
Set appWord = CreateObject("Word.Application")
appWord.Visible = True
appWord.Documents.Open CurrentProject.Path & "\" & "q.docx"
 
T

tina

If it's NOT the hyperlink stored in your table, then go back to the List
Box
properties, and under the Data tab (in access '03), check the Bound Column.
It usually reads 1. This is what field from the ListBox's source is being
held. You may need to change that, or the RowSource of your list box, until
the BoundColumn is reading the field that contains your hyperlink.

changing the BoundColumn from the default 1, is something i do very, very
rarely. it's just as easy to simply refer to the needed column in code. if,
for example, the listbox RowSource has 4 columns, and the needed value is in
the 3rd column, the reference wouldbe

Me!MyListboxName.Column(2)

the column index being offset by one, of course, because in a listbox the
column index is zero-based, so the first column (moving left to right) is
(0), the second column is (1), etc. all of the above applies to combobox
controls, as well.

hth
 

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