470,585 Members | 1,706 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,585 developers. It's quick & easy.

Merging Word Documents Problem

I need help with merging two Word documents into one through C#
code. The problem isn't so much getting the documents put into one as
it is maintaining the appropriate formatting, or rather reformating,
after the merge. This is a full description of my needs.

I have a C# class library that creates two Crystal Reports, and then
exports them to the harddrive as Word documents. One's orientation is
landscape, the other is portrait. I then have a function that takes
those two documents, opens one up and merges the other into the first
using insert file so that I can use gfi faxmaker to send the merged
report to the appropriate parties.

Here is the problem. When I choose insert file, it places the one
report literally on top of the second report. So I modified the first
report to have a blank page as a report header in Crystal. The merge
then works the same way, one on top of the other, but since the first
report is in landscape, it looks like the report is all in landscape.
If I do this without automation using word I can open report1, insert
file report 2, put the insertion point on the page break and select
file pagesetup and change orientation to portrait. This gives me page
one portrait, a blank page 2 after a section break in portrait and page
3 in landscape. I can then delete the page break and I am left with
exactly what I need. Report2 in portrait as page one, a section break,
and then report1 in page2 in landscape. However, when I attemtp to
automate the task with this code

//Word.Application WordApp;
Word._Document WordDoc;
WordDoc = new Word.DocumentClass();
Word.ApplicationClass WordApp = new Word.ApplicationClass();
object missingValue = Missing.Value;
object FalseObject = false;
object TrueObject = true;
//object PageBreak = Word.WdBreakType.wdPageBreak;

object docName = @"c:\report1.doc";

object endOfDoc = "\\endofdoc";

string insertDocPath = @"c:\report2.doc";

WordDoc = WordApp.Documents.Open (ref docName, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue, ref missingValue, ref missingValue,
ref missingValue);

//add a try catch block here?
object cursorLocation = WordDoc.Content.Start;

//WordApp.Selection.InsertBreak(ref PageBreak);
//WordDoc.Bookmarks.Item(ref endOfDoc).Range.InsertFile(insertDocPath,
ref missingValue, ref FalseObject, ref missingValue, ref missingValue);

//WordApp.Selection.InsertFile(insertDocPath, ref missingValue, ref
FalseObject, ref FalseObject, ref FalseObject);
WordApp.Selection.InsertFile(insertDocPath, ref missingValue, ref
FalseObject, ref FalseObject, ref FalseObject);
Word.Range rng = WordDoc.Range(ref cursorLocation, ref cursorLocation);
WordApp.Visible = true;
******WordDoc.PageSetup.Orientation =

//WordApp.Visible = true;

WordDoc.Close(ref missingValue, ref missingValue, ref missingValue);

WordApp.Quit(ref missingValue, ref missingValue, ref missingValue);

************END CODE****************

It gets to the line that is starred above, changing orientation, and
says this command is not available.

As you can see, I have tried different methods to get this to work to
no avail.

I would try to do it with the Dialog class, but I can't find good
documentation on how to use this in C#. A pointer to this info even
would be helpful. I don't claim to be an excellent researcher, but I
do try.

Some explanations that may help people understand. The artificialness
of the report with a blank page was done because Crystal seems to do
everything inside of a paragraph frame when it is exported. With out
the blank page, and thus the page break, it is virtually impossible to
get the insertion point to reside outside of a frame. If it is IN a
frame the insertion occurs inside the frame, and all formatting is only
applied to the frame. The pagesetup option is also expressly not
allowed when a selection object is on or in a frame. That is the
purpose of the WordDoc.Range(ref start, ref start) line. My thought
was the insertion point was at an illegal location for pagesetup
commands, and I was trying to manually put it on the page break. I am
not sure if I was successful. I guess I could use a range object to
select more then one frame, but I have very little experience with Word
automation, and only know what I have been able to figure out from MSDN
(that which you see in code already).

If anyone could help, I would be GREATLY appreciative. I have spent
two days on only this already, and many hours searching
groups.google.com and MSDN. I am just not coming up with the answers
that make sense to me.

I am pretty sure this should be able to be done, because I can do it
without automation with a mouse and keyboard just fine. I just can't
figure out the code that mimics my actions correctly.

I am using Word 2000 SR-1 SP3 (it is what client has)
Crystal Reports 9? (the one included in .Net)
Version 1.1.4322 of the framework

Martin Harris
Systems Development intern
E.S. Systems, Inc.

p.s. I am using Word because .pdfs don't work with faxmaker, and html
and rtf's don't seem to maintain the non-text items (frame borders and
lines and such) of the Crystal Reports.

Nov 16 '05 #1
0 6051

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan Nash | last post: by
3 posts views Thread by William Ahern | last post: by
2 posts views Thread by North Country Boy | last post: by
3 posts views Thread by Patrick | last post: by
reply views Thread by Maheshwari | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.