when we open the excel getting permission issue .
first create function to create the macro then open it .getting error on xlWorkBook = xlApp.Workbooks.Open(MacroFolderPath + sTmpExcelFile).
Please find the below code.pleas ehelp me out.
Protected Function GetMacrosTemplate() As Result
Dim result As New Result(True)
Dim rr As New ReportRequest
Dim rights As New PSRSystemRights(GetCurrentAccountID, Me.ParentID)
Dim rev As New PSRProjectRevisionBO(Me.ParentID)
Dim wsInfo As New PSROracleWSProjectInfoBO()
Dim enumerator As IEnumerator = Nothing
Dim blnOracleWSDataFound As Boolean = False
Dim sProjectNumber As String = rev.ExternalAtts.sProjectNumber
Dim projectInfoViaCallToOracleWebService As XmlDocument = GetProjectInfoViaCallToOracleWebService(sProjectNumber)
Dim sTmpExcelFile As String = String.Empty
Dim sTmpExcelFullFilePath As String = Settings.Miscellaneous.FKIL2005FileRepository
Dim sTmpMacroExcelFile As String = String.Empty
If (rights.IsOriginator Or CIntSpecial(rev.ExternalAtts.sProjectManagerAccountID) = GetCurrentAccountID()) Then
canEditOrginatorProjectManger = True
End If
Dim nsmgr As New XmlNamespaceManager(projectInfoViaCallToOracleWebService.NameTable)
nsmgr.AddNamespace("pro", "http://xmlns.oracle.com/pcbpel/psr/ebs/projectinfo")
nsmgr.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/")
Dim xmlnlist As XmlNodeList = projectInfoViaCallToOracleWebService.SelectNodes("env:Envelope/env:Body/pro:OutputParameters/pro:GET_TASK_DETAILS/pro:PROJECT_TASK_INFO/pro:Row", nsmgr)
Dim g As Guid = Guid.NewGuid()
Try
enumerator = xmlnlist.GetEnumerator
Do While enumerator.MoveNext
Dim current As XmlNode = DirectCast(enumerator.Current, XmlNode)
blnOracleWSDataFound = True
wsInfo = New PSROracleWSProjectInfoBO()
wsInfo.PSROracleWSProjectInfoGUID = g.ToString()
wsInfo.PSRRevisionSegmentID = "0"
wsInfo.PROJECT_NUMBER = current.SelectSingleNode("pro:PROJECT_NUMBER", nsmgr).InnerText.ToString.Trim
wsInfo.SEGMENT_NUMBER = current.SelectSingleNode("pro:TASK_NUMBER", nsmgr).InnerText.ToString.Trim
wsInfo.TASK_NAME = current.SelectSingleNode("pro:TASK_NAME", nsmgr).InnerText.ToString.Trim
wsInfo.ORACLE_SALES_ORDER_NUMBER = current.SelectSingleNode("pro:ORDER_NUMBER", nsmgr).InnerText.ToString.Trim
wsInfo.SHIP_FROM_ORG_CODE = current.SelectSingleNode("pro:SHIP_FROM_ORG_CODE", nsmgr).InnerText.ToString.Trim
wsInfo.ITEM_COST = current.SelectSingleNode("pro:ITEM_COST", nsmgr).InnerText.ToString.Trim
wsInfo.PLANNED_ORDER_PROGRESS_DATE = current.SelectSingleNode("pro:PLANNED_ORDER_PROGRESS_DATE", nsmgr).InnerText.ToString.Trim
wsInfo.ACTUAL_ORDERED_DATE = current.SelectSingleNode("pro:ACTUAL_ORDERED_DATE", nsmgr).InnerText.ToString.Trim
wsInfo.SHIP_DATE = current.SelectSingleNode("pro:SHIP_DATE", nsmgr).InnerText.ToString.Trim
wsInfo.Save()
wsInfo = Nothing
Loop
Finally
If TypeOf enumerator Is IDisposable Then
TryCast(enumerator, IDisposable).Dispose()
End If
End Try
Try
rr.ReportName = "/" & getCFD() & "/PSR/SegmentExport2"
'rr.ExportTypeString = "excel"
rr.ExportTypeString = "excelopenxml"
rr.AddParameter("PSRProjectRevisionID", CStrSpecial(Me.ParentID))
rr.AddParameter("TBDDate", CDateSpecial(PSRRevisionSegmentBO.kPSRRevisionSegmentBO_DefaultDate_TBD))
rr.AddParameter("SystemAdmin", rights.SystemAdmin)
rr.AddParameter("canEditOrginatorProjectManger", canEditOrginatorProjectManger)
rr.AddParameter("canEditProductionScheduler", canEditProductionScheduler)
rr.AddParameter("canEditProjectEngineer", canEditProjectEngineer)
rr.AddParameter("canEditElectricalEngineer", canEditElectricalEngineer)
rr.AddParameter("canEditSourcingScheduler", canEditSourcingScheduler)
If blnOracleWSDataFound Then
rr.AddParameter("OracleProjectInfoGUID", g)
End If
''rr.PushReportFileToBrowser(False) 'all Leads Report reports are NOT shareable!
sTmpExcelFile = Path.GetFileName(rr.ReportName) & "_" & Left(System.Guid.NewGuid().ToString(), 6) & "." & rr.ExportTypeExtension.Replace(".", "")
Dim GetGuid As String
GetGuid = Left(System.Guid.NewGuid().ToString(), 6)
If sTmpExcelFullFilePath.Trim.Length > 0 Then
If sTmpExcelFullFilePath.Substring(sTmpExcelFullFilePath.Length - 1, 1) = "\" Then
sTmpExcelFullFilePath = sTmpExcelFullFilePath.Substring(0, sTmpExcelFullFilePath.Length - 1)
End If
End If
sTmpExcelFullFilePath += "\temp\"
rr.PushReportFileToFileSystem(sTmpExcelFullFilePath & sTmpExcelFile)
#Region "START: Appling Macro"
Dim fi As System.IO.FileInfo
'Dim MacroFolderPath As String = HttpContext.Current.Server.MapPath("\backup\")
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
System.IO.File.Copy(sTmpExcelFullFilePath + sTmpExcelFile, MacroFolderPath + sTmpExcelFile)
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()
'If xlApp IsNot Nothing Then
' xlApp.Quit() ' Close Excel instance
'End If
Dim xlWorkBook As Excel.Workbook
Try
xlWorkBook = xlApp.Workbooks.Open(MacroFolderPath + sTmpExcelFile)
Catch ex As Exception
End Try
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
Catch ex As Exception
result.Succeeded = False
result.AddMessage(ex.Message)
End Try
Return result
End Function