?
Solved

c# creating word doc with template

Posted on 2014-04-21
1
Medium Priority
?
985 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 15

Accepted Solution

by:
Ess Kay earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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