0

I am having trouble saving the word document in C#. the text is pasted in the word document, however saving the file is the problem.

wordDoc.Application oWord;
wordDoc.Document oDoc;
oWord = new wordDoc.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add();
Clipboard.SetText(_Text);
oDoc.ActiveWindow.Selection.Paste();
string fileName = @"C:\Users\Sam\Documents\NewDocument.docx";
oWord.Application.ActiveDocument.SaveAs2(fileName);
oDoc.Close();

The error that occurs is "Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I believe it's the copying of the rich text box.

UPDATE I copied the contents from the rich text box and performed the method manually and this saved with no errors.

Mohsen

System.Windows.Forms.RichTextBox rtb = new System.Windows.Forms.RichTextBox();
rtb.Text = _Text;
rtb.SaveFile(@"C:\Users\Sam\Documents\NewDocument.rtf");

Ken Brittain

Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();

//Set animation status for word application
winword.ShowAnimation = false;

The error also occurs on ShowAnimation i am using office 2007, however i could be using any version of office.

Bish25
  • 606
  • 1
  • 10
  • 35
  • http://stackoverflow.com/questions/37496108/how-to-convert-base64-string-to-doc-docx-in-c/37496548#37496548 – mohsen Jun 15 '16 at 10:23
  • @mohsen i followed the example of going to rtb and there is no text inside the document, see update – Bish25 Jun 15 '16 at 10:39
  • Does the code work if you leave out the `_Text` copy/paste operations? At that point you should have an empty document. – Ken Brittain Jun 15 '16 at 11:26
  • I don't know why it doesn't work.but I used that to save in winforms.if you want to save to rtf here show how in `Wpf` http://stackoverflow.com/questions/21922177/how-to-save-content-of-richtextbox-in-blockuicontainer-to-rtf-file – mohsen Jun 15 '16 at 11:31
  • @kenBrittian No, i have found a no problem where the same error occurs. Check the new update. – Bish25 Jun 15 '16 at 11:31
  • @KenBrittain i have posted the answer below, what an annoying issue! – Bish25 Jun 16 '16 at 08:14

1 Answers1

0

I managed to solved the issue, it was something to do with the latest version on Interop.Word version 15.0.0.0. I downloaded the previous version 12.0.0.0 and worked, had to change some of the code. SaveAs2 to SaveAs. If anyone has a link of where I can report a bug, please comment.

Bish25
  • 606
  • 1
  • 10
  • 35