Use MS Word as report designer and generator in Delphi app

H

Hilarion

Hi!

I looked for it in the NG archives and Googled for it
but I'm not able to find any good examples. I'm sure
that there are plenty, but still can't find good
keywords to search for.

What I'm looking for is some good tutorial and/or
samples that show how to embed MS Word window into
an application and make it:

1. A report designer, which will allow user to
create a kind of report template by placing some named
markers (fields, but not as Word form fields) probably
from application supplied list. Those markers will
be later (see point 2) used by the application, when
the report is generated using this report template,
to place the proper data using template specified
format. There will be many application defined report
types and the field list available during report
template design should contain only the fields that
are available for this report type. This mechanism
should also allow using tabular data (user can
design the table column list - a subset of columns
provided by the application for this specific table,
design headers and somehow specify the data rows
formats separately for each column), including more
than one table in one report (probably user should
somehow bind Word table with application source table).

2. A report generator, which will allow user to select
report template (designed in step 1) valid for current
report type, and the report would get generated
(a "standalone" Word document should be the result)
based on that template and application data.

It'd be nice if the tutorials and/or samples would
include a scenario when the report templates (and/or
generated report documents) are saved to (and loaded
from) application DB (eg. some application provided
stream, not a file/network source).

The application I'll try to implement it into is
written with Borland Delphi 6. The tutorial/sample
does not have to be focused on Delphi, but if it's
not then it should not use any features very specific
to the development environment and/or programming
language/platform used.

If I'm asking in a wrong newsgroup, then please
point me to the right one.
If I'm asking for something that was allready
explained here (or in some other NG), then please
point me to the right post (by giving the post
subject and/or author name and/or post date
and/or NG name).
If it can be found on the web, then I'd be grateful
for any links.
If you have your own solutions or tips and are
willing to share them, than I'll be most grateful
for any of those.

I'm aware that this subject may be complexed,
so I'd also be grateful for any resurces that will
help me achieve even some small parts of the problem
solution.


Hilarion
 
H

Hilarion

[snip]
What I'm looking for is some good tutorial and/or
samples that show how to embed MS Word window into
an application and make it:

What I forgot to add is that I'd want it to work
with MS Word 2002 (XP) and above (2003, 2007).
It'd be nice if it worked with older versions but
it's not required.

I also forgot to thank you in advance for any
responses, which I hereby do: Thank you.

Hilarion
 
D

Doug Robbins - Word MVP

How about using Mail Merge?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Hilarion

Doug said:
How about using Mail Merge?

I'm using Polish version of MS Word so I'm not exactly sure
to which functionality you are referring to, but I suppose
it's the mechanism that allows the user to select some data
source and define a document template with fields from this
data source and then choosing a record from this data
source create a document (or documents), or e-mail those
documents (to multiple addresses).

How to use it programically? Eg. how to embed MS Word window
into my app and then:
A. In report design mode:
1. force Word to load previously saved report definition
(there'l always be one),
2. force Word to go into "Mail Merge" mode,
3. force use of my data sources specific to the current
report type (not one source but more - there will
be at least: one basic source, one tabular source
- bound to the basic in master-detail relationship)
B. In report generation mode:
1. force Word to load previously saved report definition,
2. force "read only" mode,
2. force Word to go into "Mail Merge" mode,
3. force use of my data sources AND only specified
records (this one I may get by placing only one
record in basic source and only related records
in tabular sources),
4. force generation of static document from curent
template and record.

I know how to use Mail Merge in Word, but how to use
it from my app? How to block some user action (eg.
changing data sources, editing template in report
generation mode, creating static documents in report
design mode, etc.)?

Hilarion

PS.: Please excuse my English - it's not my native
language. If something is not clear in what I
write, then please ask - I'll try to explain.
 
D

Doug Robbins - Word MVP

I do not know how you would do it in Delphi, but if you were using Access I
would suggest that you see fellow MVP Albert Kallal's "MergeSingleWord"
utility at:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

Might be best to Google for a Delphi newsgroup where you might find
something similar or get advice that is specific to Delphi.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Hilarion

Doug said:
I do not know how you would do it in Delphi, but if you were using Access I
would suggest that you see fellow MVP Albert Kallal's "MergeSingleWord"
utility at:

http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

Looks promising - has mechanisms for designing reports and
generating them. Lacks any code that would allow embeding Word
into any application and lacks code that would allow using
tabular data in the report.
I'd be grateful for examples of those "missing" functionalities.

Might be best to Google for a Delphi newsgroup where you might find
something similar or get advice that is specific to Delphi.

I'm allready doing this - got some replies to look in Word/Office
newsgroups :) but - as I said previously - I'm not looking only
for solutions allready made in Delphi. Any solution/example
made for any Window application language/environment that is
not very specific to this language/environment would do.

Hope this helps.

Yes, it helps a lot. Thank you.


Hilarion
 
D

Doug Robbins - Word MVP

It's starting to sound to me like you are wanting to do a "multiple items
per condition" type operation, which Word's Mail Merge facility does not
really have the capability to do. In such cases, I almost always use a
report in Access.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Hilarion

Doug said:
It's starting to sound to me like you are wanting to do a "multiple items
per condition" type operation,

I'm not sure how to understand that. If by "multiple items per condition"
you mean that I want data from multiple sources and multiple records
(also from one source) in my report, then you are right. Some reports
my application generates consist of parts contained in 3-4 tables,
usually 1 record in "master" table (basic report data) and many records
in "slave" tables (multiple report parameter values, which have to
be presented in tabular form in the report).

which Word's Mail Merge facility does not really have the capability to do.

