PDA

View Full Version : Hyperlink without bookmarks



dansyoz
02-12-2010, 05:09 AM
Firstly I've tried to find this issue in the existing posts but couldn't. Obviously if there is a similar issue (and especially if there is a solution) I'd appreciate direction to that post.

So whats the issue?
I have a utility which searches one or more read-only documents for 1 or more terms in a given list and which then creates a new results document for each source document which contains a tabel of 3 columns, text term searched for, sentence (or paragraph) in which the term was found, and page number.

Results can be sorted by any of these columns based on user selection at the start.

The problem.
I am trying to include a hyperlink in column 1 (search text) which not only opens the document but moves directly to the exact location in the source document where this was found.
I have tried a range of approaches (including pastespecial which has a bug in the MS code where it sometimes says the clipboard is empty when it is not).
I can create the hyperlink and have tried a range of approaches but none toake me to the exact location consistently.
I cannot insert bookmarks into the source document as these are provided to me as read-only.
I have already tried (amongst other things)

.Hyperlinks.Add Selection.Range, Address:=SourceDoc.FullName, SubAddress:=SourceDoc.ActiveWindow.Selection.Sentences(1)
.Hyperlinks.Add Selection.Range, Address:=SourceDoc.FullName, SubAddress:=SourceDoc.ActiveWindow.Selection.Information(wdActiveEndPageNum ber)
.Hyperlinks.Add Selection.Range, Address:=SourceDoc.FullName, SubAddress:=SourceDoc.ActiveWindow.Selection.Information(wdFirstCharacterLi neNumber)
As well as a range of other approaches including PastespecialIf I manually paste a hyperlink (Paste as Hyperlink) then I can see (with field codes turned on) that a switch \S is added by word.
Example - {hyperlink "deltext.doc" \S "1,4240,4245,0,,junk "}
which appears in the example to be of the format

\S - maybe stands for subaddress - not sure
param 1 = 1 - appears to indicate the structural location ie. document body (this appears to be 2 if the link is to the header or footer, 3 if a footnote etc)
param 2 = 4240 which seems to correspond to the character number of the first character in the selection eg. 4240 characters from start of doc (confirmed with selection.start value)
param 3 = 4245 which seems to correspond to the character number of the last character in the selection eg. 4245 characters from start of doc (confirmed with selection.end value)
param 4 = I think this corresponds to wdbuiltinstyle
param 5 - no value so no idea
param 6 - hyperlink text that is displayedThe questions
Is there a way to set this in vba so I can get a consistent hyperlink approach to work.
Is the structure of hyperlink parameters documented and readily available on the web somewhere (I haven't had much luck finding it)

Hoping someone can at least point me somewhere useful to look if not (ideally) reply with - "I had that exact same problem"
Thanks

macropod
02-14-2010, 05:49 PM
Hi dansyoz,

AFAIK, it's not possible to hyperlink to a specific location in a Word document without using a bookmark for the sub-address. And, again AFAIK, even if you create a hyperlink to a location that isn't already bookmarked, Word automatically inserts a hidden bookmark at that location.

Even if it were possibly for the hyperlink sub-address to use some form of offset referencing (which would have to be a character offset for a Word document, because of the page dimension and printer dependant pagination issues), any subsequent edits to the referenced document could easily invalidate the offset.

dansyoz
02-14-2010, 08:19 PM
Macropod thanks for the reply.

As far as I am aware Word can't insert hidden text in a read-only document. If they can do this without some update of the file modification then what else can they do to our docs without flagging it?
Regardless, testing the paste as hyperlink manual approach and then

looking for bookmarks and hidden text - shows nothing
opening the source document in write mode then running document inspector to remove all potential hidden text then following the link from the results document - still takes you to the exact word.As far as the offset being out if the source document was edited then yes I understand that but the source document is a read only document that is version controlled and would therefore be a NEW document if edited by its author so thats not a problem for me.

Seems to me that you can (vis paste as hyperlink) generate a hyperlink to an exact location in a word document without a bookmark but Microsoft don't document this anywhere that I can find.

While I am doing this primarily on a Vista PC running Word 2007, I have also tested these same sample files on the Mac version of word (2004) I have and the links still hold.
In fact the mac is more informative and its link value <edit hyperlinks function> suggests that the string of the link (Source document.doc#%091,4474,4482,0,,Aliquam%20) from the # onwards is is most likely an anchor value. Now I just need to find out how to set that in VBA.

Regards
Dansyoz

macropod
02-14-2010, 09:01 PM
Hi dansyoz,

It's not a matter of inserting hidden text, but of inserting a bookmark. Even in a read-only document, you can do this (as part of the hyperlink creation process) while the file is open but, as soon as you close it without saving, the bookmark is lost. That might explain your inablility to get consistent results. Hyerlinking to Excel worksheets and Powerpoint slides is much easier, since there's always a cell address (Excel) or a slide (Powerpoint) to link to. Word documents aren't like that, since there's no corresponding structure to use. Consequently Word documents require a bookmark - be that hidden or otherwise.

dansyoz
02-14-2010, 09:17 PM
Hi Macropod,
thanks again for taking an interest in this.
I went down the "hidden" path per your first reply - "Word automatically inserts a hidden bookmark".

After your initial reply I looked at this carefully hoping this was the answer but the manual paste as hyperlink approach I noted (i.e the absolute character refs) does not create bookmarks (visible nor as per your first post hidden) yet it works (so far in my testing) every single time so ergo there must be a way of creating a hyperlink to a specific word or part of a document without one.

Even after closing and reopening the files several times and sending them to another platform (Mac) then opening them there they appear to continue to work consistently if manual paste-as-hyperlink is used.

The consistency issue I mentioned in my initial post is in regards to the pastespecial approach which I discovered some years ago was not reliable.

Basically with pastespecial there appears to be a bug in regards to how it views the clipboard - sometimes regarding it as empty when the exact same code with paste instead of pastespecial worked every single time (I ran ridiculous testing on this to try and resolve it without success).

Cheers
dansyoz

Dave
02-14-2010, 11:17 PM
Why not create yourself a character count array of your search items? As macropod suggested, I think it may be possible (and preferable) to VBA a hyperlink to a doc's character location. So during your word search, you collect the character locations of the finds and connect them to the search words with an array of character counts which you later use to set the hyperlink location. Haven't done it myself but seems somewhat logical. HTH. Dave

On edit: If you set the document Selection to the found word character count, would it avoid the use of hyperlink? (with a little VBA to open the doc and view it at the Selection point)

dansyoz
02-15-2010, 04:05 AM
Dave
Thats essentially what I am trying to do.
Selection.Start and Selection.End yield the required character offsets so its just finding the appropriate way to get the hyperlink inserted.
It would help immensely if I could find some document regarding the hyperlink parameters - til then I'll make the assumptions re structure per my first post.

Re your edit.
The addition of VBA code to the results file is not desirable as the ultimate recipient of the results file (with the hyperlinks) may have a security setting preventing macros.

Anyway have a solution testing which I will post shortly if it works.
Cheers

dansyoz
02-15-2010, 04:06 AM
Quick entry so I can post my sample code - getting blocked because it is recognising some of the code sample as a link and only have 4 posts.

dansyoz
02-15-2010, 04:22 AM
I now have some code working that seems so far (still testing) to build the hyperlinks I want albeit a more complicated approach than it should be and with one issue. The displayed text is the filename so I assume there's another parameter I have to add.

Example heavily edited subset of code to insert a hyperlink to a specific location follows :

Dim strHlinkText As String
Dim strtemp As String
Dim lngLinkStart As Long
Dim lngLinkEnd As Long


lngLinkStart = SourceDoc.ActiveWindow.Selection.Start
lngLinkEnd = SourceDoc.ActiveWindow.Selection.End

strtemp = Replace$(SourceDoc.FullName, "\", "\\")
strHlinkText = "file:///" & strtemp & " \s 1," & lngLinkStart & "," & lngLinkEnd & ",0,," & Selection.Text


NewDoc.ActiveWindow.Selection.Fields.Add _Range:=Selection.Range, _
Type:=wdFieldHyperlink, _
Text:=strHlinkText, _
preserveformatting:=True







I am of course assuming at this stage that I am only linking to body text
Will update as testing progresses but looks good so far.
Cheers
dansyoz

dansyoz
02-17-2010, 06:46 PM
A quick update for anyone who's interested.

The displayed text is the filename so I assume there's another parameter I have to add.

Ok so I have fixed this as the "Texttodisplay" attribute of the hyperlink is available after its created - but not during creation when using the fields.add approach. I've also cleaned the code up a bit.
So far my testing has been sound with the hyperlink taking me to exactly the right spot in the document.

Set rngActiveCell = .Rows(k + 1).Cells(2).Range
rngActiveCell.MoveEnd wdCharacter, -1 ' remove end of cell marker from range
strHlinkText = "file:///" & strtemp & " \s 1," & gResArray(4, k) & _
"," & gResArray(5, k) & ",0,," & gResArray(3, k)
.Cell(k + 1, 2).Range.Fields.Add Range:=rngActiveCell, _
Type:=wdFieldHyperlink, _
Text:=strHlinkText, _
preserveformatting:=True
NewDoc2.Hyperlinks(k).TextToDisplay = gResArray(3, k) ' this changes the text to the found word

A lot more stuff gets put into the table - this is just the hyperlink part

fumei
02-18-2010, 11:39 AM
The problem.
I am trying to include a hyperlink in column 1 (search text) which not only opens the document but moves directly to the exact location in the source document where this was found.

Are you saying your code above does this? Not only makes a hyperlink, but also points to a specific location with no bookmark?

dansyoz
02-18-2010, 04:55 PM
Fumei,
Yep thats correct - once I have finished testing I'll mark this as solved but so far seems to be working well.
I want to test more for links to footnotes, footers, etc but so far so good.

dansyoz
02-22-2010, 04:25 AM
Quick update - the offsets in the hyperlink appear to be relative to the story element in word. The first param does not appear to correlate to wdStoryType for example a hyperlink to an endnote is yielding 6 for this value which would correlate to the even page header not endnote (value 3) - will update as testing progresses

fumei
02-22-2010, 09:58 AM
Hmmmm. I can get it to put in a hyperlink to a document, but I can NOT get it to hyperlink to a specifc location in a document without a bookmark.

Please parse precisely...
strHlinkText = "file:///" & strtemp & " \s 1," & gResArray(4, k) & _
"," & gResArray(5, k) & ",0,," & gResArray(3, k)

HOW are you getting it to link to a specified location in a file, with no bookmark at that location?

dansyoz
02-22-2010, 10:36 PM
HOW are you getting it to link to a specified location in a file, with no bookmark at that location?
With the code I posted earlier - it works every time for me so far in my testing and I am generating some results files with my test data that have 60+ hyperlinks.

I am using Word 2007 on this particular computer but can generate the same results using source files originally created in word 2003. I have also tested with a totally new test file saved as 2003 format and 2007 format and both work the same way. Note - as previously mentioned the source file is opened read-only. I have checked the source file pre and post execution - no changes are apparent. Turning on show bookmarks indicates that no new bookmarks are created.


Please parse precisely...
strHlinkText = "file:///" & strtemp & " \s 1," & gResArray(4, k) & _
"," & gResArray(5, k) & ",0,," & gResArray(3, k)

Not sure what you mean here but

strHlinkText = a local (to sub) string variable.
file:/// (file:///) = a string value - obviously
strtemp = string var containing the full name of the source file we are linking to set using strtemp = SourceDoc.FullName.
\s 1, = a switch derived from results of a paste hyperlink - ie this mimics part of the content of a hyperlink using the "Paste-As-Hyperlink" menu function. Seems to refer to document part or type - still confirming.
An example (with field codes turned on) of a generated paste-as-hyperlink is {HYPERLINK "file:///C:\\Users\\dansyoz\\Documents\\Sourcetest1.docx" \s "1,497,504,0,,"}.
gResArray(4, k), = the start position returned by the original find is stored in an array and this refers to that value - equates to selection.start
gResArray(5, k), = the end position returned by the original find is stored in an array and this refers to that value equates to selection.end
0,, = Frankly not sure what this value is but believe it might provide a reference value relating to the offset values of start and end (see prior two params) . Still investigating this. The second part (after the first comma) is a missing param which I don't yet have a lead on and therefore leave empty.
gResArray(3, k), = the text I was searching for with the find is stored in an array - had hoped it would provide a label but doesn't so hence my earlier post about changing the displaytext.Is this what you are looking for.

I have derived this approach by trying to mimic what Word does. I cannot find anything that actually documents these fields.

fumei
02-23-2010, 12:14 PM
Interesting.

1. I can not find a \s switch for a hyperlink field.

2. There is a \l switch for a "location". So if it make a hyperlink field with:

{ HYPERLINK "c:\zzz\Already.doc" \l "20" } I get no error. However, clicking the hyperlink simply opens the file at the beginning, and NOT at any location. I thought maybe 20 would mean 20 characters in (Using the Range?).

3. so gResArray(4,k), gResArray(5,k) and gResArray(3,k) are numbers...except for gResArray(3,k) which is -apaprently - test: "gResArray(3, k), = the text I was searching for"

Thus: {HYPERLINK "file:///C:\\Users\\dansyoz\\Documents\\Sourcetest1.docx" \s "1,497,504,0,,"}.

With 497 and 504 being a Range start and end, assumably.

OK. So I edited my hyperlink to:

{ HYPERLINK "c:\zzz\Already.doc" \s "1,20,22,0,," }

Again, no error. Again, clicking the hyperlink opens the file, but NOT to any specified location. It opens it at the start.

I can not duplicate what you say you have done.

JulieC
02-23-2010, 02:26 PM
I've only skimmed, but I did something similar recently that does insert hyperlinks properly for Word 2003. It isn't addressing your issue directly, but maybe it will give you another hyperlink creation path with more options. My head is hurting from the Word object model documentation dance too.

With some pseudocode and the "meat":

' Set the Selection however you do that, I use Selection.Find
' Link internet links without the bookmark (#)
' If your address is in variable ADDRESS
result = StrComp("http", ADDRESS, 4))
If 0 = result Then
ActiveDocument.Hyperlinks.Add Selection.Range, ADDRESS
Else
ActiveDocument.Hyperlinks.Add Selection.Range, "#", ADDRESS
End If

JulieC
02-23-2010, 02:29 PM
Never mind, it looks like you have a solution working already, cheers!

dansyoz
02-24-2010, 01:56 AM
Firstly thanks for the continuing interest in this issue.


1. I can not find a \s switch for a hyperlink field.

Where are you looking to "find" this switch. I can't find any documentation of these switches at all. As noted earlier in this thread I derived this by creating a hyperlink manually then working through the field codes. If I could point you to the documentation for the \s switch then this thread would have been quite a bit shorter.:)

A Paste-As-Hyperlink result looks slightly (but not materially) different to my generated result
Paste-As-Hyperlink = {HYPERLINK "C:\\Users\\David\\Documents\\myfile.doc (file://\\Users\\David\\Documents\\myfile.doc)" \s "1,7047,7054,0,,"}
My VBA generated result = {HYPERLINK "file:///C:\\Users\\David\\Documents\\myfile.doc (file:///C:\\Users\\David\\Documents\\myfile.doc)" \s "1,7047,7054,0,,"}
As noted earlier in the thread the switch seems to use charcter offset values (I am using Selection.Start and Selection.End - ref posts 9 and 10 in this thread) from some document element. I am using 1 as a hard coded value for the first param as this seems to always work but am trying to find out how this changes depending on where the link is within the document (ie. different sections, stories etc).
See attached screen dumps (with field codes turned on) showing the links word itself generates with Paste-As-Hyperlink which include this switch.



2. There is a \l switch for a "location". So if it make a hyperlink field with:

This switch appears to relate to a hyperlink reference e.g._Hlk254771620 rather than a location per se. Word adds this on some occasions - haven't pursued the conditions for when it does this yet.When it does this however the format of the field is
Paste-As-Hyperlink = {HYPERLINK "myfile.doc" (file://users//David//Documents//myfile.doc) \l "_Hlk254771620" \s "1,7047,7054,0,,"}


I can not duplicate what you say you have done.
A touch of healthy scepticism here? :) It works repeatedly and consistently for me. I have no add-ins or special utilities loaded - just plain old word. I'm not a newbie - been coding off and on in multiple languages for over 25 years - but I can always learn something new - and have already from this forum.
I too wish I knew why this works more precisely - maybe there is something weird happening. I had run a screen cam type capture to actually show you that they link to the exact location but the resulting file is much too large to upload.

dansyoz
02-24-2010, 02:16 AM
As to - Where are you looking to "find" this switch. I can't find any documentation of these switches at all.

found some documentation on switches which DOES NOT list the \s
Need to refine my searching technique on google and within the Microsoft site - no idea why this didn't come up before!!:doh:

See http://office.microsoft.comm/en-au/word/HP051895441033.aspx
Doesn't solve the mystery of the structure of the \s switch but its something.
Oh and Fumei you were correct \l is (accroding to the Redmond crew) indeed a location. This switch only appears in my results when I have been testing manually. Never appears when I am using the VBA method with my read-only source file.

dansyoz
02-24-2010, 06:44 AM
Just stumbled via google on someone else who has been down a similar path.
Seems to concur in respect of params - still doing my own testing.
Ref - http://www.generation-nt.com/us/answer/what-format-word-hyperlink-uses-range-help-36332682.html

fumei
02-24-2010, 10:59 AM
Must be something with 2007. I can not duplicate this.




Bummer.

dansyoz
02-24-2010, 08:53 PM
Update for those that are interested.

Been doing some further testing and exploration.
First Dave at generation-nt (see earlier post) was on the right track. I'll reiterate what he found and what I have confirmed -




The first numeric parameter seems to be
1 = Located in Document Body including list items table cells etc
2 = Located in header or footer
3 = Located in a footnote
6 = Located in an endnote
7 = Located in a comment




However I have not yet been able to reconcile this value with any of words enumerated constants so unsure where this comes from.

The second and third numeric parameters correlate to selection.start and selection.end and appear to be relative to some other as yet unidentified document element.

The fourth element correlates (indirectly) to an enumerated constant WdBuiltinStyle. It is derived by adding one and negating it. For example the value of WdBuiltinStyle for a comment is wdStyleCommentText and equates to -31. Add one to this makes it -30 and negating it makes it 30 - this has proved true for all elements tested so far.
Still no handle on the last param.

But most interestingly of all I just explored the XML schema (2003 and 2007) for word and came across a reference to arblocation. This confirms that what I am doing is supported in Office ie hyperlink without bookmark although as it indicates this approach is not intended for programmatic use.:*)

From http://msdn.microsoft.com/en-us/library/aa172760(office.11).aspx

"arbLocation - stringType (http://msdn.microsoft.com/en-us/library/aa214315(office.11).aspx) optional - Tracks locations in documents that have no bookmark targets. Used internally by Word."

fumei
02-25-2010, 09:43 AM
Ah, I use Word 2002.

SamT
02-26-2010, 06:55 PM
I found a reference to these hyperlink switches/field codes.

The only /s switch reference I found indicated that the field should be formatted with a named style, i.e. /s=stylename
stupid cut and paste...can't remove text formatting.:banghead:

\l

Specifies a location in the file, such as a bookmark, where this hyperlink will jump.
\m

Appends coordinates to a hyperlink for a server-side image map (server-side image map: A graphic containing sensitive regions, or "hot spots," that a user can click to follow a hyperlink. A server-side image map requires a script on a Web server that identifies the sensitive regions and their corresponding hyperlinks.) (javascript:AppendPopup(this,'ofServerSideImageMap_5')).
\n

Causes the destination site to be opened in a new window.
\o

Specifies the ScreenTip (ScreenTip: A short description that appears when the user holds the mouse pointer over an object, such as a button or hyperlink.) (javascript:AppendPopup(this,'IDH_ofdefScreenTips_6')) text for the hyperlink.
\t

Specifies the target that the link should be redirected into. Use this switch to link from a frames page to a page that you want to appear outside of the frames page. For example: { Hyperlink "filename" \t "_top" }
The destination Web page will appear in the entire browser window instead of a frame.
Options for the \t switch are:
\t "_top"

Whole page \t "_self"

Same frame \t "_blank"

New window \t "_parent"

Parent frame The default (without the switch specified) is Page Default (none).

dansyoz
02-28-2010, 12:20 AM
Sam - yep have covered this in earlier post - see http://office.microsoft.comm/en-au/word/HP051895441033.aspx
The \s parameter is generated by Word (in 2007) via Paste-As-Hyperlink.

Per my last post it appears that this parameter equates to "arblocation" which is used internally by word.

Thanks