C#在操作word時,將RichTextBox內容粘貼到word時失敗,昨天經網友提醒,發現是延時的.
於是今天在copy()之後,延時幾秒中以達到效果.但怎麼也不成功.求教
string strDirectory = System.IO.Directory.GetCurrentDirectory().ToString();
string strTempFileName = strDirectory + @"\temp000.docx";
Clipboard.Clear();
_Word._Application _wardApp = new _Word.Application();
_wardApp.Visible = false;
_Word._Document _wordDoc = null;
object missing = System.Reflection.Missing.Value;
object filename = strTempFileName;
object readOnly = false;
object isVisible = false;
_wordDoc = _wardApp.Documents.Add(missing, missing, missing, isVisible);
_Word.Paragraph _wordPara = _wordDoc.Content.Paragraphs.Add(ref missing);
_Word.Range _wordRange = _wordDoc.Paragraphs[1].Range;
#region 將RichTextBox的內容存儲到臨時word文檔中
try
{
//建立word工作
rtxbContent.SelectAll();
rtxbContent.ClipboardCopy();
**//暫時使用了一個彈出窗口解決的**.
MessageBox.Show("正在生成文件,請點擊以確認","確信生成");
_wordDoc.Activate();
_wordRange.Paste();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "失敗啦!<( ̄︶ ̄)>");
_wordRange.Paste();
}
//保存word文檔
_wordDoc.SaveAs2(filename, missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
//退出word
_wardApp.Quit(true, missing, missing);
MessageBox.Show("保存成功", "成功啦");
#endregion
Thread.Sleep(10*1000); 1秒=1000毫秒,可以改成10000。