+ Reply to Thread
Results 1 to 25 of 25

Use Excel VBA to add a hyperlink to a word document

  1. #1
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18

    Use Excel VBA to add a hyperlink to a word document

    I have a word template that I open (as a new document) with excel VBA.
    This template has bookmarks in it and I can copy and paste text or cell values from excel to the template to fill it out automatically. The one area I am having problems is in trying to create an email hyperlink in one of my bookmarks.

    Does anybody have any suggestions?
    Here is an example of the code I am using that does not work correctly:
    Please Login or Register  to view this content.
    I have tried several variations on this and cannot seem to get it to work. I can paste the "LinkName" to the bookmark and select the bookmark just fine, but I cant get the hyperlink to attach.

    Any help would be greatly appreciated.
    Rick

    P.S. I am using Office 2007 for this project. The Macro starts and runs in excel which then opens the word document. I wasn't sure if any of that was clear or not.
    Last edited by r0cket88; 12-02-2008 at 08:35 AM. Reason: Update som information

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Needs a bit more tweaking but maybe:
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 12-02-2008 at 02:32 PM.

  3. #3
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    When I tried your code I get an error as follows:
    Run Time Error '-2147417851 (80010105)':
    Method 'Add' of object 'Hyperlinks' Failed

    When I press the debug button it takes me to the mydoc.hyperlinks.add line in your code.

    If I press the Help button the dialog box pulles up an Automation Error (Error 440).

    This also locks up microsoft word to where I cannot do anything with it. I can't select a location to add or delete text, and I can't even scroll the window. The only thing I can do is close out Word. When I close word it creates an error to send to microsoft as well.

    Your code did get me closer however as before I close Word I can see where the macro has added blue underlined text to the bookmark. the text however is "Mailto:%20Email@Address.com" This also deletes the bookmark from the document. I don't really care if that is the end result, but it may be something useful as far as information goes.

    I appreciate the help. I am just having a really hard time getting my head around which objects I need to use to do this and then what properties I need to set to get it to work right.

    Thanks
    Rick
    Last edited by r0cket88; 12-02-2008 at 08:45 AM. Reason: additional information

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    I did mine in 2003 using a DOT file. That might be the difference. Mine put the "Name Here" as the link text.

    As I said, it needs a bit more work to avoid the error when closing. I generally just use DOC files to do this kind of thing. You can easily copy the original DOC file and use it. If you want an example, let me know. I think this would avoid all the problems. Otherwise, I would have to see how to tweak your code some more.

  5. #5
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    well, I need to file to be a .dotm file as there is another macro that runs on document close when the user is done editing it.

    I discovered something and thought it might be worth investigating.
    If I copy a cell that includes a hyperlink and then paste in Word, it will take the hyperlink formatiing with it.

    Currently the excel file is set up where the name (Text to Display) comes from one cell and the address comes from another. I may be able to use these cells to create the hyperlink in excel and then copy the cell and paste to the word bookmark.

    I really appreciate all your help on this.
    Rick

  6. #6
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Well, I found something that may work. Now I just need to tweak it so that it looks right in my template.

    If you copy a cell that contains a hyperlink in excel, and then paste it using pastespecial in word, it will paste the hyperlink correctly. The only issue I am having now is that when it pastes the cell it adds a carriage return at the end which screws up the spacing on my template.

    So the code that works to get a hyperlink from excel to word is as follows:

    Please Login or Register  to view this content.
    For future reference if anyone else chooses to use this code, be sure to add error trapping and to release the word document after you are done controlling it with excel.

    Anyone have any thoughts on the carriage return issue?
    Rick

  7. #7
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You can use Replace to replace the vbCrLf.

  8. #8
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    do you mean using a find and replace in word?
    I was under the assumption that the line feed or carriage return that is inserted is no different than any other line feed in Word that is created by pressing enter. If this assumption is incorrect, how would I search for the VbCrLf? I know there are some special search terms for things like carriage returns and other formating items, is there one for this particular item?

    Rick

  9. #9
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You can try changing the DOCM to DOTM and see how this works for you. This method takes into account the scenario where MSWord may already be open.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 12-02-2008 at 02:50 PM.

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    After the paste, select the bookmark and then use the Selection in VBA's Replace().

    Untested:
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Quote Originally Posted by Kenneth Hobson View Post
    After the paste, select the bookmark and then use the Selection in VBA's Replace().

    Untested:
    Please Login or Register  to view this content.
    The problem with this technique is that when you paste into the bookmark, the bookmark itself is removed. Once you have pasted into the bookmark, you can no longer search for that bookmark to do a replace.

    Now I have several bookmarks after this, and I may be able to do a search for them, but then I would have to back up to vbCrLf and delete it.
    the document is actually formatted as such:

    Cc: Vendor: [txtVendor2], [txtVendor3], [txtVendor4]
    Verification:[txtVeri]
    Purchasing:[txtPurch]

    The bookmarks and their actual names are in the brackets above. So if I place a hyperlink in the bookmark named txtVendor2, I could then search for the VbCrLf backwards from the txtVendor3 bookmark.

    Rick

  12. #12
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    There are ways to do it but I would just use the method that I posted earlier. Have you tried it?

    If you want to go down the long path of the copy/paste method, we can fix that problem if needed.
    Last edited by Kenneth Hobson; 12-02-2008 at 05:24 PM.

  13. #13
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Quote Originally Posted by Kenneth Hobson View Post
    You can try changing the DOCM to DOTM and see how this works for you. This method takes into account the scenario where MSWord may already be open.
    Please Login or Register  to view this content.
    When using this code I get a runtime error 424 on the wd.hyperlinks.add line

    I have changed this line to accomodate the code I already have in place, so that line actually currently reads as:

    Also, I am using this code with a .docm file. I may have misunderstood, but I was under the impression that the errors we were getting before were caused by the .dotm file.

    Please Login or Register  to view this content.
    Thanks again
    Rick

  14. #14
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Kenneth,

    I really appreciate all of your help on this. This morning I am trying to get your code to work and I feel like I am very close. here is the code I currently have:

    Please Login or Register  to view this content.
    This code results in a hyperlink being placed in the bookmark with the correct email address when you ctrl+Click to follow the hyperlink. The only problem is that the TextToDisplay does not show up correctly. The displayed text shows up as "﷼ 4". The cell that I am getting the information from (E6) contains the name Mike Dennis. When I cycle through the macro by individual lines the LinkName Variable shows the correct information, I am just not sure why that does not transfer over to the document like it should.

    Rick
    Last edited by r0cket88; 12-03-2008 at 09:17 AM.

  15. #15
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    You don't really need LinkName and LinkAddress unless you reference it more than once. Just use it directly. However, being such a small program, it makes little difference either way.


    Select is generally not needed and can slow things. It is better to use something like:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    The code that you are seeing is just a small part of the overall program that I am working with. It actually involves 2 user forms, 1 workbook and the Word Document.

    I am using the linkname and linkaddress because ultimately I am putting 8 hyperlinks into different bookmarks throughout my document and it was my opininon that it would be easier to code and follow the code if I used the defined variables.

    In the actual program I am dealing with, I am using a find on a specific sheet of the workbook to find the row that I need to work with and then assigning the value to LinkName and LinkAddress by the following code:

    Please Login or Register  to view this content.
    Where x is determined by which particular persons information I am trying to recover.

    Any ideas other ideas why my linkname is not coming out correctly?
    I will go try a few things and see if I can get anything to work.

    Thanks again.
    Rick

  17. #17
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Does debug show what you expect? e.g.
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Please Login or Register  to view this content.
    The above code functions correctly. I think this will get me where I need to be for the Macro I am working on. I may have to change the NameCell and AddressCell ranges for each person that I need to add to the document, but that shouldn't be to hard.

    I do also have a data validation to determine who and how many people the user wishes the document to be sent to so that will cut down some on the processing time.

    Unless you have any suggestions, I think this is what I will use and we can call this one solved. I really don't understand why they had to make it so difficult to do this as it seems like it should be a fairly simple task.

    Thanks for all of your help

    Rick

  19. #19
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Quote Originally Posted by Kenneth Hobson View Post
    Does debug show what you expect? e.g.
    Please Login or Register  to view this content.
    I tried something similar by putting in a messagebox to print the results of linkname and yes it shows the correct result. It just seems weird that I don't get what I am expecting.

    When I run the debug.print LinkName code in the immediate window, yes I do get what I am expecting to showup.

    I even tried setting it as:
    Please Login or Register  to view this content.
    I get the same result in a messagebox (and the immediate window) but the same problem in Word. It looks like it is an issue with Word though because sometimes it shows up in word as what looks to be an asian character. Of course if the font is wrong and the character cannot be shown, then Word will display it as a square.

    Rick

  20. #20
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Might be a 2007 issue. If you post an xls with the problem and a sample docm, I can test how it marks up the docm in 2003.

  21. #21
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    I have attached the xls that I used.
    I was not able to attache the .docm file as I can not get it to save below the 10.0kB minimum allowed file size.

    All the document is though is a macro enabled document with a bookmark in it that is named txtVendor2

    The original document is set up something like a fax to where it looks like this:

    Cc: Vendor: [txtVendor2]
    Purchasing:

    I don't know why it does this. granted the other code works so I can use that and have tested it in my main macro as functional.

    Rick
    Attached Files Attached Files

  22. #22
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    I changed:
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    after it gave the link text as 1600. Later, it worked as it was which makes sense since the default property of the Range is Value. However, I do recommend that you use the method that I posted where I set the reference using either GetObject() or CreateObject(). Otherwise, you get errors when you try to close the 2nd instance if you only used the CreateObject method.
    Last edited by Kenneth Hobson; 12-03-2008 at 03:02 PM.

  23. #23
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    I got it working the way you suggested. If I recall correctly, the method you are suggesting and using is called late binding and the method I was using (Dim wb as excel.application) is considered early binding.

    Is there any reason you recommend the late binding over the early binding?

    I initially had some issues because I needed to get rid of all the original bindings, but I have it set up correctly now.
    On to the next Task on my design list.

    Rick

  24. #24
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    There are arguments for one binding method over another. Generally, I like early binding so that I can make use of intellisense. Since you are doing this from Excel, the reference to it is the default and preset. It is the reference to a new MSWord or an existing MSWord that we have to check for. I have not done it with early binding but that doesn't mean that we can't do it.

    Someday, I probably need to see if I can do it using early binding. One can use an error routine as I did in the late binding method or other methods like API's FindWindow(). Part of it comes down to using New in the Dim or not.

    cheers

  25. #25
    Registered User
    Join Date
    09-15-2008
    Location
    White Lake, Mi.
    Posts
    18
    Thanks for all the info Kenneth. I really appreciate all your help.

    On a personal note, I used to leave closer to you as I grew up in Kansas City and went to school in Pittsburg, Ks. just north of the OK State Line.

    Thanks again
    Rick

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1