Click here to Skip to main content
15,868,340 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello!

I`m trying to create a report system by using MSWord 2007, but i have problems with "Template replication".

I have done the code part that opens template and creates a new document, but document is MESS - borders are erased, tables and captions are in completely different positions, what is unacceptable.

I`m working four days to find solution on MSDN and other popular programming sites, but i can`t figure it out!

Thank you for your time.

I`m creating application that uses Microsoft Word 2007 as a report tool - printing certain documents from their respective templates. It fails to do so. Here is a piece of code:
C#
private void ManagersDoc_Click(object sender, EventArgs e) 
{
 Object wMissing = System.Reflection.Missing.Value;
 Object wTrue = true;
 Object wFalse = false;
 Word.Application wordApp = new Word.Application();
 Word.Document wordDoc = new Word.Document();
 wordApp.Visible = true;
 Object docPath = @"J:\Quar.dotm";
 // Path to Document
 Template wordDoc = wordApp.Documents.Add(ref docPath, ref wMissing, ref wTrue, ref wTrue);
 ... 
// Interaction with Database PostgeSQL 9.1.2.
 Object oSaveAsFile = @"J:\\SampleDoc.doc";
 wordDoc.SaveAs(ref oSaveAsFile, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing, ref wMissing); wordApp.Quit();


We have template of document that is going to be printed, it has:
1) Two tables
2) Captions
3) Markers All is neat, clean and takes 50 roughly percent of sheet.

What code is supposed to do:
1) Create a new Word document from "Quar.dotm" Template.
2) Fill it with data exactly as it is in template

What it does:
1) Create a new word Document from "Quar.dotm" template.
2) Fill it with data in such way that you have to manually place all tables, captions and markers in it.

In addition to that, in preview mode (where MsWord shows how this document will look on paper) everything is split on 2 pages, 2 tables randomly re-sized and scattered across two pages.
IDE: Visual Studio 2010 Language: C# 3.0 Reason why C# 3.0 - app is going to be deployed on Windows XP. .Net Framework version: 3.5 What i`m doing wrong here?
Posted
Updated 28-Apr-12 4:11am
v2
Comments
Richard MacCutchan 28-Apr-12 8:51am    
I don't think anyone else will be able to figure it out either. You need to edit your question and add some sensible technical information about what you are doing.
[no name] 28-Apr-12 9:55am    
What have you tried? What errors are you getting? Where is the code that demonstrates your problem?
[no name] 28-Apr-12 10:12am    
I have removed your comments and added them to your question. Please use the Improve question widget to update your question.
Maciej Los 28-Apr-12 10:22am    
Show us what you've done until now, especially, a part of code where you saves the data into the table in MS Word document.
Aleonis 28-Apr-12 10:52am    
Saving data to the table depends on another algorithm that is going to be tested after is sort this one out. I have updated my question and it contains code and description now.
I`m trying to understand why my piece of code creates document according to a template (what was intended), but breaks order and position of information in created document (what was not supposed to happen at all) every time i execute code mentioned in my 1st message.

I see one or two reasons:
1) destination file extension is not correct!
and/or
2) SaveAs method doesn't specify FileFormat.

Ad 1)
Replace:
C#
Object oSaveAsFile = @"J:\\SampleDoc.doc";

with
C#
Object oSaveAsFile = @"J:\\SampleDoc.docx";

*.doc extension was used in MS Word 97-2003 files.
*.docx is default extension in MS Word 2007 files.

Ad 2)
If you would like to save the file in previous version of MS Word, you need to specify FileFormat. Read more about SaveAs[^] method and wdSaveFormat[^] enumeration.
 
Share this answer
 
Comments
Maciej Los 29-Apr-12 6:39am    
If my solution was helpful, please, mark it as "solved".
Aleonis 29-Apr-12 8:28am    
Well, i looked at the format, added format type: wdFormatDocument and added .docx instead of simply .Doc.

Now i`m getting error: "type mismatch HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH)"

Here is the code:
Object oSaveFileFormat = (Object)"wdFormatDocument";
wordDoc.SaveAs2(ref oSaveAsFile,
ref oSaveFileFormat,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing,
ref wMissing, ref wMissing,
ref wMissing, ref wMissing, ref wMissing);


I`ve tried also to leave format (wdFormatDocument parameter) as wMissing, while changing .doc to .docx - no effect. Error is still here...
Maciej Los 4-May-12 7:31am    
Have you defined oSaveFileFormat variable? I don't see it! Example
You are actually performing a mail merge. Using COM is not the best possible approach since it is version dependent, non-robust and cannot be deployed on server side.
Some affordable third-party for mail-merge can you save a lot of time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900