Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by quarzwar · Jan 05, 2013 at 02:22 PM · crashstreamreader

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:

H

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

crash report.png (151.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phouse2019 · Oct 24, 2018 at 03:47 PM 0
Share

But what about reading an RTF file into Unity? Isn't that only for exporting?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges