Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7847575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:56:44+00:00 2026-06-02T17:56:44+00:00

Hello I know how to open and get plain text from a word file

  • 0

Hello I know how to open and get plain text from a word file for a bit of code. However I am having a bad effect occur from this. The word doc becomes locked and I am not aware how to de allocate whatever is locking it. What I ultimately want would like to open a file that will be in a UNC path and read part of it, whether someone else has it open or not, parse lines from it, close it, don’t save anything or lock the file. The closest I can get is I can open a file, parse it, it will lock, but when I open it and close it(same machine running the code) it magically unlocks it.

I know almost nothing on COM so I hunted for a while and found something out about the Marshall object and added that. I have tried to turn on and off the reference ‘ReadOnly: True’, ‘ReadOnly: False’. I feel there is just something simple I am missing so I thought I would ask here. Any suggestions feel free, I am coding in .NET 4.0 so you don’t have pass in all those annoying obj = null, obj = null for opening the Doc.

 public static string ReadWordDoc(string loc)
{
Word.Application wordApp = new Word.Application();

Word.Document Doc = wordApp.Documents.Open(loc, ReadOnly: true);

sb = "";

foreach (Word.Paragraph objParagraph in Doc.Paragraphs)
{
try 
{
    sb += objParagraph.Range.Text + "\n";
}
catch (Exception ex)
{
    throw ex;
}
}

return sb;

Doc.Close(SaveChanges: false);

Marshal.ReleaseComObject(Doc);

wordApp.Quit(SaveChanges: false);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:56:47+00:00Added an answer on June 2, 2026 at 5:56 pm

    I figured it out for some reason a lock was being kept. I made sure I quit the Word app with ‘Do Not Save Changes’ as well as running the Garbage Collector as well. It may be a little overkill but it essentially ensures that no lock is left on my file I am targeting.

    Word._Application wordApp = new Word.Application();
        Word._Document Doc = wordApp.Documents.Open(loc, ReadOnly: true);
            try
            {
                sb = "";
    
                foreach (Word.Paragraph objParagraph in Doc.Paragraphs)
                {
                    try
                    {
                        sb += objParagraph.Range.Text + "\n";
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            catch (COMException) { }
            finally
            {
                //FileInfo finfo = new FileInfo(loc);
                //finfo.IsReadOnly = false;
    
                if (Doc != null)
                {
                    Doc.Close();
                    Doc = null;
                }
                if (wordApp != null)
                {
                    wordApp.Quit(Word.WdSaveOptions.wdDoNotSaveChanges);
                    wordApp = null;
                }
            }
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            GC.Collect();
            GC.WaitForPendingFinalizers();
    
            return sb;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Friends I want to know is there any way we can open or
i know how to write code for jquery fadein effect. suppose i have a
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that
Hello I would like to know if my script is good; I want to
hello i am little new to all this but i want to know that:
Hello Im currently writing a product syncronisation script for magento. I know how to
I need to know how i can make a string like hello to Hello
hello I have a small question in objective-C memory management. I know that if
Hello I am new to MATLAB , I wanted to know how can I
Hello every one I want to ask a question about file transfer using sockets.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.