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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word automation - How to underline text 2

Status
Not open for further replies.

Steve-vfp9user

Programmer
Feb 5, 2013
337
GB
Hi all

I have created a good word automation process for part of my app. Here is some of the code:

Code:
oRange = oDocument.Range()
oRange.Font.Name = "Arial"
oRange.Font.Size = 11
[b]oRange.InsertAfter("Quote Ref: " + CHR(9) + alltrim(myfield))[/b]
oRange.InsertParagraphAfter()
Etc.....

My question is, how can I underline one line of text (Maybe the one shown above in bold).

I have searched the forum but can't find the answer.

Any guidance would be appreciated.

WIndows 10, VFP 9 with SP2

Thank you

Steve
 
The best way to find out what the VFP Word Automation needs to do is to begin without VFP at all.

1) Go directly into Word.
2) Set up to Record a Macro
3) Perform your intended task (in this case: select the desired text and then underline it)
4) Stop the Macro recording
5) Edit the Macro so as to look over its VBA code.

By examining that VBA code you should be able to understand (despite the different language) what your VFP Word Automation code needs to do.

Give it a try and see if that doesn't give you what you need.

Good Luck,
JRB-Bldr


 
Hi JRB-Bldr

The best way to find out what the VFP Word Automation needs to do is to begin without VFP at all.

Well I think that defeats the object as the code I have written with the help of some other forum users does exactly what it says on the tin so to speak.

Using Tamar's helpful examples, this has been most beneficial to our requirements.

The only thing I am unable to find is the line of code to underline one line in our Word document which is automatically created from VFP coding.

I have no requirement at this time to use VBA code within Word but thank you for the post.

Thank you

Steve
 
Steve,

You are almost there. You already have:

[tt]oRange.Font.Name = "Arial"
oRange.Font.Size = 11[/tt]

Now just add:
[tt]
oRange.Font.Underline = 1[/tt]

The value 1 gives normal (single) underlining. 3 would give double underlining. There are many other possibilities.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike

Thank you for your response.

I added the line and yes, the text became underlined however, all the lines created on the document were underlined whereas I only require the one line.

I tried the below with and without the line of code (on the assumption 0 turns off the underline) but that didn't work either:

Code:
Etc....
oRange.InsertParagraphAfter()
[b]oRange.Font.Underline = 1[/b]
oRange.InsertAfter("The line of text I need underlined")
[b]oRange.Font.Underline = 0[/b]
oRange.InsertParagraphAfter()
Etc....

Thank you

Steve
 
Steve-vfp9user said:
I have no requirement at this time to use VBA code within Word but thank you for the post.

Obviously you did not understand what I was suggesting.
NO you will not use the VBA code, but by examining it within the Macro you will see how it accomplishes what you need.

THEN go into your VFP code, where you can create the VFP Word Automation code to duplicate the functionality. Quite often it looks very similar to the VBA code.

Good Luck,
JRB-Bldr
 
Hi JRB-Bldr

I'm not familiar at all with what your suggesting but thank you for the explanation which is appreciated.

Thank you

Steve
 
Try moving the reset of the underline property to after your insert the new paragraph.

Tamar
 
Steve,

I think the problem is that the underlining applies to the entire range. By inserting the new paragraph, you are extending that range and therefore the scope of the underlining. If you set to .Font.Underline back to 0 after your .InsertParagraphAfter(), that should fix it.

EDIT: I just noticed that Tamar has replied with the same suggestion. Sorry. I didn't mean to duplicate her reply.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
On the subject of macro recording, I agree that this is very often a useful way of discovering the objects and PEMs that you need to know, but I would treat it with caution. Often, the code generated by macro recording is quite different from you might write yourself. For example, I've noticed that macros generally use Selection objects where I would use a Range.

There's nothing wrong with this. But it does mean that you can't always do a straight conversion from VBA to VFP.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike said:
it does mean that you can't always do a straight conversion from VBA to VFP.

Absolutely, but for those numerous times when I didn't have a clue where to start, it has helped me time and time again to get headed in the right direction.

Steve-vfp9user - Good Luck,
JRB-Bldr


 
I am still having a problem with the underlining of just one line. Here is what I have:

Code:
LOCAL oDocument, oRange

