Microsoft Excel cannot access the file ' SegmentExport2_d0ad8d.xlsx'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are try

Sangtani, Payal (CSW) 0 Reputation points
2024-12-20T17:32:35.5733333+00:00

HI Web application Hosted on IIS Server and when i am trying to open the excel file from vb .net code getting

Microsoft Excel cannot access the file 'E:\inetpub\Sites\iHomeSite\main\Macros\SegmentExport2_d0ad8d.xlsx'. There are several possible reasons: • The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook.

Getting error on line

Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(MacroFolderPath + sTmpExcelFiles)

Please find the below code snippet.Issue need to get resolve asap.

#Region "START: Appling Macro"

		Dim fi As System.IO.FileInfo

		Dim MacroFolderPath As String = HttpContext.Current.Server.MapPath("\main\Macros\")

		Dim MacroTemplateFolderPath As String = ConfigurationManager.AppSettings("MacroTemplateFolderPath")

		' Dim MacroTemplatefilepath As String = ConfigurationManager.AppSettings("MacroTemplateFilePath")

		Dim NewProductTypesDate As Date = CDateSpecial(ConfigurationManager.AppSettings("NewProductTypesDate"))

		Dim MacroTemplatefilepath As String = Nothing

		Dim dCreated As Date = CDateSpecial(Session("dCreated").ToString())

		If (dCreated >= NewProductTypesDate) Then

			MacroTemplatefilepath = ConfigurationManager.AppSettings("MacroTemplateFilePath")

		Else

			MacroTemplatefilepath = ConfigurationManager.AppSettings("MacroTemplateFilePathForOld")

		End If

		Dim sTmpExcelFiles As String = String.Empty

		sTmpExcelFiles = Path.GetFileName(rr.ReportName) & "_" & Left(System.Guid.NewGuid().ToString(), 6) & "New" & "." & rr.ExportTypeExtension.Replace(".", "")

		System.IO.File.Copy(sTmpExcelFullFilePath + sTmpExcelFile, MacroFolderPath + sTmpExcelFiles)

		Dim TempMacroTemplatefilepath As String = MacroTemplateFolderPath & "ePSR Excel Task Template" & "_" & Left(System.Guid.NewGuid().ToString(), 6) & ".xltm"

		System.IO.File.Copy(MacroTemplatefilepath, TempMacroTemplatefilepath)

		Dim AppliedMacroTemplateFile As String = "ePSR Excel Task Template" & "_" & Left(System.Guid.NewGuid().ToString(), 6) & ".xltm"

		Dim AppliedMacroTemplateFilepath As String = MacroFolderPath + AppliedMacroTemplateFile

		Dim xlApp As New Excel.Application()

		Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(MacroFolderPath + sTmpExcelFiles)

		Dim xlWorkSheet As Excel.Worksheet = Nothing

		xlWorkBook = xlApp.Workbooks.Open(TempMacroTemplatefilepath)

		xlApp.Run("Auto_open")

		xlWorkBook.SaveAs(AppliedMacroTemplateFilepath, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLTemplateMacroEnabled)

		xlWorkBook.Application.Quit()

		xlApp.Quit()

		fi = New System.IO.FileInfo(sTmpExcelFullFilePath + AppliedMacroTemplateFile)

		If fi.Exists Then

		Else

			System.IO.File.Copy(MacroFolderPath + AppliedMacroTemplateFile, sTmpExcelFullFilePath + AppliedMacroTemplateFile)

		End If

		System.IO.File.Delete(MacroFolderPath + AppliedMacroTemplateFile)

		System.IO.File.Delete(MacroFolderPath + sTmpExcelFile)

		System.IO.File.Delete(TempMacroTemplatefilepath)

		Utilities.PushFileServerDocToBrowser(sTmpExcelFullFilePath & AppliedMacroTemplateFile, AppliedMacroTemplateFile)
```#End Region
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
2,053 questions
Access
Access
A family of Microsoft relational database management systems designed for ease of use.
420 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,760 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,961 Reputation points Microsoft Vendor
    2024-12-23T03:23:25.1933333+00:00

    Hi @Sangtani, Payal (CSW),

    Running Excel through Microsoft.Office.Interop.Excel on a server (like IIS) often leads to problems because Excel is not designed to be run in a server environment.

    Considerations for server-side Automation of Office

    Consider using Open XML which let developers create, edit, read, and transform file content on the server side. This is the recommended and supported method for handling changes to Office files from a service.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.