- Home /
How can i write and read Microsoft Word .docx files using StreamWriter?
Hi. I am working on a project, and i need to be able to open a Microsoft word file outside of Unity. Unfortunantely i can't open the .docx If i Edit the file using StreamWriter, if i try, Microsoft Word Crashes with the following report:
I can however Read it through StreamWriter.
Anyone know how to prevent this?
Here is the Code i used:
#pragma strict
import System.IO;
var input = "";
var filePath = "test.txt";
function Update() {
if (Input.GetKeyDown("r")) {
WriteFile(filePath);
}
if (Input.GetKeyDown("f")) {
ReadFile(filePath);
}
}
function WriteFile(filepathIncludingFileName : String)
{
var sw : StreamWriter = new StreamWriter(filepathIncludingFileName);
sw.WriteLine("Name:");
sw.Flush();
sw.Close();
}
function ReadFile(filepathIncludingFileName : String) {
var sr : StreamReader = new StreamReader(filepathIncludingFileName);
input = "";
while (true) {
input = sr.ReadLine();
if (input == null) { break; }
Debug.Log("line="+input);
}
sr.Close();
}
Thank you for taking your time reading this, i'm looking forward to your response :D
Answer by Davidovich · Jan 05, 2013 at 11:18 PM
So that script looks fine, but you're not writing a .docx file, you're writing a .txt file. If you then try to open it as a docx you're going to have problems because they're not the same thing at all.
Writing a docx file is far more complicated than just putting text in a file. 10 Seconds of googling turned up this rough overview of the parts in a docx file
The full standard is (I think) this one, which covers the technical definition of everything you can do in the standard.
The standard .NET library also includes DocumentFormat.OpenXml.Packaging which may help. http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.aspx
Answer by jeffrey39 · May 17, 2013 at 08:13 PM
You may read word files by using of wordrepair docx.
Here http://www.repairdocx.docxrepairtoolbox.com you may find the resources for alternative solution
Answer by brunurd · Apr 20, 2017 at 05:40 PM
If someone come here looking for a solution to write a Word readable file from Unity, I have created this library to do this: https://github.com/brunurd/RTF-Exporter
But what about reading an RTF file into Unity? Isn't that only for exporting?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Importing the character controller package crashes mono.exe and leaves me unable to enter playmode 2 Answers
Updating bug, script crash, cant compile script 0 Answers
Unity Editor has stopped working 4 Answers
Help! I downloaded Unity, but everytime i open it, it shuts down! How do i fix this?? 0 Answers