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 4103870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:00:54+00:00 2026-05-20T21:00:54+00:00

I have written a procedure that will open a xls from a local disc,

  • 0

I have written a procedure that will open a xls from a local disc, refresh the data in it and then save it again. This works fine.

The problem occurs when I replace the filename to point to a SharePoint site. It opens the file fine. Refreshes the file, but when it trys to save the file it throws an exception with the message “Cannot save as that name. Document was opened as read-only.”.
If I try and save the file with a different filename then it works fine.

Does anybody know what I am missing? I think it must have somethoing to do with how I am opening the file. Is there another way that I can force the opening of the file in a read/write manner?

    private static void RefreshExcelDocument(string filename)
    {
        var xls = new Microsoft.Office.Interop.Excel.Application();
        xls.Visible = true;
        xls.DisplayAlerts = false;
        var workbook = xls.Workbooks.Open(Filename: filename, IgnoreReadOnlyRecommended: true, ReadOnly: false);
        try
        {
            // Refresh the data from data connections
            workbook.RefreshAll();
            // Wait for the refresh occurs - *wish there was a better way than this.
            System.Threading.Thread.Sleep(5000);
            // Save the workbook back again
            workbook.SaveAs(Filename: filename);  // This is when the Exception is thrown
            // Close the workbook
            workbook.Close(SaveChanges: false);
        }
        catch (Exception ex)
        {
            //Exception message is "Cannot save as that name. Document was opened as read-only."
        }
        finally
        {

            xls.Application.Quit();
            xls = null;
        }
    }

Many thanks in advance for suggestions.

Jonathan

  • 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-05-20T21:00:55+00:00Added an answer on May 20, 2026 at 9:00 pm

    Unfortunately you can’t save directly to SharePoint using the Excel API. That’s why the file is being opened as read only – it’s not allowed.

    The good news is that it is possible, but you have to submit the form via a web request. Even better news is that there is sample code on MSDN! In particular notice the PublishWorkbook method that sends a local copy of the Excel file to the server via a web request:

    static void PublishWorkbook(string LocalPath, string SharePointPath)
    {
        WebResponse response = null;
    
        try
        {
            // Create a PUT Web request to upload the file.
            WebRequest request = WebRequest.Create(SharePointPath);
    
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "PUT";
    
            // Allocate a 1K buffer to transfer the file contents.
            // The buffer size can be adjusted as needed depending on
            // the number and size of files being uploaded.
            byte[] buffer = new byte[1024];
    
            // Write the contents of the local file to the
            // request stream.
            using (Stream stream = request.GetRequestStream())
            using (FileStream fsWorkbook = File.Open(LocalPath,
                FileMode.Open, FileAccess.Read))
            {
                int i = fsWorkbook.Read(buffer, 0, buffer.Length);
    
                while (i > 0)
                {
                    stream.Write(buffer, 0, i);
                    i = fsWorkbook.Read(buffer, 0, buffer.Length);
                }
            }
    
            // Make the PUT request.
            response = request.GetResponse();
        }
        finally
        {
            response.Close();
        }
    }
    

    The sample code describes a scenario for the 2007 versions of these products but other versions should behave in the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an AIR Application that downloads videos and documents from a server.
I have just written a stored procedure and stored function that serve to insert
Hai Techies, I have some stored procedure which was written in SQL server.Now i
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a watir script that downloads files. One of the files it
I have written an ASP.NET web page with C# behind that runs an existing
i have a procedure in which the below condition is to be written in
I have a SQL query which is written in string and then executed using

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.