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

Word automation, while opening a word doc, how to control how many rows to insert on a table

Status
Not open for further replies.

titoneon

MIS
Dec 11, 2009
335
US
Hi Guys,
I am working on an existing Word doc that has two tables and i am filling the bottom table with data from a cursor, the problem is that the word doc, bottom table is predefined with 17 rows and if i have more than 17 records from the cursor, then the table2 on the Word doc, will get more than 17 rows causing the bottom text i have in the word doc, to get offset and jumping to a second page leaving the first page w/o that bottom text, besides a 2do page won't have the first table either, so i need if there are more than 17 records on the cursor, to make sure i will have one page with table1, table2 filled with the records from the cursor and the text at the bottom, then a second page with the same values on table1 as they t won't never change, then table2, should have the other values from the cursor and also the same text at the bottom, don't know how to control that.

can anyone has a suggestion please ?

Note: i will insert here the Original Word doc we use to save data on it, then the result of what happened with that Word doc(save as you can see below) when the data from the cursor has more than 17 records and course i will always has more than 17 records for the second table.
also i will include here a piece of code i borrowed from a friend.

Thanks in advance

Code:
** Automation
LOCAL loW,loD, loT1, loT2,lnRow
LOCAL lcPath2
lcPath2 ="G:\APPROVED COMPANY FORMS\ISO DOCUMENTS\F-0830-005 Design Review Package.doc" 
loW = CREATEOBJECT("Word.Application")
loD = m.loW.Documents.Add(m.lcPath2)
loT1 =  m.loD.Tables[1]
loT2 = m.loD.Tables[2]

loT1.Cell[1,1].Range.InsertAfter(ALLTRIM(m.lccust_name))
loT1.Cell[1,2].Range.InsertAfter(ALLTRIM(m.lcjob_no))
loT1.Cell[2,1].Range.InsertAfter(ALLTRIM(m.lcmach_type))
lot1.Cell[2,2].range.paragraphs[1].range.Characters[lot1.Cell[2,2].range.paragraphs[1].range.Characters.count - 1].insertbefore(m.lc_TypM)
loT1.Cell[2,2].Range.InsertAfter(TRANSFORM(m.lc_date))

lnRow = 1
SELECT  JUNK3  && cursor i am talking about
GO top
SCAN
	IF m.lnRow = m.loT2.Rows.Count
		m.loT2.Rows.Add()
	ENDIF
	lnRow = m.lnRow + 1
	loT2.Cell[m.lnRow,1].Range.InsertAfter(ALLTRIM(Drawing))
	loT2.Cell[m.lnRow,2].Range.InsertAfter(ALLTRIM(Sheet))
IF 	EMPTY(rev_d)
    loT2.Cell[m.lnRow,3].Range.InsertAfter(TRANSFORM(SPACE(8)))
else    
	loT2.Cell[m.lnRow,3].Range.InsertAfter(TRANSFORM(rev_d)) 
ENDIF 	
	loT2.Cell[m.lnRow,4].Range.InsertAfter(ALLTRIM(Description))
ENDSCAN
loW.Visible = .T.
loD.SaveAs(ADDBS(JUSTPATH(FULLPATH(m.lcPath2)))+"&lc_filen")
 
 http://files.engineering.com/getfile.aspx?folder=3bb21fc6-84f8-491a-b17b-3d7258457c22&file=F-0830-005_Design_Review_Package.doc
The best approach to doing VFP Automation of Office documents is to first do your task totally without VFP.

Without using VFP at all, go into your Office document and set up to Record a Macro.
Then do whatever you want to do - recording the entire process start to completion.
Then Stop Recording the Macro and go into the Macro Edit Mode to examine what was done and how it was done.
Even though the Office Macro code (VBA) is not the same as VFP, you have an idea as to what needs to happen.

Now go into VFP and attempt to duplicate those actions in VFP Automation.
When you do, at the Beginning of the Automation set your object's Visibility TRUE
loW.Visible = .T.​
so that you can watch what is occurring
When everything works as intended, you can set your Visibility FALSE

That process has worked for me time and time again.

Good Luck,
JRB-Bldr



 
More or less, you need to wrap the bulk of your code in a loop for the number of pages you need. Your SCAN loop should become a counted loop that goes either to 17 or to the end of the table.

(BTW, you don't need GO TOP before SCAN. It's implied unless there's a WHILE clause.)

Tamar
 
Thanks a lot for the explanation, the approach of the record a macro is the thing i was missing, thanks, yeah that was right, no reason for the "GO TOP"
Thanks again
 
Tamar - you are correct that using the Macro approach verbatim may not be the best approach to use.
But when you don't have a clue as to how/what to do in order to make something occur via VFP Automation, it can be a very useful guideline.
I know that it has worked for me many times.

JRB-Bldr

 
Right. A macro at least tells you the object(s) and method(s) of interest. But one should never just translate it into VFP code.

Tamar
 
As rule of thumb: A macro is as good as it is reusable within Word itself - or not. Many macro code is hardcoding values or making use of a current selection, often it's just half baked what you need. It also depends on your way of recording the macro and I recently often had the problem of not being able to do things in macro recording mode I could do without. It's also good to read into the object model and macros can give you good hints on what objects and properties to look at in more detail.

Bye, Olaf.
 
My preferred discovery tool for Office Automation is the Object Browser, which you can reach from the Visual Basic Editor in Word, Excel, etc. It gives you the full spec for each of the PEMs in the object model - a bit like the Class Browser in VFP.

That, plus Intellisense, will usually be enough to get started, although I do occasionally use the macro recorder as well.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
In this case, though, the Word automation code is alraady there, this mainly is about doing the SCAN loop for 17 records only and then change to a new Word page. The main task to add to the automation is creating enough Word page copies to have enough room for X times 17 records + Y<17 records.

Number of pages needed therefore is number of rows/17 rounded down plus 1, if the number of rows is not divisible by 17 exactly.

[tt]nPages = INT(Reccount()/17) + IIF(Reccount%17>0,1,0)[/tt]

Now you need code copying the template page as many times as you need copies (nPages-1).

Taken from
Assuming the page to be copied is exactly one page above the new one created and the selection point when your code is run is on the new page, then this should work:
Code:
'Go back one page
Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1
'Copy current page
ActiveDocument.Bookmarks("\Page").Range.Copy
'Go forward one page
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
'Paste copied page
Selection.Paste

If you need to goto/copy a specific page you can use 'Name'. E.g 

Selection.GoTo What:=wdGoToPage, Name:="3" 

would go to page 3.

You know better than us what page number of your template you need to copy.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top