Merge Word Document problem

M

mharris

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

**********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?
WordDoc.Activate();
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);
rng.Select();
WordApp.Visible = true;
******WordDoc.PageSetup.Orientation =
Word.WdOrientation.wdOrientLandscape;

//WordApp.ActiveDocument.Save();
//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.

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. If nothing else, if you know a better group to
ask the question in, I am willing to try anything.

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.
 
C

Cindy M -WordMVP-

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.
I'm only familiar with Word, and not the other software involved, so I
need a little additional clarification
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.
I take it, when you bring this into Word and click in the text coming
in from Crystal, you see some kind of border/frame around the text?
When you right-click this "frame" you should see a "Format" command in
the Context menu. Does it say "Format Frame" or something else (such
as Text box or Object or ???)?

Your surmise, that the commands probably aren't available when the
cursor is in this position, and you're using the Selection object, is
quite likely correct. One important thing to understand about
automating Word is that you should try to work around the Selection
object, using Ranges. So you were definitely headed in the right
direction when you started playing with the Range object. Try
something along these lines (I'm a VB person, so you'll probably need
to tweak the following to get "real" C#)

Word.Range rng = WordDoc.Content;
//Focus at the end of the document
rng.Collapse(Wd.CollapseDirection.wdCollapseEnd); //wdCollapseEnd
//Insert the section break here

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 

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