How do I get the Moniker Word uses for a Link?

R

Robin Tucker

Hi,

I am embedding links, via. VBA (well, via. VB.NET, through VBA!) into Word.
I know where my OLE object is, I want to embed a link from it to some text.
When I "record a macro", "pasting special", the resulting field code looks
something like this:

{ LINK OurCompany.InfraredImage "Document3.doc" "_1158083192\\Spot 1" \a
\t }

I can identify all of these things except the "_1158083192", which is
presumably the moniker Microsoft Word uses to identify the object being
linked to.

Can anyone tell me how I get this number into my program, so I can embed a
link via. code? This only works at present through copy/paste special. I
cannot find the moniker anywhere in the OLEFormat property of the
InlineShape. Is there an interface I should use on the OLE object that will
give me this number?

Thanks



Robin
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message, < Robin Tucker > wrote:

|| Hi,
||
|| I am embedding links, via. VBA (well, via. VB.NET, through VBA!) into
Word.
|| I know where my OLE object is, I want to embed a link from it to some
text.
|| When I "record a macro", "pasting special", the resulting field code
looks
|| something like this:
||
|| { LINK OurCompany.InfraredImage "Document3.doc" "_1158083192\\Spot 1" \a
|| \t }
||
|| I can identify all of these things except the "_1158083192", which is
|| presumably the moniker Microsoft Word uses to identify the object being
|| linked to.
||

This looks like a hidden bookmark Word created when it inserts the hyperlink
from one document to another (or to another location in the same document)

Depending on the logic regulating the hyperlink destination, it is possible
to use code to first insert a Bookmark at the destination, and then use that
bookmark to build the LINK field code at the source.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

Ok guys, here is the thing. I'm not looking to link to a document, or part
of another document (well I am, but not an office document). The link I
gave was from an item in an OleItemCollection object, which is implemented
by an OLE object which I have embedded in my document.

The ole object comes from the InlineShape.OLEFormat.Object item. The item I
want to link to comes from this object (it is itself an OLE object). In the
OleItemCollection, the object is called "Spot 1". In Microsoft Word, the
object has a moniker prefix of _1158083192. This number is the one I'm
after. Given an InlineShape, how do I find this part of the Moniker?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message said:
Ok guys, here is the thing. I'm not looking to link to a document, or part
of another document (well I am, but not an office document). The link I
gave was from an item in an OleItemCollection object, which is implemented
by an OLE object which I have embedded in my document.

The ole object comes from the InlineShape.OLEFormat.Object item. The item I
want to link to comes from this object (it is itself an OLE object). In the
OleItemCollection, the object is called "Spot 1". In Microsoft Word, the
object has a moniker prefix of _1158083192. This number is the one I'm
after. Given an InlineShape, how do I find this part of the Moniker?

Sorry for being so thick... but I still do not understand what is your goal
here.
You wrote that the target of the link is not an Office document, yet, the
LINK field you posted shows that you are linking to a Word document
(Document3.doc). I am still confused as to what you are actually trying to
do.

Normally, in a LINK field, as in:

{LINK Excel.Sheet.8 "Test.xls" "Total!L20C1:L30C1" \a \p}
(Linking to part of spreadsheet)

LINK
Type of field
Excel.Sheet.8
Class of OLE link
"Test.xls"
Source file for Link
"Total!L20C1:L30C1"
Location in Source file
\a \p
Field switches

Or, linking to part of a Word document:
{LINK Word.Document.8 "Test.doc" "OLE_LINK1" \a \p}

When creating the link, Word created the bookamrk called "OLE_LINK1" in the
target document.

Now, you get:
{LINK OurCompany.InfraredImage "Document3.doc" "_1158083192\\Spot 1" \a \t}

OurCompany.InfraredImage
A class I am not familiar with.
"Document3.doc"
Normally, when you are linking to a Word document,
the class is "Word.Document.8"
"_1158083192\\Spot 1"
Why is the location in the Word document not of the
type "OLE_LINK1"?
But I would bet that "_1158083192 is a hidden bookmark
in your target document (Document3.doc)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

Ok, the target of the link is an object within an InlineShape within a word
document :)

Consider the following:

Document1.Doc contains an Embedded OLE object of the class MyCompany.MyShape

MyCompany.MyShape implements IOleItemContainer, and I am able to fetch an
OLE Object called "Spot 1" from the IOleItemContainer it implements.

Now, when I activate MyCompany.MyShape in Document1, I can Copy and Paste
Special..., a link from "Spot 1" into the document. So, whenever Spot 1
changes in my activated MyCompany.MyShape, its value is updated in the link
(this works at present using Copy/Paste).

I know I can embed a link in my document using the Link field. However, I
have no way of getting the Moniker for the link from Word. The Moniker I
found when recording the Copy/Paste into a Macro looked like this:
"Document3.doc" "_1158083192\\Spot 1". Ie. the object is in Document3.Doc
(which happens to be the same document I want to embed the link into), the
object is _1158083192 and the sub object within that object is "Spot 1". I
know about Document3.Doc and I know about "Spot 1", but in order to create
the link, I need the Moniker Word gives the embedded object (ie. the
"_1158083192").

However, a colleague writes to me:

"I think we can get the buggers by getting the IOleObject interface for the
embedded image and calling GetMoniker. However, I recall there being all
sorts of problems, such as Word stupidly creating a brand new moniker each
time GetMoniker is called. And I have just noticed that you can
copy/paste-link a tool twice, then it stops working. It is a quagmire.
Perhaps we can report all the bugs and get MS to fix it in Orifice 2007..."

:)
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message said:
Ok, the target of the link is an object within an InlineShape within a word
document :)

Consider the following:

Document1.Doc contains an Embedded OLE object of the class MyCompany.MyShape

MyCompany.MyShape implements IOleItemContainer, and I am able to fetch an
OLE Object called "Spot 1" from the IOleItemContainer it implements.

Now, when I activate MyCompany.MyShape in Document1, I can Copy and Paste
Special..., a link from "Spot 1" into the document. So, whenever Spot 1
changes in my activated MyCompany.MyShape, its value is updated in the link
(this works at present using Copy/Paste).

I know I can embed a link in my document using the Link field. However, I
have no way of getting the Moniker for the link from Word. The Moniker I
found when recording the Copy/Paste into a Macro looked like this:
"Document3.doc" "_1158083192\\Spot 1". Ie. the object is in Document3.Doc
(which happens to be the same document I want to embed the link into), the
object is _1158083192 and the sub object within that object is "Spot 1". I
know about Document3.Doc and I know about "Spot 1", but in order to create
the link, I need the Moniker Word gives the embedded object (ie. the
"_1158083192").

You got the "_1158083192" when you used Paste Special, right?

You know that Word created the "_1158083192" and built the LINK field codes
using that.

So, have you tried assigning a bookmark to the InLine shape yourself and
then create the LINK field in your code (Not by using Paste Special, but by
adding a Field of the Type LINK, or by adding an empty field and adding in
the field codes). This way you will use your own bookmark, not the one Word
randomly creates when the Paste Special is done. And since you know the
object name (Spot!, right?) it should be easy to build your own LINK field
from scratch.

I have never tried to build Link fields that way, so there may be other
catches, but I think it is worth trying.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

Nooooo. That won't work. The bookmark is a position in the document! If I
do that, then I'll still need the Moniker for the Embedded object either
way. I know where the item is in the document (I have it in the
InlineShapes collection after all!).

Apparently, Word doesn't do this very well (in fact, it sucks at doing it).
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message said:
Nooooo. That won't work. The bookmark is a position in the document! If I
do that, then I'll still need the Moniker for the Embedded object either
way. I know where the item is in the document (I have it in the
InlineShapes collection after all!).

Are you sure that "_1158083192" is not a bookmark?
It really looks like one!
Usually, when Word gives name to objects, like AutoShapes, it uses a naming
system like:
"Rectangle1 or "Shape1", etc.
Whereas names like "_1158083192" are usually bookmarks...
But I have never worked with links pointing to sub-object in embedded inline
shapes...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

One moment. I will assume its a bookmark, in which case it should be in the
bookmarks collection of the document, right? In fact, I just did a test and
my bookmark collection count is zero. However it looks, Word is definately
not treating it as a bookmark internally. Its a bit of an enigma :)
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message said:
One moment. I will assume its a bookmark, in which case it should be in the
bookmarks collection of the document, right? In fact, I just did a test and
my bookmark collection count is zero. However it looks, Word is definately
not treating it as a bookmark internally. Its a bit of an enigma :)

It is a *hidden* bookmark.
Bookmark names cannot start with a digit and bookmark names that start with
an underscore are hidden bookmarks. If you do not display them, the bookmark
collection returns 0 (unless, of course, there are other regular bookmarks).

Try this to check your bookmark collection count:

'_______________________________________
ActiveDocument.Bookmarks.ShowHidden = True
MsgBox ActiveDocument.Bookmarks.Count
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

There it is!

We are close to cracking this, but still there is a problem. There are 3
bookmarks in the InlineShape range, only one of which provides the correct
Moniker (in this case, the last one). Why word has 3 hidden bookmarks here
is a mystery. All of the bookmarks have the same range {29..29}. The
inline shape has the range {0..32}.



Thanks.
 
R

Robin Tucker

Thought we'd cracked it, but it turns out not to be the case. Word ADDS the
bookmark to the shape when it adds the link. So I'm back at square zero. I
don't have any bookmarks in my inline shape until after the link has been
added, but I cannot add the link unless I know the bookmark name. Weird no?
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Robin Tucker > écrivait :
In this message said:
Thought we'd cracked it, but it turns out not to be the case. Word ADDS the
bookmark to the shape when it adds the link. So I'm back at square zero. I
don't have any bookmarks in my inline shape until after the link has been
added, but I cannot add the link unless I know the bookmark name. Weird no?

That's how Word works when creating all links (LINK, REF, etc.)

Go back to the beginning of this thread. See my first reply. Can you add the
bookmark yourself? How/When is the LINK destination created? If you have no
control over that, is there a way to determine the destination location by
applying some logic test to the document? Or is there a clue in the source
document that you can use to find the destination in the target document?

Or, are you systematically creating links for all the embedded objects in
the destination document?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
R

Robin Tucker

Ok, heres my solution. I tried adding the bookmark but it didn't seem to
work. What I'm going to do is call IOleObject::GetMoniker on the
InlineShape.OLEFormat.Object (after I've written a little wrapper for
IOleObject, which .NET doesn't seem to provide). Then, I can just add the
link with the Moniker name and presumably Word will know which object I am
referring to.
 
H

Howard Kaikow

Not a mystery.

Word adds hidden bookmarks all over the place any time you change certain
things.
A document can get quite dirtied with the critters, but deleting them is
precarious as you do not know what is referring to the bookmark from another
document.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top