Solved

c# creating word doc with template

Posted on 2014-04-21
1
690 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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now