Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Creating hyperlinks in a Word doc with VBS

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,57166
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Creating hyperlinks in a Word doc with VBS
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Creating hyperlinks in a Word doc with VBS - 2/27/2008 8:46:17 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
I'm taking on my first scripting challenge, writing a script that creates a Word doc and saves it as a users signature in Outlook. It's pretty much done but I'm trying to figure out how to have the script create a link, as often our signatures get updated  taglines that contain links. Googling produced plenty of results for VBA, but I couldn't get them to work in VBS. How do I manipulate the hyperlink object in Word with VBS?
Thanks!
 
 
Post #: 1
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/27/2008 8:58:57 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Post some of the VBA code that you found.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 2
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 12:42:45 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
I found this at http://www.mcfedries.com/ABGVBA/Chapter12.doc
Object.Hyperlinks.Add(Anchor, Address, SubAddress, ScreenTip, TextToDisplay, Target)
Object                   The object in which you want to insert the hyperlink (such as a Document or Range object in Word, or a Worksheet or Range object in Excel).
Anchor                 The object the user will click to follow the hyperlink. This could be a section of text, a Range object, or a graphic.
Address               The address of the link, which can be a file path, a UNC path, or a URL.
Remaining parameters are optional.

Then this at MSDN http://msdn2.microsoft.com/en-us/library/aa221980(office.11).aspx

These are the two best examples I've found.

Thanks!


< Message edited by Thorsteenster -- 2/28/2008 12:46:58 AM >

(in reply to Thorsteenster)
 
 
Post #: 3
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 1:35:35 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Let me be more explicit. Post code that you have actually tried to convert along with the converted VBS code and a good explaination of how it did not work.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 4
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 1:54:03 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
The VBS code was a Hey SCripting Guy article on MS, it was for a very basic sig. I added color, so really I havn't converted any syntax, just expanded on what I had. The hyperlink line dosn't return anything unless I have it in an improper format, an illegal character and leaving it open to something expected, then it just pops up with an error. It's looking like I need to set something as an anchor for the HL and do something like hyperlinks.add(anchor, www.url.com) I'm going to play with creating a variable, have it typetext the variable and set the variable name as the anchor. If your interested, here is what I have so far, it pulls info out of AD and inserts into a Word doc and saves it as a sig and sets it as default sig in Outlook.
On Error Resume Next
Set objSysInfo = CreateObject("ADSystemInfo")
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
strName = objUser.FullName
strTitle = objUser.Title
strDepartment = objUser.Department
strCompany = objUser.Company
strPhone = objUser.telephoneNumber
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
objselection.font.name = "arial"
objselection.font.size = "11"
objselection.font.bold = true
objselection.font.color = RGB(80, 0, 217)
objSelection.TypeText1 strName
objselection.font.color = RGB(255, 92, 0)
objselection.TypeText2 " | "
objselection.font.color = RGB(80, 0, 217)
objselection.TypeText3 strTitle
objselection.font.color = RGB(255, 92, 0)
objselection.TypeText4 " | "
objselection.font.color = RGB(80, 0, 217)
objselection.TypeText5 strPhone
objSelection.TypeParagraph()
objSelection.TypeText6 strCompany
objselection.font.color = RGB(255, 92, 0)
objselection.TypeText7 " | "
objselection.font.color = RGB(80, 0, 217)
objselection.TypeText8 strDepartment
objSelection.TypeParagraph()
objSelection.TypeParagraph()
objselection.font.color = RGB(0, 0, 128)
objselection.typetext9 "Interested in building a playground or in the process of planning?  You’re not alone!  Kaboom.org has hundreds of playground project postings from people just like you, who used the KaBOOM! Project Planner to make their playground dream a reality."
objselection.font.color = RGB(80, 0, 217)
objselection.typetext10 " How did a community solve fundraising issues? Or collect tools? Or round up volunteers?   You can find their answers and experiences at http://projects.kaboom.org/!"
Set objSelection = objDoc.Range()
objSignatureEntries.Add "KaBOOM! Sig", objSelection
objSignatureObject.NewMessageSignature = "KaBOOM! Sig"
objSignatureObject.ReplyMessageSignature = "KaBOOM! Sig"
objDoc.Saved = True
objWord.Quit

(in reply to ebgreen)
 
 
Post #: 5
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 1:58:05 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
I didn't even read past the first line of your code. Remove the global On Error Resume Next. Especially while you are doing your development.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 6
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:01:46 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Which generates this as my sig

Thor Jensen | computer support specialist | 202-464-6190
KaBOOM! | 4455 Connecticut Avenue NW Suite B100 Washington DC 20008
 
Interested in building a playground or in the process of planning?  You’re not alone!  Kaboom.org has hundreds of playground project postings from people just like you, who used the KaBOOM! Project Planner to make their playground dream a reality. How did a community solve fundraising issues? Or collect tools? Or round up volunteers?   You can find their answers and experiences at http://projects.kaboom.org/!

 
 But before we can implement it, I need to have it create hyperlinks.
I'm a little surprised though that Word dosen't automatically parse it as a link since it will format typed in URL's into hyperlinks. I even tried in the script changing it from HTTP to WWW but it didn't, still just plain, unlinked text.

(in reply to Thorsteenster)
 
 
Post #: 7
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:03:22 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Ahh, ok, so then if it hangs on my hyperlink.add line I'll know instead of thinking it passed but produced nothing.

