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
 
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
 
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
 
Hi JRB-Bldr

Thank you for your valued post.

I'll try that your suggestion out and post back

Thank you

Steve
 
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
 
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")

[b]****** Line added from Mike Lewis' post

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

******[/b]

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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top