Error in creating word document SharePoint Openxml

Sachin Soni 26 Reputation points
2021-10-07T14:54:01.767+00:00

Dear Experts,

SharePoint 2016
Windows server 2019

I have used DocumentFormat.OpenXml for generating word document in my SharePoint solution, during opening xml I have faced error in below line.

byte[] binFile = fielTemplate.OpenBinary();

Error

Message: Cannot open file "MyForm.docx". Inside: at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
at Microsoft.SharePoint.Library.SPRequest.GetFileAsStream(String bstrUrl, String bstrWebRelativeUrl, Boolean bHonorLevel, Byte iLevel, OpenBinaryFlags grfob, String bstrEtagNotMatch, Object punkSPFileMgr, Boolean bHonorCustomIrm, IrmProtectionParams fileIrmSettings, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagNew, String& pContentTagNew, SPFileInfo& pFileProps)
at Microsoft.SharePoint.SPFile.GetFileStream(SPWeb web, String fileUrl, SPFileLevel level, OpenBinaryFlags openOptions, String etagNotMatch, SPFileStreamManager spMgr, SPFileRightsManagementSettings rightsManagementSettings, Boolean throwOnVirusFound, SPVirusCheckStatus& virusCheckStatus, String& virusCheckMessage, String& etagNew, String& contentTagNew, SPFileInfo& fileprops)
at Microsoft.SharePoint.SPFile.GetFileStream(OpenBinaryFlags openOptions, String etagNotMatch, String& etagNew, String& contentTagNew)
at Microsoft.SharePoint.SPFile.OpenBinary(SPOpenBinaryOptions openOptions)
at ServicePage.GenerateReports(String ReportName, Int32 AccountRequestId)
Inner Exception: System.Runtime.InteropServices.COMException (0x81070211): Cannot open file "MyForm.docx".
at Microsoft.SharePoint.Library.SPRequestInternalClass.GetFileAsStream(String bstrUrl, String bstrWebRelativeUrl, Boolean bHonorLevel, Byte iLevel, OpenBinaryFlags grfob, String bstrEtagNotMatch, Object punkSPFileMgr, Boolean bHonorCustomIrm, IrmProtectionParams fileIrmSettings, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagNew, String& pContentTagNew, SPFileInfo& pFileProps)
at Microsoft.SharePoint.Library.SPRequest.GetFileAsStream(String bstrUrl, String bstrWebRelativeUrl, Boolean bHonorLevel, Byte iLevel, OpenBinaryFlags grfob, String bstrEtagNotMatch, Object punkSPFileMgr, Boolean bHonorCustomIrm, IrmProtectionParams fileIrmSettings, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagNew, String& pContentTagNew, SPFileInfo& pFileProps) Help Link : Source: Microsoft.SharePoint

Note: I have already uploaded new document template in SP library.

Please help.

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,596 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,893 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,636 Reputation points Microsoft Vendor
    2021-10-08T07:25:05.35+00:00

    Hi @Anonymous ,
    I tested the following code and found when we haven't get the file, the error occurred.

    using (SPSite site = new SPSite("url"))  
                {  
                    using (SPWeb web = site.OpenWeb())  
                    {  
                        SPFile file = web.GetFile("fileurl");  
                        string Destination = @"c:\\Test";  
      
                        byte[] binary = file.OpenBinary();  
      
                        FileStream stream = new FileStream(Destination + "/" + file.Name, FileMode.Create);  
                        BinaryWriter writer = new BinaryWriter(stream);  
                        writer.Write(binary);  
                        writer.Close();  
                    }  
                }  
    

    138794-image.png
    Please check if the code has get the doxc file correctly. The wrong path of the file also will cause the issue. If the error still occurs, please share us more code about how you get the file.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.