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!

Format Columns in WORD 2

Status
Not open for further replies.

gogins

IS-IT--Management
Sep 1, 2001
17
GB
Dear All,

I have a dbf table containing texts and numerics of various length. Using Word Autonation, I am trying to insert these values into a document. I do not however succeed in obtaining neat columns in the document. Tab-padding does not help. I have tried to use a table in the document and this works ok but is VERY slow!
Is there another way of doing this?
Many thanks.
Gogins











 
Here is code I use that creates a table for me and I insert data into the columns. If you don't understand any of this just email me and I will explain any part.

oWord = CREATEOBJECT("Word.Application")

oWord.Visible = .T.
oDoc = oWord.Documents.Add()
* Get Document in Print View
oDoc.ActiveWindow.View.Type = wdPrintView

oRange = oDoc.Range()

* Base Body Style
oBodyStyle = oDoc.Styles.Add("Body", wdStyleTypeParagraph)
WITH oBodyStyle
.BaseStyle = oDoc.Styles[wdStyleNormal]
WITH .Font
.Name = "Times New Roman"
.Size = 14
.Bold = .T.
ENDWITH

WITH .ParagraphFormat
.Alignment = wdAlignParagraphLeft
.SpaceAfter = 0
ENDWITH
ENDWITH

* Put Header in document -- Leave This Out of Pages 2 - 4 -- WITH oDoc.Sections[1].Headers[wdHeaderFooterPrimary]
oRange=.Range()
WITH oRange
.Text = "GRAND ENTRY"
.Style = oDoc.Styles["Heading 1"]
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Shading.BackGroundPatternColorIndex = wdGray25
ENDWITH
ENDWITH

oRange = oDoc.Range()

WITH oDoc
.pagesetup.leftmargin = 36
.pagesetup.rightmargin = 36
.pagesetup.topmargin = 43
.pagesetup.bottommargin = 43
ENDWITH

WITH oRange
.Style = oSpec
.InsertAfter(sponsorflag+cr)
* Leave Some Blank Space
.Collapse(wdCollapseEnd)
.End = oRange.End+1
.Style = oEvent
.InsertAfter(cbr)
ENDWITH

* Put Table in
oRange.Collapse(wdCollapseEnd)
oTable = oWord.ActiveDocument.Tables.Add(oRange,2,8)

WITH oTable
* Remove Borders
.Borders.InsideLineStyle = .F.
.Borders.OutsideLineStyle = .F.
* Set Column Widths
.Columns(1).SetWidth(160,0)
.Columns(2).SetWidth(38,0)
.Columns(3).SetWidth(38,0)
.Columns(4).SetWidth(38,0)
.Columns(5).SetWidth(160,0)
.Columns(6).SetWidth(38,0)
.Columns(7).SetWidth(38,0)
.Columns(8).SetWidth(38,0)
* Shade the First Row
.Rows(1).Shading.Texture = 300
* Put Heading Info in
.Cell[1,1].Range.InsertAfter("#### Name")
.Cell[1,2].Range.InsertAfter("Prf 1")
.Cell[1,3].Range.InsertAfter("Prf 2")
.Cell[1,4].Range.InsertAfter("Score")
.Cell[1,5].Range.InsertAfter("#### Name")
.Cell[1,6].Range.InsertAfter("Prf 1")
.Cell[1,7].Range.InsertAfter("Prf 2")
.Cell[1,8].Range.InsertAfter("Score")
ENDWITH

* Set Font of Table
oWord.ActiveDocument.Tables(1).Range.Font.Size = 7
oWord.ActiveDocument.Tables(1).Range.Font.Name = "Arial"
oWord.ActiveDocument.Tables(1).Range.Font.Italic = .F.
oWord.ActiveDocument.Tables(1).Range.Font.Bold = .T.
 
If you don't understand any of this just email me and I will explain any part.

Please avoid suggesting taking solution into the private. The idea of this forum is for every one to benefit the solutions proposed.





Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
I'm sorry I was not trying to do anything wrong. I posted quite a bit of code and I just wanted to answer and questions that might arise. I will do it on the forum.
 
Buffie - Many thanks for your response. It certainly
plugged a few holes in MY knowleedge. I would be also
interested in a non-table solution: how can one arrange
neat columns without the use of a table.

I am not sure how Mike's comment fits into this public thread, but it is quite obvious that we can learn here more than just about VFP.