oWord = Createobject("Word.Application")
oDocument = oWord.Documents.Open(SYS(5)+SYS(2003)+"\longquotes\"+mqlongquote+'.doc')

oRange = oDocument.Range()
oRange.Font.Name = "Arial"
oRange.Font.Size = 11

oRange.InsertAfter("Quote Ref: " + CHR(9) + qlongquote)
oRange.InsertParagraphAfter()
oRange.InsertAfter("Date: " + CHR(9) + CHR(9)+ DTOC(quotedate))
oRange.InsertParagraphAfter()
oRange.InsertAfter("Client: " + CHR(9) + CHR(9)+ qpropcust)
oRange.InsertParagraphAfter()
oRange.InsertAfter("Site: " + CHR(9) + CHR(9)+ ALLTRIM(PROPER(sadd01))+ ;
  ", "+ALLTRIM(PROPER(sitename)))
oRange.InsertParagraphAfter()
oRange.InsertAfter(CHR(9) + CHR(9)+ ALLTRIM(PROPER(msadd02))+", "+ ;
  ALLTRIM(PROPER(msadd03))+" "+ALLTRIM(UPPER(mspcode)))
oRange.InsertParagraphAfter()
oRange.InsertAfter("Subject: " +CHR(9) + ALLTRIM(LEFT(UPPER(qsubject),1))+ ;
  ALLTRIM(RIGHT(LOWER(qsubject),LEN(qsubject)-1)))
oRange.InsertParagraphAfter()
oRange.InsertParagraphAfter()

oRange.Font.Underline = 1

[b]oRange.InsertAfter("Description of proposed works")  && THIS IS THE LINE I WANT UNDERLINED[/b]

oRange.Font.Underline = 0

oRange.InsertParagraphAfter()
oRange.InsertParagraphAfter()

oWord.ActiveDocument.Save()
oWord.ActiveDocument.Close()

As mentioned, I have tried putting the line oRange.Font.Underline = 1 in different places but either all text or no text are underlined.

JRB-Bldr, thank you for you post

Thank you

Steve
 
As far as a glimpse over your code tells me, you once set oRange = oDocument.Range() and then never change that? That makes the whole document the range you modify. And you expect the underline to limit on one line instead of the whole document? Think again.

Bye, Olaf.
 
Ok Olaf

Firstly, I am trying my best here and your response comes across as quite brutal. We're not all experts you know.

I'm not asking for the answer, I'm looking for some quidance.

So you say:

As far as a glimpse over your code tells me, you once set oRange = oDocument.Range() and then never change that? That makes the whole document the range you modify. And you expect the underline to limit on one line instead of the whole document? Think again.

I added the following to try that but I'm still not getting that single line to underline.

Code:
Etc....
oRange.InsertParagraphAfter()
oRange.InsertParagraphAfter()

oRange = oDocument.Range()
[b]oRange.Font.Name = "Arial"
oRange.Font.Size = 11
oRange.Font.Underline = 1[/b]

oRange.InsertAfter("Description of proposed works")
Etc....

No need for you to respond, I'll look elsewhere.

Thank you

Steve
 
Steve,

I understand what is going wrong, but I'm not sure how well I can explain it.

The problem is that oRange refers to the entire document. That's because you initially set it to oDocument.Range(). Each time you insert new text, or a new paragraph mark, the range is extended, so it still covers the whole document. When you set the range's underline property to 1, that setting will apply to the whole document. Setting it back to zero will remove the underlining from the whole document.

What you want to do is to get a new range object, covering only the sentence ""Description of proposed works". Note that that will be a different variable (e.g. oRange1 rather than oRange). You then set the Font.Underline of that new range (oRange1) to 1. And that's all. There should be no need to set it back to zero.

So the question is: how do you create that new range? That's where my knowledge is lacking. There should be a way of starting a new range at the insertion point, and ending it at another arbitrary point, but I can't think how to do that. (Tamar, if you see this, please help.)

If all else fails, you could loop through the document's Sentences collection, looking for a sentence that contains the specified text. Or perhaps execute the Find method to find the text. Either way, that should give you the new range, which you can then underline as explained above.

I'm sorry if this is not much help. But at least I hope it will help you understand what is going on.

Final word. If Tamar (or anyone else) contradicts what I have just written, the chances are that they are right and I am wrong.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
It isn't harsh at all. I just point out you naturally act on the whole document, by the mere meaning of the natural english word range. And once you're pointed to that you can find out what a range is in Word and how you can get at smaller ranges than the whole document.

The only reason I don't chink out "the code" necessary is simply not knowing what you need exactly, so you need to dig deeper on your own here a bit more. Your code already targets a smaller type of ranges, pragraphs.

Start up the Word help and look into it's VBA Object Reference. This object model stays the same, whether you program it Word internally via VBA basic or in VFP automation.

There is no such range type as "Lines". Word has many collections, paragraphs, sentences, words.

Is that really harsh? You're blaming me of an offense I never made, I just ponit out the most important misunderstanding, that alone should help. Besides, when I say I took a glimpse at your code, it's likely indicating I'm short of time spending on this. I'm also not the only expert here and others can get the idea what to address. Okay?

Bye, Olaf.

 
Let me just say that I don't think Mike's response was brutal at all since it's something you should be able to see yourself. However, I also know how easy it is to overlook the obvious, especially when it's right in front of you, so please let us all calm down.

Unfortunately, since you "wear a mask" by not using your real name, you are not qualified to get any assistance from me.
 
Tore, my real name is Steve Williams. I have no problem with disclosing that.

In any case, I am happy not to receive any assistance from you.

Mike Lewis: Thank you for your response which makes a lot more sense and at least gives me a pointer as to where to look next for which I greatly appreciate.

When I have a resolution I will post back as I am aware that this HELPS others (which is what I thought this forum was all about).

Steve Williams, that's me.... Steve

Thank you

Steve
 
Steve, yes please do post your solution if you manage to find one. As you say, that will help other people who have the same problem. (It will also help me fill in my own knowledge gaps.)

By the way, whatever other forum members might say, I for one am perfectly happy to help people even if I don't know their real name. That said, I do like your new display name. It's much nicer to be able to address someone as Steve rather than vfp9user.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
But going back to the question ....

Maybe looping through the Sentences collection might be the way to go.

Something like this perhaps:

Code:
FOR EACH oSentence IN oRange.Sentences
  IF oSentence.Text = "Description of proposed works"
    oSentence.Font.Underline = 1
    EXIT
  ENDIF
ENDFOR

This is just off the top of my head. I don't know if it will work, but it at least gives you something to work on.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top