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

Programmically Open word 1

Status
Not open for further replies.

carolynh

Programmer
Feb 13, 2004
44
US
What i would like to do is progrmmically open Word, programmically place some data from the database in the doc and then let the user print or save or close it. I am running v 91D and will be on Win2003 server.
 
Carolyn,

Have a look at src\samples\activex\spellcheck\spell.p in your Progress installation directory. That should help to get you started.

Mike.
 
You should use MS Word as automation server.

Try this:

First of all, you must create word template.
Choose Insert, Field, MailMerge, MergeField. Then enter the name of the field and click ok. Field will appear in document somehow like this: «FIELDNAME».

Do this for every field you want to have in Word doc, and format the document as you like.

When the main document is merged with the selected data source, information from the specified data field is inserted in place of the merge field.


DEFINE VARIABLE hWord AS COM-HANDLE NO-UNDO.
DEFINE VARIABLE hDocument AS COM-HANDLE NO-UNDO.

CREATE "Word.Application":U hWord NO-ERROR.
ASSIGN FILE-INFO:FILE-NAME = cTemplatePath.

IF FILE-INFO:FULL-PATHNAME = ? THEN
RETURN ERROR.

ASSIGN hWord:VISIBLE = NO
hWord:DisplayAlerts = 0
hWord:ActivePrinter = SESSION:pRINTER-NAME + " on ":U + SESSION:pRINTER-PORT.

hWord:Documents:Add()

/* page setup */

ASSIGN hWord:ActiveDocument:pageSetup:TopMargin = 2.54 * 28.35
hWord:ActiveDocument:pageSetup:BottomMargin = 2.54 * 28.35
hWord:ActiveDocument:pageSetup:RightMargin = 2.03 * 28.35
hWord:ActiveDocument:pageSetup:LeftMargin = 2.03 * 28.35
hWord:ActiveDocument:pageSetup:Gutter = 0
hWord:ActiveDocument:pageSetup:HeaderDistance = 1.27 * 28.35
hWord:ActiveDocument:pageSetup:FooterDistance = 1.27 * 28.35 NO-ERROR.

/*****/

hWord:Selection:InsertFile( FILE-INFO:FULL-PATHNAME ) NO-ERROR.

/* to replace fields in document */

/* cSearch - name of the field in Word document
cReplace - value that you want to assign */

hWord:ActiveDocument:Content:Find:Execute( cSearch,
NO,
NO,
NO,
NO,
NO,
YES,
1,
NO,
REPLACE( cReplace, "~n":U, ", ":U ),
2,
) NO-ERROR.




/* if you just want to print */

NO-RETURN-VALUE hWord:printOut.
hWord:Quit( NO /* Don't save changes */ ).

/******/

/* if you want to see doc */

ASSIGN hWord:VISIBLE = YES.

/******/

RELEASE OBJECT hWord.

For detailed explanation of used metods and parameters see Word's Visual Basic editor help( Tools, Macro, VB editor ).

Hope this helps...
 
HelloMike
When I try to run your sample, I get an error telling me that is doe not understand 'cSerach'. Is there documentation on this anywhere>
 
Carolyn,

Is this a compile or a runtime error? What's the error message number? I've just tried it here and it works fine. That's with 9.1D08, WinXP and MS-Word 2002 SP2.

Mike.
 
Mike
It is a compile time error on Version 9.1D06 WinXP. ** Unknown Field or Variable name - cSearch. (201)
 
Hmm. Weird. I'll have a closer look in the morning -- I have to be somewhere else right now.
 
Carolyn,

There is no variable cSearch in spell.p. However, cSearch is mentioned but not defined in the sample coded that tvrtko posted earlier. Why not just simply define it ?

Mike.
 
Mike
I took another look atthe sample code and seen where he made comments about the cSearch and cReplace.
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top