Many thanks again,
Gogins.
 
* Put Heading Info in
.Cell[1,1].Range.InsertAfter("#### Name")
.Cell[1,2].Range.InsertAfter("Prf 1")
.Cell[1,3].Range.InsertAfter("Prf 2")
.Cell[1,4].Range.InsertAfter("Score")
.Cell[1,5].Range.InsertAfter("#### Name")
.Cell[1,6].Range.InsertAfter("Prf 1")
.Cell[1,7].Range.InsertAfter("Prf 2")
.Cell[1,8].Range.InsertAfter("Score")

Note in the above code I am inserting just text and not any table info. These could just as well be variables that had some value....
 
I may not have answered your question quite right. You DO NOT see the tables so no ones knows if it is a table or not. I just found that making data line up and look good is so much easier with a table.
 
Buffie thanks.

A bit stuck with the values of the constants included in
your code, and also with the ospec in .Style = oSpec
Can you help?

Many thanks, Gogins.
 
Some of the constants I defined in the program at the top like.

#define swchp "Championship Package Sponsor(s): "+ALLTRIM(grp.cswchp)

#define wdPrintView 3

Others are all the constants used in msword9.h which is a header file for Microsoft Word that has all of Word's constants defined. I found it on the internet. Then you include it in your program like this.

#include msword9.h

The oSpec is another special formatting that I defined in my program that I just did not copy and send it looks like this:

* Special Events
oSpec = oDoc.Styles.Add("Special", wdStyleTypeParagraph)
WITH oSpec
.BaseStyle = oBodyStyle
.Font.Italic = .T.

WITH .ParagraphFormat
.SpaceAfter = 4
ENDWITH
ENDWITH

You can define your own formatting like the above. Makes it Sweet when you want certain fonts for certain type of paragraphs or words or whatever.
 
As Buffie said, tables are the best way to line things up in Word. However, sending table data over one cell at a time can be slow.

One alternative is to build the data you want up as a single string with tabs between "columns" and returns between "rows." Send that string to Word, capture it in a Range variable and then use Word's ConvertToTable method to turn the tabbed stuff into a table.

I don't have code for this at hand, but I have done it this way in the past. If you get stuck, post another message.

Tamar
 

Tamar hello.

Thanks very much for your response. I have spent so many hours on this that I am now compelled to continue with the cell-by-cell approach (which I can sufficiently control) and study the ConvertToTable method at a later time.

UNLESS... somebody can help me by showing me how to use the (Page 96 in MS Automation with VFP) TextColumn object and in particualr how to control the oRange.InsertBreak(8) horizontal(!) column break line.

Many thanks again.
 
Tamar hello (2)

Following your hint I have found which gives good examples of mass conversion of any dbf to Word table. The example which uses ConvertToTable, works particularly well. Thank you so much for pointing this possibility out to me.

I hope you wouldn’t mind, but could you still help me further with the TextColumns and InsertBreak(8) I mentioned in my last message? The possibility of bolding/shading individual rows within the table is very attractive.
 
You can also achieve bold using something like the following. Where Bold is part of the font definition.

* Base Body Style
oBodyStyle = oDoc.Styles.Add("Body", wdStyleTypeParagraph)
WITH oBodyStyle
.BaseStyle = oDoc.Styles[wdStyleNormal]
WITH .Font
.Name = "Times New Roman"
.Size = 14
.Bold = .T.
ENDWITH

WITH .ParagraphFormat
.Alignment = wdAlignParagraphLeft
.SpaceAfter = 0
ENDWITH
ENDWITH

How much data is going into Word? Tamar make a very good point about the speed. I have used the 'convert to table' also and I am looking for that code but I can not put my fingers on it. It was a quite awhile ago.
 
The TextColumns stuff on Page 96 is really for what are normally called newspaper columns, that is, columns where you fill one, and then it goes to the top of the next column. They give you a lot less control than tables. Based on your original description of your problem, I don't think this is what you need.

Once you convert the text to a table, you can do whatever formatting you want on the table. The ConvertToTable method returns a Table object, so grab that object and then format it as you need. Look at pages 86 and forward for details on formatting a table.

Tamar
 
Tamar, Buffie
Thank you both so much for your really useful comments
which have directly helped me to sort things out here and
added a great deal to my understanding of the topic. Well
deserved Stars for your extensive responses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top