Export data to MS Word
You can export data from your database to a Microsoft Office Word document.
To export data from your database to MSWord, you must create a script file (*.cd).
In the file, you must have the query that will call the data from the tables you want and the commands to export the data to MSWord.
Let’s take a few moments to walk through the process step-by-step.
1. Open the application.
2. Right-click and select “Developer.”
3. Select “Scripts” and then select “Create.”
4. In the window, you must write your code. The example below shows the code for exporting a customer’s order into MSWord.
proc exp_order()
order_id = Input(“Parameter”;”Order’s number:”;””)
cust_id = Input(“Parameter”;”Customer’s code:”;””)
start_sql “C_ORD” “KOSMOS”
SELECT
CUSTOMER.NAME, ORDER.DATE, ORDER.NUMBER,
ORDER.AMOUNT, ORDER.QUANTITY
FROM
CUSTOMER, ORDER
WHER
(CUSTOMER.CUSTOMER = ORDER.CUSTOMER)
AND (ORDER.NUMBER =: N)
AND (CUSTOMER.CODE =: C)
end_sql
call RunEmbSQL(“C_ORD”;order_id;cust_id)
q =QueryByName(“C_ORD”)
dat = TGetFld(q;”DATE”)
num = TGetFld(q;”NUMBER”)
name = TGetFld(q;”NAME”)
amnt = TGetFld(q;”AMOUNT”)
qnt = TGetFld(q;”QUANTITY”)
dat, num, name, amnt and qnt is the variables that I want to send to word
call DisconnectWord() this command closes ms word
call OpenWordDoc(“C:\KOSMOS\ORDER.DOC”) this command opens the word file (we must create this file, the data always exports into an existing excel file)
call FixVarsForWord()
call SendVarsToWordDoc1(“ORDER.DOC”). These two commands send the variables to word
call ShowWord() this command opens the word and the file
call FreeEmbSQL(“C_ORD”)
end
5. Save changes and close the window.
6. You must create a document with the name order.doc into the folder c:\kosmos.
In the document, you must call the variables with a word’s command, DOCVRIABLE.
You can use the DocVariable field to retrieve the value of a variable after setting it to display within a Word document.
-- On the Insert menu, click Field
-- In the Categories box, select Document Automation.
-- In the Field names list, select DocVariable.
-- In the New Name box, under Field properties, type the name of the document variable.
-- Click OK.
For example {DOCVARIABLE “dat” \* MERGEFORMAT}
7. Call the procedure.
(You can call the procedure from the menu or with a button.
The command you use is “call procedure name().”
For example “call exp_order()“ )