none
Reading file from local drive in SharePoint 2010 Visual Web Part

    Question

  • Hi All,

    I am new to share point 2010 development.I am working on an application to perform mail merge operations. I am trying to read a word file(Template for mail merge) from local drive/network shared locations in share point 2010 visual web part using below code. 

    Object missing = System.Reflection.Missing.Value;
    Word.Application oWord = null;
    Word.Document oWordDoc = null;
    oWord = new Word.Application();
    oWordDoc = new Word.Document();
    object oTemplatePath = @"D:\Templates\IT.docx";
    // Object oTemplatePath = @"\\windows-2be8wa6\Templates\IT.docx";
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref missing, ref missing, ref missing);

    This code works fine in simple Asp.Net application but when I try to run
    this code in Visual Web part it gives me following error

    Com Exception 
    "Word was unable to read this document. It may be corrupt. Try one or more of the following: * Open and Repair the file. * Open the file with the Text Recovery converter"

    Kindly guide me how to resolve this issue.

    Regards,
    Nabeel Ahmad
    Tuesday, May 01, 2012 4:49 AM

All replies

  • Hi All,

    I am new to share point 2010 development.I am working on an application to perform mail merge operations. I am trying to read a word file(Template for mail merge) from local drive/network shared locations in share point 2010 visual web part using below code. 

    Object missing = System.Reflection.Missing.Value;
    Word.Application oWord = null;
    Word.Document oWordDoc = null;
    oWord = new Word.Application();
    oWordDoc = new Word.Document();
    object oTemplatePath = @"D:\Templates\IT.docx";
    // Object oTemplatePath = @"\\windows-2be8wa6\Templates\IT.docx";
    oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref missing, ref missing, ref missing);

    This code works fine in simple Asp.Net application but when I try to run
    this code in Visual Web part it gives me following error

    Com Exception 
    "Word was unable to read this document. It may be corrupt. Try one or more of the following: * Open and Repair the file. * Open the file with the Text Recovery converter"

    Kindly guide me how to resolve this issue.

    Regards,
    Nabeel Ahmad

    Tuesday, May 01, 2012 4:49 AM
  • hmm, your combining two things that SharePoint web parts really shouldn't be doing, you are reading from the file system and using the office com api.

    If you've already gotten the "Dont use the com api on the server" lecture and want to move forward...is this a permissions issue? In your asp.net application were you impersonating the app pool or not?


    Please mark my response as an answer if appropriate.
    Learn.SharePoint.com

    Tuesday, May 01, 2012 5:17 AM
  • where is the file located on the sharepoint server or on your local machine ?

    Because sharepoint will try to fetch the file from sharepoint server


    If the reply was helpful or informative, please remember to mark it as answer or vote as helpful. MCITP SHAREPOINT

    Tuesday, May 01, 2012 6:10 AM
  • May be you don't have permissions to read this file?

    Check permissions of "SharePoint - 80" web application pool account.


    Don't happy, be worry...

    Tuesday, May 01, 2012 7:40 AM
  • file is located on share point server 
    Tuesday, May 01, 2012 7:59 AM
  • Hi Aviw,<o:p></o:p>

    Thanks for your reply. I have verified Share Point – 80  web application pool account has full permission.


    Tuesday, May 01, 2012 8:36 AM
  • Thanks for ur reply

    I don't thinks its an permission issue as i have verified from both ISS Settings & share point Central Administration my account has full permission. Can you suggest any alternative of COM API for word automation in share point?

    Tuesday, May 01, 2012 8:43 AM
  • I always use the components from aspose (aspose.com), they are great for doing word automation.

    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Tuesday, May 01, 2012 9:56 AM