Solved

c# creating word doc with template

Posted on 2014-04-21
1
712 Views
Last Modified: 2014-05-26
Hi,

I am using the code below.

I have a problem in that
1) I want to save the file without the user actually having to save it
2) also the other problem is that even if the user has to save it i get error "file in use by other user or application"

   Object oMissing = System.Reflection.Missing.Value;

            Object oTemplatePath = "C:\\Invoices\\Test1.dotx";



            Word.Application wordApp = new Word.Application();
            Word.Document wordDoc = new Word.Document();

            wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

            foreach (Word.Field myMergeField in wordDoc.Fields)
            {


                Word.Range rngFieldCode = myMergeField.Code;

                String fieldText = rngFieldCode.Text;



                // ONLY GETTING THE MAILMERGE FIELDS

                if (fieldText.StartsWith(" MERGEFIELD"))
                {

                    // THE TEXT COMES IN THE FORMAT OF

                    // MERGEFIELD  MyFieldName  \\* MERGEFORMAT

                    // THIS HAS TO BE EDITED TO GET ONLY THE FIELDNAME "MyFieldName"

                    Int32 endMerge = fieldText.IndexOf("\\");

                    Int32 fieldNameLength = fieldText.Length - endMerge;

                    String fieldName = fieldText.Substring(11, endMerge - 11);

                    // GIVES THE FIELDNAMES AS THE USER HAD ENTERED IN .dot FILE

                    fieldName = fieldName.Trim();

                    // **** FIELD REPLACEMENT IMPLEMENTATION GOES HERE ****//

                    // THE PROGRAMMER CAN HAVE HIS OWN IMPLEMENTATIONS HERE

                    if (fieldName == "Title")
                    {

                        myMergeField.Select();
                        wordApp.Selection.TypeText(sTitleToPrint);

                    }
                    if (fieldName == "FirstName")
                    {

                        myMergeField.Select();
                        wordApp.Selection.TypeText(sNameToPrint);

                    }
                    if (fieldName == "Surname")
                    {

                        myMergeField.Select();
                        wordApp.Selection.TypeText(sSurnameToPrint);

                    }

                }

            }

            DateTime now = DateTime.Now;
            string sConvertTimetoString = Convert.ToString(now);
            string sRemoveCharfromTime = sConvertTimetoString.Replace('/', ' ');
            string sRemoveColonfromTime = sRemoveCharfromTime.Replace(':', ' ');

            string sNametoSaveInvoice = sClientIDtoSave + " " + sClientNametoSave + " " + sRemoveColonfromTime;
            wordDoc.SaveAs(sNametoSaveInvoice);
            //wordApp.Documents.Open("sNametoSaveInvoice");
            wordApp.Application.Quit();
        }

Open in new window


Please help

Thanks

R8VI
0
Comment
Question by:R8VI
1 Comment
 
LVL 15

Accepted Solution

by:
Ess Kay earned 500 total points
ID: 40013441
The problem #2 you are having - file in use. What happens with Microsoft office products and extension,  when a file is opened, a new file is created (lock file).
If you try to overwrite using C#, microsoft finds the lockfile, and says it is in use. When the file is no longer in use, microsoft deletes the lock file.

What you need to do:

Several thing i can think of...
One:   When you create the connection instance to word, it create the file, so just close the connection and you will be able to save the file

Two: Save as a temp file, When used is done with the editing, close the instance(as above) Then copy the temp to the real location (overwrite and change name), then delete the temp





As far as your save problem, you can try an autosave using the second method noted above.
Then if the program is ended abruply, see if you have any temp files left, and bring up the screen, Do you want to resume the save files or diregard (just like microsoft)


hope that helps
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question