?
Solved

c# creating word doc with template

Posted on 2014-04-21
1
Medium Priority
?
908 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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