• 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
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 t$$anonymous$$s?

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 = "";
     w$$anonymous$$le (true) {
         input = sr.ReadLine();
         if (input == null) { break; }
         Debug.Log("line="+input);
     }
     sr.Close();
 }

Thank you for taking your time reading t$$anonymous$$s, i'm looking forward to your response :D

crash report.png (151.7 kB)
Comment

People who like this

0 Show 0
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

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 t$$anonymous$$ng at all.

Writing a docx file is far more complicated than just putting text in a file. 10 Seconds of googling turned up t$$anonymous$$s rough overview of the parts in a docx file

The full standard is (I t$$anonymous$$nk) t$$anonymous$$s one, w$$anonymous$$ch covers the technical definition of everyt$$anonymous$$ng you can do in the standard.

The standard .NET library also includes DocumentFormat.OpenXml.Packaging w$$anonymous$$ch may help. http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.aspx

Comment
MarkFinn
hatuf

People who like this

2 Show 0 · 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

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

People who like this

0 Show 0 · 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

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 t$$anonymous$$s library to do t$$anonymous$$s: https://github.com/brunurd/RTF-Exporter

Comment

People who like this

0 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Unity bug - can't test game 2 Answers

Why Android crash after loading 3 models? 0 Answers

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer


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