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

Word Automation works when oWord.visible = .t., but not with .f.

Status
Not open for further replies.

ontsjc

Technical User
May 17, 2000
113
Hello,

I have a pretty simple method that takes some rich text entered in an rtf control on a form and pastes it into a Word table. The method builds the Word table from scratch each time and when I'm debugging the method I've set the visible property to .T. so I can watch what's going on. Then when it is working, I set the visible property to .f. The method builds the table and populates the header row no problem, but then doesn't populate any of the table cells. Not sure what is going on here, any suggestions would be greatly appreciated.
 
Can you post the code?
I have no problems with Word and mostly it is set Visible = .f.


Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Here's the whole thing as ineligent as it is:

LPARAMETERS cDocName

cFile = SYS(5)+CURDIR()+"Temp.doc"
oWord = createobject("word.application")
oword.documents.open(cFile)
oWord.visible = .F.
WITH oWord
.Selection.Font.Size = 12
.Selection.Font.Name = 'Times New Roman'
.ActiveDocument.PageSetup.Orientation = 1
.Selection.ParagraphFormat.Alignment = 1
.ActiveDocument.Tables.Add(oWord.Selection.Range,1,14,1,1)
*head off accross the top and do the headers
FOR x = 1 TO 14
cName = 'column'+ALLTRIM(STR(x,2))
cName = 'thisform.grid1.'+cName+'.header1.Caption'
cName = &cName
cCaption = ALLTRIM(STRTRAN(cName,'_',''))
.Selection.TypeText(cCaption)
IF x < 14
.Selection.MoveRight(12)
ENDIF
ENDFOR
.Selection.SelectRow
.Selection.ParagraphFormat.Alignment = 1 &&Center Justified
.Selection.Font.Bold = 9999998
.Selection.Rows.HeadingFormat = 9999998 &&Repeats first row on different pages
endwith
SELECT comments
count
nNumberRows = _tally
nNumberRows = nNumberRows * 4
oWord.Selection.InsertRowsBelow(nNumberRows)
oWord.Selection.ParagraphFormat.Alignment = 0 &&Left Justified
oWord.Selection.Font.Bold = 9999998
nInsert = nNumberRows
oWord.Selection.Rows.AllowBreakAcrossPages = .F.

GOTO top
DO WHILE !EOF()
WITH oWord
.Selection.MoveUp(5,1)
.Selection.MoveDown(5,1)
.Selection.SelectRow
.Selection.ParagraphFormat.Alignment = 1
*Fill in the contents of the grid
FOR x = 1 TO 14
cField = FIELD(x)
IF INLIST(LOWER(cField),'date_enter','date_resp','dateincorp')
cText = DTOC(&cField)
ELSE
cText = ALLTRIM(&cField)
ENDIF
IF x = 1
.Selection.MoveRight(12)
.Selection.Font.Bold = 9999998
.Selection.TypeText(cText)
.selection.font.bold = 9999998
ELSE
.Selection.TypeText(cText)
endif
.Selection.MoveRight(12)
ENDFOR
.Selection.MoveDown(5,3)
skip
ENDWITH
ENDDO

x = 1
y = 4
WITH oWord
.Selection.MoveUp(5,nNumberRows)
DO WHILE y <= nNumberRows
.Selection.MoveDown(5,1)
.Selection.SelectRow
.Selection.Cells.Merge
cRowText = 'Row '+ALLTRIM(STR(x,5))
.Selection.TypeText(cRowText)
x = x+1
.Selection.MoveDown(5,1)
.Selection.SelectRow
.Selection.Cells.Merge
cRowText = 'Row '+ALLTRIM(STR(x,5))
*!* .Selection.TypeText(cRowText)
x = x+1
.Selection.MoveDown(5,1)
.Selection.SelectRow
.Selection.Cells.Merge
cRowText = 'Row '+ALLTRIM(STR(x,5))
*!* .Selection.TypeText(cRowText)
.Selection.MoveDown(5,1)
y = y+4
x = x+1
ENDDO
ENDWITH
x = 1
GOTO top
SCAN

cFindTxt = "Row "+ALLTRIM(STR(x,5))
*Now fill in the RTF fields
With oWord.Selection.Find
.Text = cFindTxt
.Forward = .T.
.Wrap = 1
.Format = .F.
.MatchCase = .F.
.MatchWholeWord = .T.
EndWith
oWord.Selection.Find.Execute
WITH oWord
.Selection.Font.Bold = 9999998
.Selection.TypeText('Original Comment:')
.Selection.Font.Bold = 9999998
.Selection.TypeParagraph
.Selection.TypeParagraph
cText =ALLTRIM(comments.comment)
thisform.copyclip(cText)
.Selection.Paste
.Selection.MoveDown(5,1)
.Selection.SelectCell
.Selection.Font.Bold = 9999998
.Selection.TypeText('Comment Response:')
.Selection.Font.Bold = 9999998
.Selection.TypeParagraph
.Selection.TypeParagraph
cText =ALLTRIM(comments.response)
thisform.copyclip(cText)
.Selection.Paste
.Selection.MoveDown(5,1)
.Selection.SelectCell
.Selection.Font.Bold = 9999998
.Selection.TypeText('Change to Document:')
.Selection.Font.Bold = 9999998
.Selection.TypeParagraph
.Selection.TypeParagraph
cText =ALLTRIM(comments.change)
thisform.copyclip(cText)
.Selection.Paste
ENDWITH
x = x+3
ENDSCAN
oWord.Selection.WholeStory
oWord.Selection.Font.Name = "Times New Roman"
oWord.Selection.Font.Size = 12
oWord.ActiveDocument.SaveAs(cDocName,0)
oWord.quit
RELEASE oWord
RETURN


 
A few comments that may help you clean this up and perhaps solve your problem.

1) To go through all the columns of a grid, use a FOR EACH loop:

Code:
FOR EACH oColumn IN ThisForm.Grid1
   * Now you can refer to the problems of a particular column directly:
   cName = oColumn.Header1.Caption
   *etc.

ENDFOR

2) Don't work with the Selection object in Word. It's slow and unwieldy. Use Range objects instead. Similarly, you don't often need the MoveX methods. In addition, the Insert methods are a better choice than TypeText.

3) The Tables.Add method returns an object reference to the table. Save that in a variable and you can refer to all the components of the table:

Code:
oTable = .ActiveDocument.Tables.Add(oWord.Selection.Range,1,14,1,1)

The table has Rows and Columns collections and a Cell method that let you address entire rows, columns or individual cells directly. For example, to put text into Row 1, Column 3, you could use this code:

Code:
oTable.Cell(1,3).Range.InsertAfter("Here's a heading")

There's so much code in your example that I'm not going to try to rewrite it all. If you want to give one example of something you want to do, I can give it a shot.

Tamar
 
THANKS! Let me play with your suggestions first. I'll read up on the range objects. I've been piecing this together from the macro recorder in Word (which is probably obvious), but I'll try taking your approach over the weekend and see what I can do. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top