Solved

c# creating word doc with template

Posted on 2014-04-21
1
797 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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