|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello Everybody
I am working on a web based ASP.NET project wherein I am trying to open a user selected word document in the browser window using VB.NET. I have tried the Shell() Command, Tried CreateObject(), also tried using Word.ApplicationClass. After using Word.ApplicationClass I have also done the security settings for the Word.olb file. But I am yet not able to open the file. If I try to do that using the first 2 methods it works in case of WINForms. But not for WebForms. Please Suggest me tha appropriate way of opening word, excel, PDF. Please confirm if I can use OLE Control somehow in ASP.NET My Code goes somewhat like this 1)My First Approach THIS WORKS FINE WITH WINFORMS BUT DOESNOT WORK WITH WEBFORMS. Code:
Dim XLApp As Excel.Application
Dim XLBook As Excel.Workbook
Dim XLsheet As Excel.Worksheet
XLApp = CType(CreateObject("Excel.Application"), Excel.Application)
XLBook = CType(XLApp.Workbooks.Open("C:\Cyber School Docs\RahulKate.xls"), Excel.Workbook)
XLsheet = CType(XLBook.Worksheets(1), Excel.Worksheet)
XLsheet.Application.Visible = True
2)My Second Approach This also works fine with Winforms using VB.NET but not with Webforms Code:
Shell("C:\Program Files\Microsoft Office\Office\WINWORD.EXE " & "C:\Rahul1.doc", vbMaximizedFocus)
3) My Third approach This doesnot work with either. Code:
Dim MyApp As New Word.ApplicationClass() Dim FName As String FName = "C:\Cyber School Docs\Rahul.doc" Dim MyDoc As New Word.Document() MyDoc = MyApp.Documents.Open(FName) MyDoc.Activate() Regards Rahul |
|
#2
|
||||
|
||||
|
If you just want them to view it why don't you just binary the file to them and let them IE do the rest? No need to make it complicated.
|
|
#3
|
|||
|
|||
|
I am trying to do something simmilar. Did you ever resolve the issue?
|
|
#4
|
|||
|
|||
|
If you haven't resolved the issue....I was doing this (of course probably not the most efficient way....) I think you're basically just missing excel_workbook.Activate()
note: this is also not cleaned up, I apologize. Code:
Imports Excel
Imports System.IO
Imports System.Runtime.Serialization
Namespace spread
<Serializable()> Public Class XlsFile
Private _filename, _
_client_filepath As String
Private _file_size As Integer
Public Property filename() As String
Get
Return _filename
End Get
Set(ByVal Value As String)
_filename = Value
End Set
End Property
Public Property client_filepath() As String
Get
Return _client_filepath
End Get
Set(ByVal Value As String)
_client_filepath = Value
End Set
End Property
Public Property file_size() As Integer
Get
Return _file_size
End Get
Set(ByVal Value As Integer)
_file_size = Value
End Set
End Property
'Public Function GetBinary(ByVal name As String) As FileStream
' If (File.Exists("name")) Then
' Return New FileStream("name", FileMode.Open, FileAccess.Read, FileShare.Read)
' End If
'End Function
Public Sub New()
xls = New Excel.Application
xls.Visible = False
End Sub
Public Sub Close()
'xls.Workbooks(1).Close(False)
ReleaseComObject(wb)
ReleaseComObject(ws)
ReleaseComObject(xls)
End Sub
Public Sub Open(ByRef fi As FileInfo, Optional ByRef isBinary As Boolean = False)
'myWorkbooks = CType(xls.Workbooks.Open(fileName), Excel.Workbook)
'myWorksheets = CType(xls.Worksheets(1), Excel.Worksheet)
If (isBinary) Then
wb = xls.Workbooks.Open(ConfigurationSettings.AppSettin gs("binPath") + filename + ".bin")
Else
wb = xls.Workbooks.Open(fi.FullName)
End If
xls.Visible = True
wb.Activate()
End Sub
Private Sub ReleaseComObject(ByRef ref As Object)
Try
Do Until _
System.Runtime.InteropServices.Marshal.ReleaseComO bject(ref) <= 0
Loop
Catch
Finally
ref = Nothing
End Try
End Sub
End Class
End Namespace
Code:
Imports System.IO
Imports Microsoft.Office
Namespace spread
Module MainModule
'Globals
Public Const GENERAL_EXCEPTION_MESSAGE As String = "An exception has occured: "
Public Const INVALID_FILE_FORMAT As String = "The file format you attempted to " & _
"upload is not a valid format! Only excel file formats are allowed."
Public excel_file As XlsFile
Friend xls As Excel.Application = Nothing
Friend wb As Excel.Workbook = Nothing
Friend ws As Excel.Worksheet = Nothing
End Module
End Namespace
Code:
Private Sub cmdUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpload.Click
Dim file As File
Dim fileName As String = filUpload.PostedFile.FileName.Trim
Dim fileLength As Integer = 0
Dim fileContentType As String = Nothing
Dim uploadSuccess As Boolean = False
Dim fileExtension As String
Dim fs As FileStream
Dim bw As BinaryWriter
Dim intStatus As Integer
Dim fi As New FileInfo(fileName)
Try
fileLength = filUpload.PostedFile.ContentLength
fileContentType = filUpload.PostedFile.ContentType
'validate the file type is acceptable
If (fileContentType = "application/vnd.ms-excel") Or (fileContentType = "application/octet-stream") Then
If file.Exists(fileName + ".bin") Then
file.Delete(fileName + ".bin")
End If
Dim fileContent(fileLength) As Byte
excel_file.filename = fi.Name
excel_file.client_filepath = fi.FullName
excel_file.file_size = filUpload.PostedFile.ContentLength
excel_file.Open(fi, False)
SerializeFile(excel_file)
Else
lblNotification.Visible = True
lblNotification.Text = INVALID_FILE_FORMAT
End If
Catch exOra As OracleClient.OracleException
lblNotification.Text = GENERAL_EXCEPTION_MESSAGE + exOra.Message
lblNotification.Visible = True
Catch ex As Exception
lblNotification.Text = GENERAL_EXCEPTION_MESSAGE + ex.Message
lblNotification.Visible = True
Finally
conn.CloseConnection()
End Try
End Sub
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > ASP.NET & VBA |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|