INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Word automation - How to underline text

Word automation - How to underline text

(OP)
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
oRange.InsertAfter("Quote Ref: " + CHR(9) + alltrim(myfield))
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

RE: Word automation - How to underline text

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


RE: Word automation - How to underline text

(OP)
Hi JRB-Bldr

Quote:

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

RE: Word automation - How to underline text

Steve,

You are almost there. You already have:

oRange.Font.Name = "Arial"
oRange.Font.Size = 11


Now just add:

oRange.Font.Underline = 1


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

RE: Word automation - How to underline text

(OP)
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()
oRange.Font.Underline = 1
oRange.InsertAfter("The line of text I need underlined")
oRange.Font.Underline = 0
oRange.InsertParagraphAfter()
Etc.... 

Thank you

Steve

RE: Word automation - How to underline text

Quote (Steve-vfp9user)

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

RE: Word automation - How to underline text

(OP)
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

RE: Word automation - How to underline text

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

Tamar

RE: Word automation - How to underline text

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

RE: Word automation - How to underline text

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

RE: Word automation - How to underline text

Quote (Mike)

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


RE: Word automation - How to underline text

(OP)
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

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

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

RE: Word automation - How to underline text

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.

RE: Word automation - How to underline text

(OP)
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:

Quote:

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()
oRange.Font.Name = "Arial"
oRange.Font.Size = 11
oRange.Font.Underline = 1

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

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

Thank you

Steve

RE: Word automation - How to underline text

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

RE: Word automation - How to underline text

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.

RE: Word automation - How to underline text

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.

RE: Word automation - How to underline text

(OP)
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

RE: Word automation - How to underline text

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

RE: Word automation - How to underline text

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

RE: Word automation - How to underline text

(OP)
Thank you Mike

Having viewed your profile (and your website) you are clearly someone who has great knowledge of VFP and I also like, and appreciate, your responses which are constructive.

Thank you

Steve

RE: Word automation - How to underline text

How about you try something so that you can actually 'see' what your Automation code is doing line-by-line.

CODE -->

oWord = Createobject("Word.Application")
oDocument = oWord.Documents.Open(cDocFile)
oWord.Visible = .T.

SET STEP ON 

That should Open the Word document so that you can now see where your cursor is, where it is going, what it highlights, and what else occurs.
And the SET STEP ON will Force your code to suspend and open the trace window so that you can then single step through your Automation code watching its results on the oRange object.

First I'll admit that I am much better at Excel Automation than Word Automation.

I don't know how you are intending to locate the Word text you want to underline.
Are you trying to move the cursor or searching for a specific text string.

If searching for a text string, you can use something like:

CODE -->

* --- Find text 'Setup' ---
orange = oDocument.range(0,0)
orange.Find.Text = "Setup" 

Or you can use something like:

CODE -->

xLine = "Get Camera-1, Camera-2"
oRange.InsertAfter( xLine) 

Either way with Word now visible you can confirm that you found and Selected what you want.

Now you want to Underline what you have:

CODE -->

* --- Define Underline Color (Black) ---
oRange.Font.UnderlineColor = 0  && wdColorBlack
* --- Underline with Singe Line ---
oRange.Font.Underline = 1  && wdUnderlineSingle 

Try things like that and see where it gets you.
Again, I just now did all of this while not really knowing what I was doing, so hopefully you can do something similar and get things working.

Good Luck,
JRB-Bldr




Now in your VFP code

RE: Word automation - How to underline text

(OP)
Hi JRB-Bldr

Thank you for your valued post.

I'll try that your suggestion out and post back

Thank you

Steve

RE: Word automation - How to underline text

Steve,

I've just tested this, and it seems to work:

Immediately after inserting "Description of proposed works", do this:

CODE -->

oSentence = oRange.Sentences(oRange.Sentences.Count)
oSentence.Font.Underline = 1 

Remove all other code that sets or unsets the underlining.

My thinking is that oSentence will contain a reference to the range of the last sentence, that is, the one you just added. That range is quite separate from oRange. It is the oSentence range that you need to underline - for the reasons I stated earlier.

Note that oSentence is singular, but Sentences (as in oRange.Sentences) is plural.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Word automation - How to underline text

(OP)
Hey Mike Lewis

Star for you. The line of text to be underlined is now working.

Below, I have posted the whole code set up in an effort to assist others, which as mentioned previously, is what I believed this forum is all about.

I would also like to say thank you to you Mike for your replies which in my eyes are directed to those who have a basic knowledge and understanding but just might not have the expertise of others. We are not all experts frm the off so thanks again, I appreciate it.

The mqlongquote is from a table field to identify the reference e.g. ABCD-1234-2017 so we have a unique document linked to the relevant record.

The default.doc is a template with the business header and footer so we have the same template each time but with the relevant fields taken from the table record.

Here's the code:

CODE -->

COPY FILE SYS(5)+SYS(2003)+"\longquotes\default.doc" TO ;
  SYS(5)+SYS(2003)+"\longquotes\"+ ;
  ALLTRIM(mqlongquote)+"\"+mqlongquote+'.doc'

LOCAL oDocument, oRange

oWord = Createobject("Word.Application")

oDocument = oWord.Documents.Open(SYS(5)+SYS(2003)+"\longquotes\"+ ;
  ALLTRIM(mqlongquote)+"\"+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(add01))+ ;
  ", "+ALLTRIM(PROPER(sitename))+" "+ ALLTRIM(PROPER(add02)))
oRange.InsertParagraphAfter()
oRange.InsertAfter(CHR(9) + CHR(9)+ ALLTRIM(PROPER(add03))+ ;
  ", "+ALLTRIM(PROPER(add04))+" "+ALLTRIM(UPPER(pcode)))
oRange.InsertParagraphAfter()
oRange.InsertAfter("Subject: " +CHR(9) + ALLTRIM(LEFT(UPPER(mqsubject),1))+ ;
  ALLTRIM(RIGHT(LOWER(mqsubject),LEN(mqsubject)-1)))

oRange.InsertParagraphAfter()
oRange.InsertParagraphAfter()

oRange.InsertAfter("Description of proposed works")

****** Line added from Mike Lewis' post

oSentence = oRange.Sentences(oRange.Sentences.Count)
oSentence.Font.Underline = 1

******

oRange.InsertParagraphAfter()
oRange.InsertParagraphAfter()

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

In addition to the above, I also added the following after the document was created, saved and closed only because it worked and it is also used to open the document within another part of the program.

Here's that code:

CODE -->

DECLARE INTEGER ShellExecute IN shell32.dll ;
  INTEGER hhdWin, ;
  STRING cAction, ;
  STRING cFileName, ;
  STRING cParams, ;
  STRING cDir, ;
  INTEGER nShowWin

lcFileName = mqlongquote+'.doc'

lcPath = SYS(5)+SYS(2003)+"\longquotes\"+ ;
  ALLTRIM(mqlongquote)+"\"

ShellExecute(0, "open", lcFilename, "", lcPath, 1) 

Thanks again to those who gave advice and in particular Mike Lewis

Just a reminder from me, I'm Steve Williams aka Steve-vfp9user

Thank you

Steve

RE: Word automation - How to underline text

Steve,

Delighted to hear that you have got it working. And thanks for posting your final code. As you say, that's helpful for others who might have similar problems or who just want to learn how these things are done.

I admit that I had a bit of a mental block with this problem, but it was good to help figure it out.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close