Can I detect when a MacroButton text would span more than one line

A

Anna Neiderud

As suggested by the MVPS site I put my hyperlinks in macrobutton fields to be
able to get an event whenever the user clicks it. This is done
programmatically from an addin written in VB 6 and works fine. The only
problem is when the inserted hyperlink is too long. I then get the text
"DisplayText cannot span
more than one line!" and I understand this, but is there a way to detect the
condition either by testing the length before or detecting the error after it
has happened? I need to be able to tell my user what to do about it.
 
K

Klaus Linke

Hi Anna,

When you create the hyperlink, perhaps you can truncate the actual address in the .TextToDisplay ... say if the address is longer than a certain number of characters?

Klaus
 
A

Anna Neiderud

Thanks for your suggestion, but how would I know what number of characters to
use? I need to be sure, and since characters have varying width, and
different font sizes can be used, I cannot set a fixed number.

/Anna
 
K

Klaus Linke

If your page (or column) width isn't really small, wouldn't some guesstimate like 40 or 50 characters work?
If you want to exploit the full available space, or that space varies wildly because of different column widths or font sizes, you could probably add an error handler to the macro that inserts the hyperlink, and truncate the .TextToDisplay in the error handler (then reinsert the hyperlink) until the error disappears.
Seems like a lot of work though, which I'd probably avoid if possible ;-)

Regards,
Klaus
 
A

Anna Neiderud

The second approach with the errorhandler sounds like what I'm looking for,
unfortunately I don't seem to get an error. The only indication of the error
that I can see is the text displayed in the document and I can't see that
programmatically.

/Anna
 
J

Jean-Guy Marcil

Anna Neiderud was telling us:
Anna Neiderud nous racontait que :
The second approach with the errorhandler sounds like what I'm
looking for, unfortunately I don't seem to get an error. The only
indication of the error that I can see is the text displayed in the
document and I can't see that programmatically.

Just a quick thought here...
What if you put the Fields(1).Result in a string? Won't that string hold the
error code if the text is too long? ("DisplayText cannot span more than one
line!")

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

Klaus Linke

Hi Jean-Guy,
What if you put the Fields(1).Result in a string? Won't that
string hold the error code if the text is too long? ("DisplayText
cannot span more than one line!")

It should, but it doesn't seem to :-(

Hi Anna,

Should have tried...
You use something like { MacroButton mymacro { Hyperlink "http://www.some.org" }}, right?

Regards,
Klaus
 
J

Jonathan West

Anna Neiderud said:
As suggested by the MVPS site I put my hyperlinks in macrobutton fields to
be
able to get an event whenever the user clicks it. This is done
programmatically from an addin written in VB 6 and works fine. The only
problem is when the inserted hyperlink is too long. I then get the text
"DisplayText cannot span
more than one line!" and I understand this, but is there a way to detect
the
condition either by testing the length before or detecting the error after
it
has happened? I need to be able to tell my user what to do about it.

Hi Anna

If the text to be converted into a macrobutton field is currently selected,
then you can check whether the selection wraps to the next line like this

If Selection.End > ActiveDocument.Bookmarks("\Line").Range.End Then
'Selection is too long
Else
'Selection is ok
End If


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
A

Anna Neiderud

You use something like { MacroButton mymacro { Hyperlink
"http://www.some.org" }}, right?

Yes, thats what I am using, and the hyperlink is inserted using
rng.Hyperlinks.Add(rng, url) rather than using TextToDisplay (since it needs
to contain symbols in some cases). Therefore Jonathan West's suggestion below
sounds promissing. I'll try that.

Thanks for the discussion.

/Anna
 
A

Anna Neiderud

Thanks!

It didn't make the code look nice (since I didn't have the text selected and
had to select it and than select the old selection again) but it works :)

/Anna
 
A

Anna Neiderud

And if someone else will be using this technique I need to add that you need
to put the text first on a line first, then do the check, and then move it
back as desired. Otherwise you might get undesired results since a hyperlink
in running text can wrap also when it is not as long as a line and then it
would still work in a macrobutton field.

Here is some code in case someone wants it (perhaps overly complex for
general use, but due to the rest of my application I need it to look this
way):

' insertionRange contains the text in question and is part of
' the range macroButtonField.Code i.e. { macrobutton macro <insertionRange> }
Private Sub InsertLinkInMacroButtonField(insertionRange As Range,
macroButtonField As Field, address As String)
Dim oldSelection As Range
Dim hl As Word.Hyperlink
Dim hlRange As Range
Dim rng As Range
Dim rngToRemove As Range
Set oldSelection = Selection.Range.Duplicate

' Cut and paste insertion Range to a new fresh line, check if it is
wrapping and
' if so truncate and copy back
Set rng = GetFieldRange(macroButtonField)
rng.Collapse wdCollapseStart
rng.insertBefore vbCr & vbCr
Set rngToRemove = rng.Duplicate
rng.Collapse wdCollapseStart
rng.MoveStart wdCharacter, 1
insertionRange.Cut
rng.Paste

rng.Select
Dim truncated As Boolean
Dim rngToTruncate As Range
truncated = False
While Selection.End > evDoc.Bookmarks("\Line").Range.End And
Len(rng.text) >= 1
'Selection is too long
Set rngToTruncate = rng.Duplicate
rngToTruncate.Start = rngToTruncate.End - 1
rngToTruncate.text = ""
truncated = True
Wend
If truncated And (Len(rng.text) >= 3) Then
Set rngToTruncate = rng.Duplicate
rngToTruncate.Start = rngToTruncate.End - 3
rngToTruncate.text = "..."
rng.End = rngToTruncate.End
End If

' cut and paste rng back, insert hyperlink, remove extra characters at
range
rng.Cut
insertionRange.Paste
rngToRemove.text = ""
insertionRange.Hyperlinks.Add insertionRange, address

oldSelection.Select
end sub

Public Function GetFieldRange(f As Field) As Range
Dim rng As Range
Set rng = f.Code.Duplicate
rng.TextRetrievalMode.IncludeFieldCodes = True
rng.TextRetrievalMode.IncludeHiddenText = True
rng.Start = rng.Start - 1
rng.End = rng.End + 1
Set GetFieldRange = rng
End Function
 

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