open and save word document- read only problem

Answered open and save word document- read only problem

  • 15 เมษายน 2554 10:08
     
     

    first i don't know english very well im sorry about that and than im working on windows form application and use visual studio 2010 and vc#.

     

    i open word document it's okay but it's read only. i want to open change and save this document.  i can't find any solution about this problem.

    word document which on my disk is not read only but application open it at read only mode. what can i do? how can i give permission to application to open document with change permission

ตอบทั้งหมด

  • 15 เมษายน 2554 13:21
     
      มีโค้ด
    We did a Word integration project a few years ago.  This issue sounded familiar to me, so I had a look at the code we used to save Word documents and this is what I found:
            string originalPath = ActiveContent.Document.FullName;
            bool readOnly = ActiveContent.Document.ReadOnly;
            try
            {
              ActiveContent.Document.Save();
            }
            catch (Exception)
            {
              // a "command failed" exception is thrown here if you hit cancel on
              // the Save As dialog
              // However for some reason the "read-only" exception occurs, even though
              // the document IS successfully saved. Weird!
    
    Take that as you like..
  • 15 เมษายน 2554 18:02
     
      มีโค้ด

    unfortunately Microsoft.Office.Interop.Word dll not include activecontent class.

    my code block below

     

    Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
    Document document = app.Documents.Open("D:\\word2.doc");
      
      int count = document.Words.Count;
      for (int i = 1; i <= count; i++)
      {
      // Write the word.
      string text = document.Words[i].Text;
      Console.WriteLine("Word {0} = {1}", i, text);
      }
      document.Activate();
    

     



  • 15 เมษายน 2554 21:23
     
      มีโค้ด

    I can open a read-only Word document via:

          File.SetAttributes(file, FileAttributes.Normal);
          File.Open(file, FileMode.Open);
    

    If I have problems writing to the document then I can utilize the FileIOPermission:

     

    string file = "\\\\usdfoit01\\cmname$\\Documents\\test.doc";
    
    FileIOPermission fio = new FileIOPermission(FileIOPermissionAccess.Write, file);
    
    fio.Demand();
    

    Please Vote &/or "Mark As Answer" if this post is helpful to you. Thanks and happy coding :D
  • 15 เมษายน 2554 22:31
     
      มีโค้ด

    I can open a read-only Word document via:

       File.SetAttributes(file, FileAttributes.Normal);
       File.Open(file, FileMode.Open);
    

    If I have problems writing to the document then I can utilize the FileIOPermission:

     

    string file = "\\\\usdfoit01\\cmname$\\Documents\\test.doc";
    
    FileIOPermission fio = new FileIOPermission(FileIOPermissionAccess.Write, file);
    
    fio.Demand();
    

    Please Vote &/or "Mark As Answer" if this post is helpful to you. Thanks and happy coding :D
    :( not work
  • 16 เมษายน 2554 3:02
     
     
    what exactly is the code you are trying and what error messages occur?

    Please Vote &/or "Mark As Answer" if this post is helpful to you. Thanks and happy coding :D
  • 16 เมษายน 2554 14:57
     
      มีโค้ด

    im not getting error message. when open word document at windows, i edit and save document but when open document at form application it is read only mode and i can't save my changes. it is not give any error messages

    code block

        private void pToolStripMenuItem_Click(object sender, EventArgs e)
        {
          int r = RIndex;
          object missing = System.Reflection.Missing.Value;
          Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
          try
          {
            Document document = app.Documents.Open("D:\\work\\meb\\Checkedlisbox.doc");
            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
              // Write the word.
              string text = document.Words[i].Text;
              Console.WriteLine("Word {0} = {1}", i, text);
            }
    
            document.Activate();
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex + "-----\nError");
          }
    
        }
    
    
    please help :(

  • 16 เมษายน 2554 21:06
     
     
    do you still have the document open elsewhere?  There is really nothing wrong with your code.  I just ran it on a simple test app, and it displayed every word in my document in the console.  Maybe you should add app.Visible = true, and check your task manager and see, how many copies of Word you have running.  The default seems to be to run Word invisible.
  • 17 เมษายน 2554 13:36
     
     คำตอบ
    it is very interesting :s i don't do anything and open application today then run app and it work it is very interesting :s
    • ทำเครื่องหมายเป็นคำตอบโดย neurocranium 17 เมษายน 2554 14:47
    •  
  • 11 พฤษภาคม 2555 21:00
     
     

    I found the fix to Word 2010 giving the error message "Read-Only" when saving a Word file.  For me, after I copied folders to an external hard drive (destination) that contained both Word 2010 and Word 1997-2003 documents, I opened some of the Word files from the destination source.  The only files which I opened that gave me the read-only error when I tried to save the file were the Word 1997-2003 documents.  None of the Word 2010 files opened resulted in the Read-Only error message when I saved the files.

    So, a work around for the Read-Only error (but still wishing Microsoft would step up and just fix the problem):  Open the Word 1997-2003 file, and save it as a Word 2012 file with a new name.  Technically, you don't need to use a new file name, as the extension will be different, but it reduces confusion.