• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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
0
awolarczuk
Asked:
awolarczuk
  • 4
  • 3
1 Solution
 
inthedarkCommented:
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.
0
 
awolarczukAuthor Commented:
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
0
 
inthedarkCommented:
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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
awolarczukAuthor Commented:
"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
0
 
inthedarkCommented:
(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.
0
 
awolarczukAuthor Commented:
thanks an example of the rtf file would be good as well
0
 
inthedarkCommented:
Function showing how to convert RTF data to Word


Public Function SaveAsDocOK(psDocFileName As String, Optional psDocData As String = "") As Boolean

' Function to:
' 1) Save psDocData (which is RTF type data) as an RTF file
'    or save the contents of msDocument as an RTF file
' 2) Open Word App
' 3) Load the RTF and save as doc


zClearError

Dim WA 'As Word.Application ' don't use Early binding in case word version change


Dim OK
Dim sDocPath As String

If Len(psDocData) > 0 Then
    msDocument = psDocData
End If

sDocPath = GF.LeftPart(psDocFileName, ".", True) + "."

OK = GF.WriteFileOK(sDocPath + "rtf", msDocument)
GF.Delay 0.25 ' allow windows server write buffer to empty before file is opened


' Open the Word Application

On Error Resume Next
Do
    Err.Clear
    Set WA = CreateObject("Word.Application")
    If Err.Number <> 0 Then Exit Do
   
    WA.Visible = False ' make it so the op cannot see Word
       
    ' Open the RTF file within Word
    WA.Documents.Open sDocPath + "rtf"
    If Err.Number <> 0 Then
        ErrD = "Could not load file: " + sDocPath + "rtf" + vbCrLf + Err.Description
        ErrN = Err.Number
        On Error GoTo 0
        Do While WA.Documents.Count > 0
            WA.Documents(1).Close False
            DoEvents
        Loop
        WA.Quit False
        Set WA = Nothing
        Exit Function
    End If
   
    Dim lDocType As Long
   
   
    ' Save the document
   
    lDocType = 0 'WdSaveFormat.wdFormatDocument
   
    ' you way need to create earlier versions of word document
    '109=word 97
    'lDocType = 109
   
    '
    WA.ActiveDocument.SaveAs sDocPath + "doc", lDocType
   
    If Err.Number <> 0 Then
        ErrD = "Could not save file: " + sDocPath + "doc"
        ErrN = Err.Number
        On Error GoTo 0
        Do While WA.Documents.Count > 0
            WA.Documents(1).Close False
            DoEvents
        Loop
        WA.Quit False
        Set WA = Nothing
        Exit Function
    End If
    On Error GoTo 0
   
    ' cose all documents
    Do While WA.Documents.Count > 0
        WA.Documents(1).Close False
        DoEvents
    Loop
   
    WA.Quit False

    Exit Do
Loop

Set WA = Nothing

' remove the RTF data
Kill sDocPath + "rtf"

SaveAsDocOK = True

End Function
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now