Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Automate Adding Hyperlinks to a Word Document? 2

Status
Not open for further replies.

PeggySDay

Programmer
Apr 13, 2007
17
US
I must admit that I'm very green at programming and this may seem like a simple problem.

I have an index of documents in MS Word 2003 which are in a numbered list. I'd like to create a macro to go to each numbered document, select the document, and insert a hyperlink that references the document, which is saved in a directory. The list would look something like this:

1. Management Agreement
2. Deed of Trust
3. Pledge and Security Agreement

There are almost 300 documents so that's why I want to automate this project. The first document would be saved as 001.pdf, the second document would be saved as 002.pdf, and so on, which is on my hard drive.

Any help would be sincerely appreciated!

 
Hi Peg,

Ah, Microsoft Visual Basic 6.3 - the code I posted is for VBA. You add the VBA code to a Word document or template directly. I hadn't seen any mention of using VB instead of VBA in your previous posts. Try adding the code to the document that you want to create the hyperlinks from and running it from there.

You'll need to do that with the code for updating the links anyway.

Cheers

[MS MVP - Word]
 
macropod, Visual Basic 6.3 is VBA.
when in VBE menu ? -> About visual Basic
 
Hi PHV,

Thanks for that - not having Word 2003, I wasn't aware they're the same beast. In that case, it shouldn't make any difference to how the code runs - unless its being run from an app other than Word. If that's the case, the appropriate Word references would need to be added.

Cheers

[MS MVP - Word]
 
Can you give me some specific step by step instruction when you write:

"Try adding the code to the document that you want to create the hyperlinks from and running it from there."

I am in the document that I want to create the hyperlinks from, and pressing alt+F11 to open up Visual Basic.

Under Project Explorer, I can see this branching:

Normal
Modules
New Macros

Does any of this help?

 
You should have a Project at the same level than Normal.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, PH.

Can you tell me how to be sure I'm adding this macro to the Normal template?

Regards, Peg
 
You'll put your Normal.dot on the CD ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
To add code to the Normal template:

1) Open VBA

2) Open the Project Explorer (Ctrl+R), if it is not open already. (By default, the Project Explorer is located on the left-hand side of the VBA environment, unless it's been moved.)

3) Select "Normal". If you wish, click on the + sign next to Normal to view its contents.

4) Choose Insert|Module to insert a module in the Normal template.

[red][banghead]— Artificial intelligence is no match for natural stupidity.[/red]
 
PH, nope the normal.dot will not be on the CD. But, once I've used the macro to insert the hyperlinks I don't need the macro any longer.

duGly-Because I work for a large firm that has done a lot of macro development, trying to look at the Project Explorer window to figure out the lay of the land is somewhat difficult. I went another route and opened the normal.dot file, pressed alt+F8 to display macros, chose Normal.dot (global template) under "Macros In," and the macro appears in the list.

Any other suggestion? I do appreciate the help I'm getting. One day I hope to reciprocate.

Many thanks- Peg
 
I'm a bit confused, Peg. Perhaps we're referring to two different things when we talk of the Project Explorer.

The Project Explorer I refer to is a window within the VBA window. As I mentioned, it is traditionally on the left-hand side of the window, unless it has been moved. If it has been closed, you may open it by pressing Ctrl+R.

The Project Explorer shows a project for each open Word document (along with its template). So if you have one new, unnamed document open in Word, the Project Explorer will have an entry for

1. Project (Document 1), and
2. Normal.

Each entry represents a project, and contains all modules, class modules and userforms for the project. The entries are listed in a tree view format, allowing you to view/hide their contents by clicking the + or - signs.

To add a macro to the Normal template, highlight the Normal folder, and choose Insert|Module. Open the newly created module, rename it if you wish (in the properties window), and start writing your code.

Does that make more sense, or am I still not understanding your question?

[red][banghead]— Artificial intelligence is no match for natural stupidity.[/red]
 
Hi Peg,

You'll need two code modules -
. one in Normal.dot, for your HyperlinkInsert sub, since you'll be wanting to use this regularly; and
. one in each working document, for holding the rest of the code.

Pasting the HyperlinkInsert sub into Normal.dot's 'New Macros' module should work fine.

Cheers

[MS MVP - Word]
 
dUgly- I think we're talking about the same thing re: Project Explorer.

Here's my tree:

Normal
Microsoft Word Objects
This Documents
Forms
cmdControl
UserForm1
Modules
NewMacros

Another indication this macro is saved under the Normal.dot is that in the title bar of the code window appears "Normal-New Macros(code)." That being the case, I'm at a loss.

Thanks for the info., MacroPod. What would you suggest I try next?

[smile]

 
Hi peg,

Once you've inserted the code, activate your working document and run the HyperlinkInsert sub.

Cheers

[MS MVP - Word]
 

Hello Macropod,

I hope that I'm not being annoying, but I want to make sure I understand. Can you tell me the steps for-

[li]inserting the code[/li]
[li]activating the working document[/li]

When you say "insertig the code" is that something that I haven't done already?

Does "activating the working document" have anything to do with entering coding?

Thanks for your patience!!
[flowerface]
 
Hi Peg,


OK, If you start Word and close all documents, all that will be active is the Normal.dot template. Based on your previous posts, if you press Alt-F11 to open the VBE window you will see:

Normal
Microsoft Word Objects
Modules
New Macros

At this point, you could simply double-click 'New macros' to open that module and paste the HyperlinkInsert sub into it. Alternatively, could could add a new code module (via Insert|Module) and paste the HyperlinkInsert sub into that.

Now go back into Word's document pane (Alt-F11 again) and open up your working document. It's now active. If you now run the macro, your working document will be modified with the hyperlinks.

If you press Alt-F11 again, to go back into the VBE, you'll see a reference to your working document:
Project (working document)
Microsoft Word Objects
This Document
References

Click on your working document project and use Insert|Module to add a code module to it. Now copy & paste the rest of the code I gave you into that. Then go back into Word's document pane and save your working document. Done.

Cheers

[MS MVP - Word]
 
Hello MacroPod-

Thank you for your diligence on this problem and also for the detailed explanation. After reading your explanation, I feel certain that I'm taking the correct steps.

I continue to get the same error message so I'm writing to our macro developer here at work to ask whether there might be anything running in the background that could be causing it to fail. Our computer systems at work load numerous templates that are locked. I can't even view their contents.

I also decided that I would run the macro on my home computer as it's running different software:

-MS Windows XP Professional
-MS Word 2002
-MS Visual Basic 6.3

I was so sure that it would run on my home computer. Brace yourself for the new error message:

Runtime error '13': Type mismatch

It breaks on this line of code:

Code:
If oPara.Range.Words(1).Text = (oPara.Range.Words(1).Text) ^ 1 Then

I just can't win. What are your thoughts about type mismatch? What does that signify?

Thanks again my friend from Oz-
Peg
 
Try this instead:
If IsNumeric(oPara.Range.Words(1).Text) Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Brilliant, PHV!!

Within about 5 seconds it marked the entire document and referenced each address accurately.

You deserve my unending gratitude!!!

My confidence in VB has been restored. I salute you. Please excuse my overexuberance, but I'm soooo relieved.

MacroPod, you also deserve an equal amount of praise!

[thumbsup2]

My warmest regards,
Peg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top