That's what I was affraid of. I found one VB app that managed to do
something like that using slightly different approach, but there
was no way for the user designing the report to influence the tabular
data formats (he could only put a placeholder into the report design
which would be replaced by a table when the report was generated).

In such cases, I almost always use a report in Access.

But in this case there's no Access. The database is an unspecified
OLE DB source (I mean it can be any database - we are using MS SQL
in development environments). We can't require MS Access on client
computers - we can only require MS Word for report design and
report generation.


Is there no other way around this? Someone mentioned SmartTags and/or
SmartDocuments (whatever that is). Someone else mentioned that I
could try developing some MS Word add-in.
If you think that one of those paths may lead to a solution for
my problem, then please point me to the right direction.

Again thank you for your help you provided and for your help that
you'll hopefuly provide in the future.

Regards.

Hilarion
 
D

Doug Robbins - Word MVP

For the multiple items per condition, read "one to many" and from your
further description, I would say that is what you are doing.

And, that means, that mailmerge is probably not able to be used for the
purpose and it will be necessary to "roll-your-own" using vba.

You might get a few ideas from the "Group Multiple items for a single
condition" item on fellow MVP Cindy Meister's website at:

http://homepage.swissonline.ch/cindymeister/mergfaq1.htm#DBPic

Or take a look at the following Knowledge Base Article:

http://support.microsoft.com/default.aspx?scid=kb;en-us;211303

or at:

http://www.knowhow.com/Guides/CompoundMerges/CompoundMerge.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Hilarion

Doug said:
For the multiple items per condition, read "one to many" and from your
further description, I would say that is what you are doing.
Yup.


And, that means, that mailmerge is probably not able to be used for the
purpose and it will be necessary to "roll-your-own" using vba.

OK. I was affraid that it'll come to this.

You might get a few ideas from the "Group Multiple items for a single
condition" item on fellow MVP Cindy Meister's website at:

http://homepage.swissonline.ch/cindymeister/mergfaq1.htm#DBPic

Or take a look at the following Knowledge Base Article:

http://support.microsoft.com/default.aspx?scid=kb;en-us;211303

or at:

http://www.knowhow.com/Guides/CompoundMerges/CompoundMerge.htm

I'm digging into those resources. Thank you very much.

I'll give feadback if I found a solution to my problems.
Thank you again.

Best regards.


Hilarion
 
H

Hilarion

OK. I was affraid that it'll come to this.
I'm digging into those resources. Thank you very much.

I'll give feadback if I found a solution to my problems.
Thank you again.



I looked into those (thank you again for providing the links),
but - as you anticipated - I found out that probably the only
way to do what I need is to "roll-my-own" solution similar
to mailmerge mechanism (using Word OLE interfaces).

I planned to use mergefields. My Word extension would recognize
which of them have to be treated as tabular data an it would be
my extension that does the value substitution (not the Word
mailmerge mechanism).

Before I got to a working prototype, I was shown an application
that does a very similar task. It was "Microsoft Small Business
Accounting 2006". It allows to create invoice (and not only
invoice) templates using XSD schema and XML tags in the document
and XML tag nesting is used to indicate tabular data. The application
then allows creation of invoice document (from invoice application
form) with chosen invoice template.
The problem is that I found no documents nor articles that would
describe how one can do such transformation. I mean how can
I use such a Word document place document data in the XML tags
(including tabular data). If there is such a manual or article,
then I'd be grateful for a link to it, but I'm afraid that
"Microsoft Small Business Accounting 2006" has it's own solution
that uses Word interfaces to locate XML tags and replace them
with data, and that there's no additional support for such
actions.

Another idea that came to me was a slightly different approach.
It did not require any use of Word OLE interfaces (so you not
only do not have to deal with differences between Word versions
but also do not need any Word version installed at all).
The idea was to create an application that is able to read
and modify Word documents in Microsoft Office Open XML format
(docx, docm). The format is open, so all specifications are
available and is based on XML, which is quite easy to use.
Support for Open XML can be added to Word 2000, Word 2002 (XP)
and Word 2003 (and is natively supported in Word 2007), so there
should be no problems with users of older Word versions. The
problems with this solutions are:
- Microsoft Office Open XML format specification is more
that 6000 pages long, so there's no way I can read it
all.
- Microsoft Office Open XML format uses ZIP compression, so
my application does not only have to handle XML, but also
has to handle ZIP decompression and compression.
My application has to be written using Delphi 6 and can't
use .Net, so .Net 2.0 support for Open XML is not available
for me. I'll deal with the size of Open XML specification
somehow, but the ZIP part of the problem remains. I looked
for any documentation on any Microsoft API that allows
ZIP handling. There should be something like this at least
on the Windows platforms that have integrated ZIP handling
(like Windows ME, Windows XP) and / or when the MS Office
with Open XML format support is installed (eg. Windows 98
with Word 2000 with Open XML support installed). My goal
is to:
1.not include any ZIP handling library in my application,
2.use any available Microsoft ZIP handling (as above:
from Windows or from MS Office),
3.if no Microsoft ZIP handling is available, allow user
to select any third party compression application that's
able to handle ZIP files.
If you know anything that would help me achieve the second
part, then I'd be very grateful.

Thank you all in advance and thanks again to Doug Robbins
for all the help he already provided.

Regards.

Hilarion

PS.: Again, if you'll find my English not clear, then please
not hesitate and ask - I'll try to explain better.
English is not my native language and I do not have
much occasions to use and practice it, so it's very
far from perfect.
 
D

Doug Robbins - Word MVP

Sorry, I am going to have to leave you here as I don't have any experience
with Delphi or the XML/Zip features that you are talking about. I am sure
however that if the data was in Access, I could cobble something together.

Probably best if you post a new message so that it will be more likely to
come to the attention of others who may be able the help.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top