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

Encase / Line up text from memo field in word document

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB
Hello

I am using the code below (some of which has been edited / removed) to create a WORD document and add some records to it from a table.

Code:
#DEFINE vfpCR CHR(13)
#DEFINE vfpTAB CHR(9)
#DEFINE wdSeparateByTabs 1
#DEFINE wdTableFormatColorful2 9
#DEFINE wdAlignParagraphLeft 0

LOCAL loWord, loDoc, loRange, lcTable, lcField, i

* Create the document

loWord = CREATEOBJECT("Word.Application")
loWord.Visible = .T.

loDoc = loWord.Documents.Add()

WITH loWord
  .ActiveDocument.PageSetup.Orientation=0
  .selection.PageSetup.TopMargin = 21.5
  .selection.Font.Size=18
  .selection.Font.Bold=.T.
  .selection.ParagraphFormat.Alignment=1 && CENTER ALIGN
  .selection.typetext("MY TITLE HEADER"+vfpCR)
ENDWITH

* Add the records to the document

USE MYTABLE ORDER MYORDER

SCAN

  WITH loWord
    IF SHOWTHIS="Y"
      .selection.typetext(vfpTAB+ ;
        "Item created: "+TIMECREATE+" on "+ ;
        dtoc(DATECREATE)+ ;
        " by "+alltrim(proper(CREATOR))+vfpTAB+vfpCR)
      .selection.typetext(vfpTAB+"Type: "+ ;
        alltrim(DESCTYPE)+vfpCR)
      .selection.typetext(vfpTAB+"Area: "+ ;
        alltrim(AREA)+vfpCR)
    ELSE
      .selection.Font.Bold=.T.
      .selection.typetext(vfpTAB+ ;
        "No match in this area"+vfpCR)
      .selection.Font.Bold=.F.
    ENDIF

*  Other code similar to above here... (edited)

    IF NOT EMPTY(INFOTEXT)
      .selection.typetext(vfpTAB+"Information: "+ ;
        alltrim(infotext)+vfpCR)
      ENDIF
    IF NOT EMPTY(ACTIONTEXT)
      .selection.typetext(vfpTAB+"Action required: "+ ;
        alltrim(actiontext)+vfpCR)
    ENDIF
  ENDWITH

* Now add a row of - to show end of record

  WITH loWord
    .selection.ParagraphFormat.Alignment=1 && CENTER ALIGN
    .selection.typetext(REPLICATE(CHR(45),142)+vfpCR)
    .selection.ParagraphFormat.Alignment=0 &&	LEFT ALIGN
  ENDWITH
	
ENDSCAN

The fields INFOTEXT and ACTIONTEXT are both MEMO fields. When they are shown on the word document they start off with an indent as shown below but the second line of the MEMO field (which more often than not, is more than one line) starts more to the left than in line with the first line (I hope that makes sense).

The layout looks something like this when the word document opens (but is restricted here because of the way Tek-Tips displays it). The first four lines are tabbed over, as is the first row of the MEMO field but subsequent lines from the MEMO are not tabbed

1. An area somewhere
Time created: 10:06 on 16/08/2012 by Jeff Smith
Description of item
United Kingdom

Text in memo field associated with this record, Text in memo field associated with this record, Text in memo field associated with this record, Text in memo field associated with this record, Text in memo field associated with this record, Text in memo field associated with this record...

My question is two fold:

1. How do I get the memo field to line up with the words "An", "Time", "Description" etc?
2. If possible, can I put the MEMO fields in a table cell (if that is a more efficient process)?

I am aware of how to create a table with cells:

Code:
oTable=oWord.ActiveDocument.Tables.Add(oRange,1,1)  &&  Change oRange for more rows/columns

I am also aware of setting border lines (add or remove them):

Code:
.Borders.InsideLineStyle=.T.

So if 2 is more efficient than 1, I can add a table/cell and remove the border and perhaps encase the memo field inside the cell to make it line up and be "Tabbed over" for the full contents of the MEMO fields as it should be.

Any advice would be appreciated

Thank you

 
You can set tab positions in word, eg for numerations, so that only the number is left and any text belonging to the same item number is indented.

You have to see if merely setting that helps, you may need to change your memo field contents accordingly. In general Word works with "body text", I don't know the term, text with no line breaks. We'd call it flowtext. A word numbered list would rather do a new item and number, if a line break is pasted in.

To set a tab position, the Word macro recorder records (shortened to interesting parts only);

With Selection.ParagraphFormat
.SpaceBeforeAuto = False
.SpaceAfterAuto = False
.FirstLineIndent = CentimetersToPoints(2)
End With

This is the tab setting you want 0, as you don't want the first line indented more than the rest of the text.

There are many more tab types, eg the makro recorder reveals:

With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
.NumberFormat = "%1."
.TrailingCharacter = wdTrailingTab
.NumberStyle = wdListNumberStyleArabic
.NumberPosition = CentimetersToPoints(0.63)
.Alignment = wdListLevelAlignLeft
.TextPosition = CentimetersToPoints(1.27)
...
Endwith

So here you set the position for the numbers of the items and the text indentation. We'd now have to find out how you get from oWord or oDocument to the "ListGalleries".

Also this ListGallery and ListTemplate needs to be applied, What I find in the macro recorder is:
Selection.Range.ListFormat.ApplyListTemplateWithLevel ...

At this point I have to say, you may have it easier using tables and cells to format text. Or ask about Word Marko programming in a Word forum or look into Hentzenwerke Books about Office/Word Automation.

One other general tip: Rather use RTF and RangeObj.Paste() will help you paste that into Word with all the sufficient style/format options RTF offers, which also includes tab positions. That would be a solution for the long run. It's a bit fumbling to get this to work. Another hint: Use Wordpad to format text as wanted, save as RTF and open with file extension changed to txt in notepad and you'll easily see RTF for a certain format, which also Word 2010 will be able to understand, if using Paste(). Te fumbling is about parting the RTF into pieces you really need or which are rather one time only document header tags. Tab positions are one of those head infos, once set work for the whole document.

Bye, Olaf.
 
Anther, much easier and pragmatic idea: In a word template define ruler(s) and tabs in them as needed, via Word itself, not via Automation.

Then instead of Documents.Add() call Documents.Add("d:\yourtemplates\ruler.doc") and you have ruler and it's tabs predefined. Then tabs in text will position it accordingly.

Bye, Olaf.
 
Hi Olaf

Thank you for your post.

The actual layout of the word document is fine for the exception of the issue with the memo field. As you know, it's quite difficult to display that giving an example on this forum due to the way it shows.

The tabs are ok, it's just the memo fields that are causing a slight issue.

Hentzenwerke Books

Yes, my thanks to Tamar which is where I used some of the code above to help me in producing what I have achieved so far.


I will look into the command "ListGalleries" a bit further and see if that can shed any further light on the subject.

I am think here though if I can get a table cell to start at a certain tab point (vfpTAB) and populate that fixed cell with the MEMO field, theoretically it should work (I think!)

Thanks again
 
Think we posted at the same Olaf so I missed the entry about rulers.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top