要將RichTextBox中的內容通過剪貼板粘貼到word中.
現在粘貼時報錯,說剪貼板為空,但我執行Ctrl+V,可以將剪貼板中的數據粘貼出來.但就是執行paste()時過不去.
代碼如下
string strDirectory = System.IO.Directory.GetCurrentDirectory().ToString();
string strTempFileName = strDirectory + @"\temp000.docx";
Clipboard.Clear();
#region 將RichTextBox的內容存儲到臨時word文檔中
try
{
//建立word工作
_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);
rtxbContent.SelectAll();
rtxbContent.ClipboardCopy();
_wordDoc.Activate();
_Word.Paragraph _wordPara = _wordDoc.Content.Paragraphs.Add(ref missing);
_Word.Range _wordRange = _wordDoc.Paragraphs[1].Range;
_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("保存成功", "成功啦");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(), "失敗啦!<( ̄︶ ̄)>");
}
#endregion
報錯如圖