Click here to Skip to main content
15,849,129 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have following code, Am using for migration
C#
object readOnly = false;
object isVisible = true; 
Word.Application app = new Word.Application();
app.WindowState = Word.WdWindowState.wdWindowStateMaximize;
app.Visible = true;
Word.Document doc = app.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
doc.Activate();
doc.Save();
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);

Sometimes documents contains some error so document cant able do the save operation, So i need to give some time to execute doc.save() , if it doesnt complete means i need to kill the process, and continue next iteration. Please give some ideas to achieve it

Thanks in Advance
Scorpion

[Edit]Code block added[/Edit]
Posted
Updated 31-Mar-13 4:30am
v3
Comments
Shahin Khorshidnia 31-Mar-13 14:34pm    
Does doc.Save() mathod return a true or false value?
sathish.kasin 31-Mar-13 15:08pm    
It doesnt return anything its a void...
void_Document.Save()
Maciej Los 31-Mar-13 15:09pm    
Remove this line: doc.Activate(); line; check filename parameter.
sathish.kasin 31-Mar-13 15:31pm    
Dear Maciej Los,

If the document contains error means its trying to save it , so the code doesnt run after doc.Save(); Here My requirement is if the code is idle for 2 minutes means it should kill the process and continue with nxt iteration
Maciej Los 31-Mar-13 15:41pm    
First of all, you don't need to use doc.Activate() method.
Seconly, you need ti identify: What kind of error MS Word generates?

In my opinion, MS Word shows some dialog and waits for user interaction. Use app.Activate() to interact MS Word between application and user.

BTW: Which version of MS Word application?

Try using try...catch :
C#
object readOnly = false;
object isVisible = true; 
Word.Application app = new Word.Application();
try
{
  app.WindowState = Word.WdWindowState.wdWindowStateMaximize;
  app.Visible = true;
 Word.Document doc = app.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref    missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
  doc.Activate();
  doc.Save();
  doc.Close(ref missing, ref missing, ref missing);
}
catch(Exception ex)
{
  // log your error here
}
finaly
{
  app.Quit(ref missing, ref missing, ref missing);
}
 
Share this answer
 
Comments
sathish.kasin 31-Mar-13 11:57am    
Thanks for your reply...

My issue is code will not through any exception....if it find any buggy document means at the time its been idle on doc.save() its doesnt execute another line since its trying to save the document
Maciej Los 31-Mar-13 15:09pm    
Good tip, +5!
In my opinion you need to validate form fields before you start saving document. I don't know your requirements, so i can't help you more. Some useful information you'll find here:
http://www.gmayor.com/formfieldmacros.htm[^]
http://word.mvps.org/faqs/tblsfldsfms/ValidateFFields.htm[^]

More about MS Word 2010 developer reference: http://msdn.microsoft.com/en-us/library/office/ee861527%28v=office.14%29.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900