(in reply to Thorsteenster)
 
 
Post #: 8
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:05:47 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Is this all of your code? I can't think that it is. There are a lot of objects in the code that are never created. Where are they coming from?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 9
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:24:46 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Sorry, I'm definately a newb to scripting, which objects do you mean? If its say, objselection.font.name, thats calling for the font type object in Word.
Here's the Hey Scripting Guy article: http://www.microsoft.com/technet/technetmag/issues/2006/10/HeyScriptingGuy/default.aspx

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:29:32 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
So are you running this as VBScript or as VBA? Either way, use the macro recorder in office to generate code that does what you want.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 11
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:39:42 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Im running it as a .VBS, or saving it as a .VBS file then just double clicking it. I'll check out the macro recorder, but is that something I would be able to run as a script, or would it have to run from within Word? I haven't done anything with macro's.....yet!

(in reply to ebgreen)
 
 
Post #: 12
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 2:47:57 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
To actually use the macro itself you would need to run it in Word. The real use of the macro recorder for a scripter is to record a macro doing what you want then look at the generated code and convert it over to VBS.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 13
 
 RE: Creating hyperlinks in a Word doc with VBS - 2/28/2008 3:41:08 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Cool, that will give me another area to explore. MS help actually helped, I was trying to paste the VBS into the macro. I'll play with it and try to generate the code. Thanks for some direction, I'll post what I come up with.

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 2:07:08 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Whew, ok, I got sidetracked but now am back to this project. Here is the code I got from recording a macro:
Selection.TypeText Text:="www.linkhere.com"
   Selection.MoveLeft Unit:=wdCharacter, Count:=16, Extend:=wdExtend
   ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
       "www.linkhere.com", SubAddress:="", ScreenTip:="", TextToDisplay:= _
       "www.linkhere.com"

I have tried pasting it into the script, changing selection to objselection to match the rest of the script, and a couple of other things. I think it might be something with VBS not understanding, or not the right format, the Activedocument.Hyperlinks.Add statement.
I got a couple of other thoughts I'll try.

(in reply to Thorsteenster)
 
 
Post #: 15
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 2:09:06 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Post the VBS code that you are trying along with the error that you get.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 16
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 2:42:37 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
here is the whole script, in the next post I'll post a couple variations and the different error messages.
Set objSysInfo = CreateObject("ADSystemInfo")
 
strUser = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUser)
 
strName = objUser.FullName
strTitle = objUser.Title
strDepartment = objUser.Department
strCompany = objUser.Company
strPhone = objUser.telephoneNumber
 
Set objWord = CreateObject("Word.Application")
 
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection
 
Set objEmailOptions = objWord.EmailOptions
Set objSignatureObject = objEmailOptions.EmailSignature
 
Set objSignatureEntries = objSignatureObject.EmailSignatureEntries
 
objselection.font.name = "arial"
objselection.font.size = "11"
objSelection.TypeText strName
objSelection.TypeParagraph()
objselection.TypeText strTitle
objSelection.TypeParagraph()
objselection.TypeText strCompany
objSelection.TypeParagraph()
objSelection.TypeText strDepartment
objSelection.TypeParagraph()
objselection.TypeText strPhone
objSelection.TypeParagraph()
objSelection.TypeParagraph()
objselection.font.bold = true
objselection.typetext "Interested in building a playground or in the process of planning?  You're not alone!  Kaboom.org has hundreds of playground project postings from people just like you, who used the KaBOOM! Project Planner to make their playground dream a reality."
objselection.typetext " How did a community solve fundraising issues? Or collect tools? Or round up volunteers?   You can find their answers and experiences at "
objselection.font.underline = true
objselection.font.color = RGB(0, 0, 255)
objselection.typetext "http://projects.kaboom.org/"
objselection.font.underline = false
objselection.font.color = RGB(0, 0, 0)
objselection.typetext "!"
 
Set ActiveDocument = objdoc.range()
 
ActiveDocument.Hyperlinks.Add Anchor = ObjSelection.Range, Address = _
        "http://kaboom.org", SubAddress = "", ScreenTip = "", TextToDisplay = _
        "http://kaboom.org"
                       
                       
 
Set objSelection = objDoc.Range()
 
objSignatureEntries.Add "KaBOOM! Sig", objSelection
objSignatureObject.NewMessageSignature = "KaBOOM! Sig"
objSignatureObject.ReplyMessageSignature = "KaBOOM! Sig"
 
objDoc.Saved = True
objWord.Quit

Returns this error:
Line: 47
Char: 1
Error: Type Mismatch: 'ActiveDocument.Hyperlinks.Add"
Code: 800A000D

Line 47, Char 1 is the ActiveDocument.Hyperlinks.Add statement.
Also, with this one here, I tried setting ActiveDocument to ObjDoc.range

< Message edited by Thorsteenster -- 3/20/2008 2:44:00 AM >

(in reply to ebgreen)
 
 
Post #: 17
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 2:49:11 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Then If I try ObjSelection.Hyperlinks.Add I get:
Error Type Mismatch: 'objselection.hypoerlinks.add'
Code: 800A000D

(in reply to Thorsteenster)
 
 
Post #: 18
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 2:56:06 AM   
  Thorsteenster

 

Posts: 18
Score: 0
Joined: 2/27/2008
Status: offline
Oops, the code in post 17 I had formatted the text I want to be a link so that it looked like a link. I was hopping it might have been something as easy. It looks good in my sig, but has no actual link.

(in reply to Thorsteenster)
 
 
Post #: 19
 
 RE: Creating hyperlinks in a Word doc with VBS - 3/20/2008 3:01:12 AM   
  ebgreen


Posts: 4972
Score: 31
Joined: 7/12/2005
Status: offline
Try this:


ActiveDocument.Hyperlinks.Add(ObjSelection.Range, "http://kaboom.org",,, "http://kaboom.org")

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Thorsteenster)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Creating hyperlinks in a Word doc with VBS Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts