Hi macropod,
I ran into a problem with the sample code. In a coulpe of cases the
documents were protected, so I get an error when executing AutoFormat. It
looks like I have no choice but to search for the @ sign, but I'm having a
hard time reproducing the quality results I've been getting with your sample
code.
When I find the @ sign I don't know how to retrieve the whole email address.
Too bad, I really like your logic but is there a way around this?
Thanks in advance.
Ray
"macropod" wrote:
> Hi Ray,
>
> Well, if your documents might have email addresses that are just plain text formatted to look like hyperlinks, as was your problem
> one), then you may need another strategy to find them. Obviously, testing for the existence of the hyperlink filed and/or the
> 'mailto' string isn't going to work in all cases. Maybe you'll need to construct a test based on the presence of the @ symbol in a
> word in the document, then test whether the word (after removing 'mailto:' if it's there) is a valid email address string. There's
> code available on the internet for doing that, but searching though the whole document for @ symbols is going to be a lot slower
> than simply testing fields.
>
> Cheers
> --
> macropod
> [MVP - Microsoft Word]
> -------------------------
>
> "Ray C"
> > Hi macropod,
> >
> > You're right, your code does work with headers. I realized it later on when
> > I processed all my documents in the folder. The problem with one particular
> > document was that the email address was in the header but it wasn't
> > translated into a hyperlink by Word. Even when the code executes the
> > Autoformat, it doesn't look like Word converts it into a hyperlink or a
> > field. When I ALT-F9, nothing happens. However, the email address was typed
> > correctly (i.e. name@domain.com).
> >
> > Since it isn't recognized as a hyperlink ,there is no "mailto".
> > Very strange.
> > All I can tell you is that the email is in the header, it is right-justified
> > (at the end of the line).
> >
> > Ray
> >
> >
> > "macropod" wrote:
> >
> >> Hi Ray,
> >>
> >> Are you sure the the email address is in the header of the problem document really is a field, and not just some text that's been
> >> formatted to look like a hyperlink?
> >>
> >> The code I posted certainly retrieves the addresses in headers and footers if the email addresses are correctly formed. The only
> >> other possibility I can think of is that the 'mailto:' might be malformed (eg 'mailto;' ' mailto:' or 'mailto :')
> >> You could nail all three malformations by changing:
> >> If Left(oFld.Result, 7) = "mailto:" Then MsgBox oFld.Result
> >> to
> >> If Left(Trim(oFld.Result), 6) = "mailto" Then MsgBox oFld.Result
> >> or
> >> If Instr(oFld.Result,"mailto") <> 0 Then MsgBox oFld.Result
> >> neither of these alternatives is quite as reliable as the original, though, being more liable to return false matches. Plus you'd
> >> have to figure out how to handle the malformed addresses in your subsequent processing.
> >>
> >> Cheers
> >> --
> >> macropod
> >> [MVP - Microsoft Word]
> >> -------------------------
> >>
> >> "Ray C"
> >> > Hi macropod,
> >> >
> >> > Thanks for the enormous help you've given me. The code sample you gave me
> >> > works on all documents except one. In this document the email address is in
> >> > the header of the document. The code doesn't catch it.
> >> >
> >> > I tried doing ALT-F9 and the email address does not change or decode itself.
> >> > I tried placing a Range.AutoFormat inside the Do loop but Word complained
> >> > that you cannot autoformat a header.
> >> >
> >> > Is there a way for Word to catch that field?
> >> >
> >> > Ray
> >> >
> >> > "macropod" wrote:
> >> >
> >> >> Hi Ray,
> >> >>
> >> >> To access the hyperlinks wherever they might be, including headers/footers & shapes, try:
> >> >> Sub HLinkTest()
> >> >> Dim oFld As Field
> >> >> Dim oRange As Word.Range
> >> >> With ActiveDocument
> >> >> .Range.AutoFormat
> >> >> For Each oRange In .StoryRanges
> >> >> Do
> >> >> For Each oFld In oRange.Fields
> >> >> If oFld.Type = wdFieldHyperlink Then
> >> >> If Left(oFld.Result, 7) = "mailto:" Then MsgBox oFld.Result
> >> >> End If
> >> >> Next oFld
> >> >> Set oRange = oRange.NextStoryRange
> >> >> Loop Until oRange Is Nothing
> >> >> Next oRange
> >> >> End With
> >> >> End Sub
> >> >>
> >> >> Cheers
> >> >> --
> >> >> macropod
> >> >> [MVP - Microsoft Word]
> >> >> -------------------------
> >> >>
> >> >> "Ray C"
> >> >> > Hi macropod,
> >> >> >
> >> >> > It's working like a charm...however, my old code was able to also search in
> >> >> > the headers and footers of the document, your example doesn't seem to be
> >> >> > doing this.
> >> >> >
> >> >> > Thanks
> >> >> > Ray
> >> >> >
> >> >> > "macropod" wrote:
> >> >> >
> >> >> >> Hi Ray,
> >> >> >>
> >> >> >> I'm not surprised that either of those fields failed - they're not valid hyperlinks. You can see that by the "" in the
> >> >> >> field,
> >> >> >> which
> >> >> >> basically says the hyperlink is nothing. If you were to manually create a hyperlink field with that code and no alternate
> >> >> >> display
> >> >> >> text, Word would return an error message on the page.
> >> >> >>
> >> >> >> You can use the following code to get around that:
> >> >> >> Sub HLinkTest()
> >> >> >> Dim oFld As Field
> >> >> >> With ActiveDocument
> >> >> >> .Range.AutoFormat
> >> >> >> For Each oFld In .Fields
> >> >> >> If oFld.Type = wdFieldHyperlink Then
> >> >> >> If Left(oFld.Result, 7) = "mailto:" Then MsgBox oFld.Result
> >> >> >> End If
> >> >> >> Next
> >> >> >> End With
> >> >> >> End Sub
> >> >> >>
> >> >> >> Cheers
> >> >> >> --
> >> >> >> macropod
> >> >> >> [MVP - Microsoft Word]
> >> >> >> -------------------------
> >> >> >>
> >> >> >> "Ray C"
> >> >> >> > Hi macropod,
> >> >> >> >
> >> >> >> > When I press ALT-F9
> >> >> >> > For the first hyperlink I have:
> >> >> >> >
> >> >> >> > {HYPERLINK "" \o
> >> >> >> > "outbind://115-00000000C02DC307E8EC334F91A9B8DEBCE1EBBD44CF2800/"}
> >> >> >> >
> >> >> >> > and for the second hyperlink I have:
> >> >> >> >
> >> >> >> > { HYPERLINK "" \o
> >> >> >> > "outbind://115-00000000C02DC307E8EC334F91A9B8DEBCE1EBBD44CF2800/" }
> >> >> >> >
> >> >> >> >
> >> >> >> > Yes, with all the spaces (weird).
> >> >> >> >
> >> >> >> > When I toggle out of ALT-F9, the email addresses are actually valid.
> >> >> >> > I get the error message when I reach the line:
> >> >> >> > If Left(HLink.Address, 7) = "mailto:" Then MsgBox HLink.Address
> >> >> >> >
> >> >> >> > It can't read the Address property of the HLink because it claims that HLink
> >> >> >> > was deleted.
> >> >> >> >
> >> >> >> > Ray
> >> >> >> >
> >> >> >> > "macropod" wrote:
> >> >> >> >
> >> >> >> >> Hi Ray,
> >> >> >> >>
> >> >> >> >> This code works for me:
> >> >> >> >> Sub HLinkTest()
> >> >> >> >> Dim HLink As Hyperlink
> >> >> >> >> With ActiveDocument
> >> >> >> >> .Range.AutoFormat
> >> >> >> >> For Each HLink In .Hyperlinks
> >> >> >> >> If Left(HLink.Address, 7) = "mailto:" Then MsgBox HLink.Address
> >> >> >> >> Next
> >> >> >> >> End With
> >> >> >> >> End Sub
> >> >> >> >> and I'm unable to reproduce either of the error messages you mentioned - even if I change the hyperlink address to
> >> >> >> >> "outbind:\\1000510063444" or "mailto:outbind:\\1000510063444".
> >> >> >> >>
> >> >> >> >> With your hyperlinks, what do you see when you press Alt-F9? This exposes the field coding and you should see something
> >> >> >> >> like:
> >> >> >> >> {HYPERLINK "mailto:here@there.com"}. Note that the display text is not necessarily the hyperlink address.
> >> >> >> >>
> >> >> >> >> Cheers
> >> >> >> >> --
> >> >> >> >> macropod
> >> >> >> >> [MVP - Microsoft Word]
> >> >> >> >> -------------------------
> >> >> >> >>
> >> >> >> >> "Ray C"
> >> >> >> >> >I already tried that and I still get the same error. The problem is that the
> >> >> >> >> > HLink instance seems deleted when it starts executing the first line inside
> >> >> >> >> > the For loop. One thing I noticed is that when I click on the hyperlinks
> >> >> >> >> > inside the Word document itself, nothing happens. When I look at the address
> >> >> >> >> > of the hyperlink (using Word, not VBA) the address has strange characters. It
> >> >> >> >> > has "outbind:\\1000510063444"
> >> >> >> >> >
> >> >> >> >> > But Word still has the email address as a hyperlink and the text does show
> >> >> >> >> > name@domain.com
> >> >> >> >> >
> >> >> >> >> > Ray
> >> >> >> >> >
> >> >> >> >> >
> >> >> >> >> > "macropod" wrote:
> >> >> >> >> >
> >> >> >> >> >> Hi Ray,
> >> >> >> >> >>
> >> >> >> >> >> How about:
> >> >> >> >> >> Dim HLink As Hyperlink
> >> >> >> >> >> objDocument.Range.AutoFormat
> >> >> >> >> >> For Each HLink In objDocument.Hyperlinks
> >> >> >> >> >> If Left(HLink.Address, 7) = "mailto:" Then
> >> >> >> >> >> ....
> >> >> >> >> >> End If
> >> >> >> >> >> ....
> >> >> >> >> >> Next
> >> >> >> >> >>
> >> >> >> >> >> Cheers
> >> >> >> >> >> --
> >> >> >> >> >> macropod
> >> >> >> >> >> [MVP - Microsoft Word]
> >> >> >> >> >> -------------------------
> >> >> >> >> >>
> >> >> >> >> >> "Ray C"
> >> >> >> >> >> > I'm writing a Word VBA function that process hyperlinks within word
> >> >> >> >> >> > documents. Since Work can also convert web sites into hyperlinks as well, I
> >> >> >> >> >> > check that the Hyperlink.Address property begins with "mailto" to make sure I
> >> >> >> >> >> > only process email addresses.
> >> >> >> >> >> >
> >> >> >> >> >> > Here's a sample of my code:
> >> >> >> >> >> >
> >> >> >> >> >> > objDocument.Range.AutoFormat
> >> >> >> >> >> > For Each HLink In objDocument.Hyperlinks
> >> >> >> >> >> > E_Mail = Split(HLink.Address, ":", 1)
> >> >> >> >> >> > If E_Mail(0) = "mailto" Then
> >> >> >> >> >> > ....
> >> >> >> >> >> > Endif
> >> >> >> >> >> > ......
> >> >> >> >> >> > Endif
> >> >> >> >> >> >
> >> >> >> >> >> > The problem is that I get an error on the third line, telling me that
> >> >> >> >> >> > Hyperlink has been deleted. Yet I clearly have two email addresses in the
> >> >> >> >> >> > document. In other cases I get a different error message saying that the
> >> >> >> >> >> > Address method of object Hyperlink failed (caused by the same line, the third
> >> >> >> >> >> > line).
> >> >> >> >> >> >
> >> >> >> >> >> > When I step code during debugging, I get 2 for objDocument.Hyperlinks.Count
> >> >> >> >> >> > which is what I'm supposed to get since there are 2 hyperlinks in the
> >> >> >> >> >> > document.
> >> >> >> >> >> >
> >> >> >> >> >> > Can anyone shed some light on this?
> >> >> >> >> >> >
> >> >> >> >> >> > Thanks in advance
> >> >> >> >> >> >
> >> >> >> >> >> > Ray
> >> >> >> >> >> >
> >> >> >> >> >>
> >> >> >> >>
> >> >> >> >>
> >> >> >>
> >> >> >>
> >> >>
> >> >>
> >>
> >>
>
>