Disable Save and Save As Option in Excel-2007 using VB .Net

  • Thread starter Thread starter rushuMT
  • Start date Start date
R

rushuMT

Guest
Hello,

I want to disable the save and save As option of Excel file which is exported. Basically, i want to make non-editable excel file which means user unable to edit, Save and save as this file. I have succeed to made read-only excel file but in user will able to save as the file.

For reference, added the application code.

Export Report Button Click Event
Private Sub PictureBox4_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox4.Click
Try
Dim warnings As Warning()
Dim streamids As String()
Dim mimeType As String = ""
Dim encoding As String = ""
Dim extension As String = ""

Dim bytes As Byte() = ReportViewer2.LocalReport.Render("Excel", Nothing, mimeType, encoding, extension, streamids, _
warnings)
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Excel|*.xls"
saveFileDialog1.Title = "Save an Excel File"
Dim workSheet As Excel.Worksheet
saveFileDialog1.ShowDialog()

If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
Dim fs As New FileStream(saveFileDialog1.FileName, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
End If

filepath1 = saveFileDialog1.FileName
MessageBox.Show(filepath1)


Catch ex As Exception

End Try
Try

Dim objFileInfo As System.IO.FileInfo = New System.IO.FileInfo(filepath1)
objFileInfo.IsReadOnly = True

Dim xlApp As New Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim MyPassword As String = "111"
xlWorkBook = xlApp.Workbooks.Open(filepath1)
xlApp.Visible = True
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
xlWorkSheet.Protect(MyPassword, DrawingObjects:=True, Contents:=True, Scenarios:= _
True, AllowFormattingCells:=False, AllowFormattingColumns:=False, _
AllowFormattingRows:=False, AllowInsertingColumns:=False, AllowInsertingRows _
:=False, AllowInsertingHyperlinks:=False, AllowDeletingColumns:=False, _
AllowDeletingRows:=False, AllowSorting:=False, AllowFiltering:=False, _
AllowUsingPivotTables:=False)

xlWorkSheet.SaveAs(filepath1.Replace(".xls", "") + "_New.xls")

xlApp.Workbooks.Close()

Catch ex As Exception

End Try

End Sub



Please do needful.


Thanks & Regards

MTrush

Continue reading...
 
Back
Top