Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word: Draw a line via vba 1

Status
Not open for further replies.

Anthony047

Technical User
Jul 28, 2008
16
IT
Hello to everybody,
I need to draw a "line" over some text of a Word document; unfortunately using the "strike" effect of the text font is not feasible due to the presence of "subscript" characters.
After selecting the text, I tryed reading the coordinates of the text with:
ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, Selection.Range
and then draw the line with:
ActiveDocument.Shapes.AddLine(pLeft , pTop, pLeft +pWidht, pTop).Select
Unfortunately the first instruction returns the coordinates with respect to top/left corner of the physical screen, wereas the second one requires the coordinates with respect to the document window (the window normally white in the application window).
And I am even not sure that the measurement units is the same for both the instructions..

Can you suggest me a viable path? Thank.
Anthony
 
Hi Anthony,

Here's one way to overline a selected string, using an EQ (equation) field:
Code:
Sub Overline()
Dim StrTxt As String
ActiveWindow.View.ShowFieldCodes = True
With Selection
  StrTxt = .Text
  .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="EQ \s\up6(\f(," & StrTxt & "))"
  .MoveLeft wdCharacter, 2
  .Delete
  .Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub
The '6' in the "EQ \s\up6(\f(," expression is measured in points and controls the overline height.

Note: The field contents must fit on one line. If there's a word-wrap in the selected range, the field will force all of it onto the one line and, if the string spans more than one line, the field will display 'Error!'.

Cheers

[MS MVP - Word]
 
Thank you macropod for your message, it showed me a useful path.
However at the completion of the macro the result is the word "Error" in my document, at the position of the Equation; no any ru-time error. But I was able to enter manually a fraction (Menu /Insert /Field, equation, then used the equation editor); big achievement for me!
When running the macro, before executing ActiveWindow.View.ShowFieldCodes = False, the equation is displayed as follows: /.EQ.\s\up6(\f(,My Word))\ (slash and final backslash is used for the equation brackets that I don't know how to type, and "dot" is used as a "space").
I tryed removing the "(" in "\up6(" as well adding something before ",My Word)", by slightly changing the parametres in the .Field.Add instruction.
I have to add several equations, your further help in removing the Error would be greately appreciated as that would save me time and errors in entering manually the fractions.
I use Word Xp (2002) in Italian.

Thanks again.
 
Two side questions:
-when I enter manually a fraction, "space" is not allowed in the terms, so I can type "MyWord" but not "My Word"! no any bypass?
-how can I edit the fraction? I can only select it as a whole.

Thank you.
 
Try this:
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False, Text:="EQ \s\up6(\f([!];[/!]" & StrTxt & "))"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
YES, it DID help, even though I had to add a "space" before the ";" (otherwise the selected text doesn't show as the denominator of the fraction).
I assume using the semicolumn is related to the Italian version of Word.

Thanks to both of you.
 
Hi Anthony,

OK, with the Italian version, you may need to change the ',' in:
EQ \s\up6(\f(," & StrTxt & "))"
to '.' or ';' - check what Word's Help file says for the EQ field.

If you're trying to enter a fraction, you could code the macro along the lines of:
Code:
Sub InsertFraction()
Dim Numerator As String, Denominator As String
ActiveWindow.View.ShowFieldCodes = True
Numerator = InputBox("Please input the Numerator")
Denominator = InputBox("Please input the Denominator")
With Selection
  .Collapse (wdCollapseStart)
  .Font.Size = Round(.Font.Size) / 2
  .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="EQ \f(" & Numerator & "," & Denominator & ")"
  .MoveLeft wdCharacter, 2
  .Delete
  .Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub
This will insert the fraction, positioning it at the first character of the selected range, and giving its characters a point size roughly half that of the point size of the font in use at the insertion point. Again, change the "," to whatever the Italian EQ field specifications require.

Cheers

[MS MVP - Word]
 
The right separator for my word version is ";" as both "," and "." results in "Error".
Macropod's macro is very useful, I shall create a modified version to be used on existing documents to reformat the existing text alternatives (like "I agree / I don't agree"); I think I shall set markers around the alternates text (for example "#I agree / I don't agree#") then create a Do/Loop, search the first marker [CCount = Selection.MoveEndUntil(Cset:="#")], get the Numerator (before the "/") and the Denominator (before the second "#"), execute the kernel of the macro, Looop.

All these things were witchery only yesterday morning.

Thanks again,
Anthony

Anthony047 (GMT+1)
 
Hi Anthony,

To reformat a selected string with a '/' separator, try:
Code:
Sub String2Fract()
Dim Numerator As String, Denominator As String
ActiveWindow.View.ShowFieldCodes = True
With Selection
  Numerator = Trim(Split(Selection, "/")(0))
  Denominator = Trim(Split(Selection, "/")(1))
  .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="EQ \f(" & Numerator & ";" & Denominator & ")"
  .MoveLeft wdCharacter, 2
  .Delete
  .Fields.Update
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub
Cheers

[MS MVP - Word]
 
You give me too much, but every byte or kbyte is welcome!

Btw, and no longer related to the problem that your code brilliantly resolved, how can I get the X-Y coordinates of a selection, given that "GetPoint" returns useless information?
(as per my first post, ActiveWindow.GetPoint pLeft, pTop, pWidth, pHeight, Selection.Range returns "absolute" values related to the top/left corner of the screen).
But this is now just a curiosity.

Many thanks.



Anthony047 (GMT+1)
 
Hi Anthony,

Try:
Code:
Sub GetCoordinates()
MsgBox "Your selection's page coordinates (in points) are: " & vbCrLf & _
  "Horizontal- " & Selection.Information(wdHorizontalPositionRelativeToPage) & vbCrLf & _
  "Vertical- " & Selection.Information(wdVerticalPositionRelativeToPage)
End Sub
Cheers

[MS MVP - Word]
 
For a moment I was doubtfull about "negative coordinates" (eg Horizontal- 92.5) then I realized it was part of the label! :)))

It works perfectly, I think we may consider resolved the case(s).
Thanks again, a very useful forum!


Anthony047 (GMT+1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top