Link to home
Start Free TrialLog in
Avatar of awolarczuk
awolarczukFlag for Australia

asked on

Writting Report in VP6 with word or vs view

Hi guys, i have been trying to get around the a fact that i need to write report for some of the software i am writting, but i am now at the stage that i have to, i have got a copy of vs view 7 and have been told that it is good to use, but on the other hand i am told that it is better to get an API for MS word and point to there.
What do you guys think??

What everone you think can you point me to some example code - this is my first time doing any sort of report
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

Sorry I do not know VS View.

The problem with generating reports using Word is that it is very slow.

A quick way is to create a Word document with tokens and save the document as an RTF file. As a kind of template.

The VB code loads the template and splits the template into parts. You then output a RTF file. When the output is complete you load the RTF and save as a word document.  This is real fast and it is cute too.


---------------------Start of RTF document
$$$TITEL$$This Is My Report Title
$$$OPTIONS$$/NoTotals
$$$SQL$$
Select * from [MyTable] Order By [MyKey];
$$$TOP$$
This is the top part of my report printed on $$TODAY$-


Starts a table with column headings
$$$LINE$$
A part of a table with clumns
|$$Field1$-|$$Field2$-$$Field3$-|
$$$END$-
End the table

That concluded the report
------------------------End of RTF Document

Once you have created your RTF file you can view using notepad so see how it is constucted.

All you need to do is read in the file, and split the file into parts using code like this

Dim sParts() As String
sParts = SPlit(sRTFFileDATA, "$$$")


Notice that you can build logic into the RTF file so your template interpreter can operatoe without any coding. So you can create new reports wihout have to write one single line of code.

You do need to understand how the paras in RTF work. I created a class that can manipulate RTF, populate the report and output a Word document.

I would also suggest that Word works well for documents like invoices etc. But Exel works better for data type reports.  With Excel it is so simple it is not true. You just slam the data onto a clipboard, paste into a spreadsheet, then apply column sizes and formats. You can even apply Bold, Underline sections etc.  Again I created a class so I can now generate reports without having to write a sinlge line of code......

If you would like any more info just ask.
Avatar of awolarczuk

ASKER

ok this might sound like a dumb question but it sounds like i have to out put it is a rtf file and then open word myself ( the software will not do it and then view it and print it from there_, please tell me if this is not correct as it sounds like a long way round
No the software does it all for you.

VB Can:

1) Create a hidden (or visible when you are testing) instace of Word.

2) Open the RTF document.

3) Save the document as a Word document.

4) Closes the Word instance.

5) Email the result to the requesting person; redirect the client to a download for the document if running on a web server. Or is the user is an interactive user, the Word instance can be made visible so the operator can decide what to do with the data.

For Excel it is just the same. Excel is much faster to work with than Word.  With excel you place the data onto the clipboard then paste the clipboard into a spreadsheet you created.

How are you going to write an app when you nothing about it. WHat you do in VB is create a reference (Projects, References menu) and select Microsoft Word x.x Object Library or the Excel one if you want to work with spread sheets.

The you can veiw the object browser or help to see what you can do.

Here is an example function for handling Excel.  

First you create your spread sheet:

Dim sData As String

sData  = "MyCol1" + vbTab + "MyCol2" + vbTab +" MyLastCol" + vbCrlf
sData  = sData + "MyRow2Col1" + vbTab + "MyRowCol2" + vbTab +" MyRow2LastCol" + vbCrlf

' create the spread sheet
OK =  ExcelCreate(sData)


Public Function ExcelCreateOK(FromData As String, Optional psFileName As String = "") As Boolean

' Fires up Excel and pastes data into a new workbook
' See class zExcel for a better version of this which allows formatting
' of columns

' OK = GF.ExcelCreateOK(sDataToPaste)

Const zxlNormal = -4143

Dim IDE As Boolean

' Are we running in IDE or EXE mode?
On Error Resume Next
Err.Clear
Debug.Print 1 / 0 ' this will create an error in the IDE
If Err.Number <> 0 Then
    IDE = True
End If
If IDE Then
    On Error GoTo 0 ' we want to see errors in IDE so we can fix them
Else
    On Error GoTo ErrorTrap
End If

' Create the Excel objects
Dim ExcelApp ' As Excel.Application
On Error Resume Next

Set ExcelApp = CreateObject("Excel.Application")


Dim WB ' As Excel.Workbook

' If no filename make visible to hold on screen
ExcelApp.Visible = Len(psFileName) = 0

' create a blank sheet
Set WB = ExcelApp.Workbooks.Add

' set app focus to the new sheet
WB.Activate
ExcelApp.Range("A1").Select

' stuff the data onto clipboard
Clipboard.Clear
Clipboard.SetText FromData

' move clipboard to Excel
ExcelApp.ActiveSheet.Paste
DoEvents
Clipboard.Clear ' release memory
DoEvents

' Do they want to save output file or leave on screen
If Len(psFileName) > 0 Then
    Dim OK
    OK = GF.KillFileOK(psFileName)
   
    ' if a file name save and close Excel
    ExcelApp.ActiveWorkbook.SaveAs FileName:=psFileName, FileFormat:=zxlNormal, Password:="", _
        WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
    ExcelApp.ActiveWorkbook.Close
    ExcelApp.Quit
    Set WB = Nothing
    Set ExcelApp = Nothing
Else
    ' Allow Excel to stay on screen
    Set WB = Nothing
    Set ExcelApp = Nothing
End If

ExcelCreateOK = True
Exit Function

ErrorTrap:
ErrN = Err.Number
ErrD = Err.Description
On Error Resume Next

ExcelCreateOK = False

End Function


"How are you going to write an app when you nothing about it"
Thanks for that, i do know what i am doing when it come to writing the code that i need for the app, but if you read my comment you will not that i have not done any reports as of yet, i have not had the need , i know i  should have done something small up for myself but just haven't have the time, and i am still learning as i go.........   if it isnt to much troulble have you got an example for the word side of the world?? i had already tuned on the components in VB6 for word as well :P
(when you nothing about it.) Very very sorry, for my poor choice of words and bad English... I meant to say that if you are not familiar with the Word/Excel objects you can make a reference to them in VB and explore them.

Another tip is to create a word/excel macro that does roughly waht you want then copy/paste into VB but usually you need to rewrite the code slightly.

OK I will find a word example......but it will be slower than using the RTF method.
thanks an example of the rtf file would be good as well
ASKER CERTIFIED SOLUTION
Avatar of inthedark
inthedark
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial