Office & Productivity Software forum

General discussion

creating multiple hyperlinks in word

by seekaye / July 26, 2007 2:20 AM PDT

I have a directory with 50 or so word/excel files in

i'd like to create a hyperlink for each one in word

e.g. if the first file was MyFile.doc

the first line on my word doc would be MyFile.doc (or even better just MyFile)

Is this possible? it takes ages to do each one indiviudally
can anyone write me a macro?

thanks

Post a reply
Discussion is locked
You are posting a reply to: creating multiple hyperlinks in word
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: creating multiple hyperlinks in word
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
No, sorry no-one can write the macro for you.
by MarkFlax Forum moderator / July 26, 2007 4:18 AM PDT

It just isn't possible without knowing all the names of the files you want to link to. And I would recommend against you sharing such information with others.

Do you know how to create hyperlinks in Word? It is very easy and for the time taken for you to write your post and wait for me and any others to reply, the job would have been done.

What is difficult here?

Mark

Collapse -
why?
by seekaye / July 26, 2007 5:10 AM PDT

i have loads(hundreds)of files i want to hyperlink into my main document and doing them one by one takes ages, highlighting the text and then the files i want arent in the default directory so i have to change that each time.

it would be much easier for me to have the list then i can cut and paste them where i want.

i'm working on visual basic express for a solution - currently i have got a dialogbox to select a directory and then all the files in that directory are listed in a word document which i opened from VB express.

just got to tuen them to hyperlinks now!

Collapse -
here's the code
by seekaye / July 26, 2007 6:15 AM PDT
In reply to: why?

Sorted

i wrote this in VB express - i didnt know i could interact with word from VB before i tried this, i thought i'd have to use a macro

it creates hyperlinks to every file in a selected directory in a new word doc.

when i'm dealing with hundreds of files in the directory it is quicker than doing it manually, hence the motivation

[microsoft word 11.0 object library needs added to COM references]

'CODE START

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'USER SELECTS A FILE AND STORES IT IN FILE

Dim file As String
file = ""

OpenFileDialog1.FileName = ""

If OpenFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
file = OpenFileDialog1.FileName
End If

'STORE ALL THE FILES IN THE SAME DIRECTORY IN A NEW ARRAY CALLED FILE

Dim files() As String = System.IO.Directory.GetFiles(GetDir(file), _
"*.*", IO.SearchOption.TopDirectoryOnly)

Dim a As Integer

'OPENS UP A NEW WORD DOCUMENT

Dim objWord As New word.application

objWord.Visible = True
objWord.Documents.Add()

'FOR EVERY FILE IN THE files ARRAY CREATES A HYPERLINK IN THE WORD DOC

For a = 0 To UBound(files)
objWord.Selection.Hyperlinks.Add(Anchor:=objWord.Selection.Range, _
Address:=files(a), _
SubAddress:="", _
ScreenTip:="", TextToDisplay:=GetFile(files(a)) + Chr(13))
Next

End Sub


'RETURNS THE PATH OF THE DIRECTORY OF A GIVEN FILE

Function GetDir(ByVal file)

GetDir = ""
Dim a, last As Integer

For a = 1 To Len(file)
If Mid(file, a, 1) = "\" Then last = a
Next


For a = 1 To last - 1
GetDir = GetDir + Mid(file, a, 1)

Next
MsgBox(GetDir)

End Function

'RETURNS THE FILE NAME GIVEN THE ENTIRE PATH

Function GetFile(ByVal file)

GetFile = ""

Dim a As Integer

For a = 1 To Len(file)
If Mid(file, a, 1) = "\" Then
GetFile = ""
Else
GetFile = GetFile + Mid(file, a, 1)
End If

Next

End Function

End Class

'CODE ENDS

Collapse -
Very impressive
by MarkFlax Forum moderator / July 26, 2007 6:45 PM PDT
In reply to: here's the code

I know a little VBA for Excel but have never dabbled in VBA for Word before. I'm much impressed, and I am glad you have got it sorted.

I misunderstood your original post, where you said, "I have a directory with 50 or so word/excel files in", I thought you meant you had just 50 files you wanted to link to.

Nice work, and well done.

Mark

Popular Forums
icon
Computer Help 49,613 discussions
icon
Computer Newbies 10,349 discussions
icon
Laptops 19,436 discussions
icon
Security 30,426 discussions
icon
TVs & Home Theaters 20,308 discussions
icon
Windows 10 360 discussions
icon
Phones 15,802 discussions
icon
Windows 7 7,351 discussions
icon
Networking & Wireless 14,641 discussions

Tech explained

Do you know what an OLED TV is?

CNET explains how OLED technology differs from regular TVs, and what you need to know to make the right shopping decision.