Advertisement
socrtwo

Excel Recovery

Nov 12th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.24 KB | None | 0 0
  1. Option Explicit On
  2. Imports Access = Microsoft.Office.Interop.Access
  3. Imports Excel = Microsoft.Office.Interop.Excel
  4. Imports Word = Microsoft.Office.Interop.Word
  5. Imports PowerPoint = Microsoft.Office.Interop.PowerPoint
  6. Imports System.IO
  7. Imports Microsoft.Office.Interop.Excel
  8. Imports System.Drawing
  9.  
  10. Public Class Form1
  11.  
  12.     Dim filename As String
  13.  
  14. #Region "Functions"
  15.     Private Function ReadExeFromResources(ByVal filename As String) As Byte()
  16.         Dim CurrentAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
  17.         Dim Resource As String = String.Empty
  18.         Dim ArrResources As String() = CurrentAssembly.GetManifestResourceNames()
  19.         For Each Resource In ArrResources
  20.             If Resource.IndexOf(filename) > -1 Then Exit For
  21.         Next
  22.         Dim ResourceStream As IO.Stream = CurrentAssembly.GetManifestResourceStream(Resource)
  23.         If ResourceStream Is Nothing Then
  24.             Return Nothing
  25.         End If
  26.         Dim ResourcesBuffer(CInt(ResourceStream.Length) - 1) As Byte
  27.         ResourceStream.Read(ResourcesBuffer, 0, ResourcesBuffer.Length)
  28.         ResourceStream.Close()
  29.         Return ResourcesBuffer
  30.     End Function
  31.  
  32.  
  33. #End Region
  34.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  35.  
  36.  
  37.         Dim oExcel As Excel.ApplicationClass
  38.         Dim oBook As Excel.WorkbookClass
  39.         Dim oBooks As Excel.Workbooks
  40.  
  41.         Dim sFile As String = PathTb.Text
  42.  
  43.         'Start Excel and open the workbook.
  44.  
  45.         oExcel = CreateObject("Excel.application")
  46.         oExcel.Visible = True
  47.  
  48.         oBooks = oExcel.Workbooks
  49.         oBook = oBooks.Open(sFile)
  50.  
  51.         oExcel.Run("OpenAndRepairWorkbook")
  52.  
  53.  
  54.  
  55.         ' Clean-up: Close the workbook and quit Excel.
  56.         oBook.Close(False)
  57.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
  58.         oBook = Nothing
  59.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
  60.         oBooks = Nothing
  61.         oExcel.Quit()
  62.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
  63.         oExcel = Nothing
  64.  
  65.  
  66.  
  67.         GC.Collect()
  68.     End Sub
  69.  
  70.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  71.         Dim OFD As New OpenFileDialog
  72.         With OFD
  73.             .ShowDialog()
  74.             filename = .FileName
  75.             PathTb.Text = .FileName
  76.         End With
  77.     End Sub
  78.  
  79.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
  80.  
  81.         Dim oWord As Word.ApplicationClass
  82.         Dim sFile As String = PathTb.Text
  83.  
  84.         'Start Word and open the document.
  85.         If Path.GetExtension(sFile) = ".xlsx" Then
  86.             MsgBox("You can not open files with extension .xlsx in Word 2007")
  87.         Else
  88.             oWord = CreateObject("Word.Application")
  89.             oWord.Visible = True
  90.             oWord.Documents.Open(sFile)
  91.         End If
  92.  
  93.     End Sub
  94.  
  95.  
  96.     Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
  97.         Dim sFile As String = PathTb.Text
  98.         Shell("C:\Program Files\Microsoft Office\Office12\EXCEL.EXE /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  99.     End Sub
  100.  
  101.     Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  102.         Try
  103.             Dim sFile As String = PathTb.Text
  104.  
  105.             Dim oExcel As New Excel.Application
  106.             Dim oBooks As Excel.Workbooks = Nothing
  107.             Dim oWB As Workbook = Nothing
  108.  
  109.             'Start Excel and open the workbook.
  110.             If Path.GetExtension(sFile) = ".xls" Then
  111.                 oExcel.Visible = True
  112.                 oBooks = oExcel.Workbooks
  113.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  114.                 MsgBox("Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.")
  115.             Else
  116.                 oExcel.Visible = True
  117.                 oBooks = oExcel.Workbooks
  118.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  119.             End If
  120.  
  121.  
  122.         Catch ex As Exception
  123.             MessageBox.Show(ex.Message)
  124.         End Try
  125.     End Sub
  126.  
  127.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
  128.         Dim sFile As String = PathTb.Text
  129.  
  130.         If File.Exists("C:\Program Files\Microsoft Office\Office12\XLVIEW.EXE") Then
  131.             Shell("C:\Program Files\Microsoft Office\Office12\XLVIEW.EXE /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  132.         Else
  133.             MsgBox("You Have Not Downloaded The Microsoft Excel Viewer. Select The Destination to Save the file." & vbNewLine & "Please install it after download")
  134.             Dim SFa As New SaveFileDialog
  135.  
  136.             SFa.Filter = "Executable Files|*.Exe"
  137.             SFa.ShowDialog()
  138.  
  139.             My.Computer.Network.DownloadFile("http://download.microsoft.com/download/e/a/9/ea913c8b-51a7-41b7-8697-9f0d0a7274aa/ExcelViewer.exe", SFa.FileName)
  140.  
  141.         End If
  142.     End Sub
  143.  
  144.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
  145.         Try
  146.             Dim sFile As String = PathTb.Text
  147.  
  148.             Dim oExcel As New Excel.Application
  149.             Dim oBooks As Excel.Workbooks = Nothing
  150.             Dim oWB As Workbook = Nothing
  151.  
  152.             'Start Excel and open the workbook.
  153.  
  154.             oExcel.Visible = True
  155.  
  156.             oBooks = oExcel.Workbooks
  157.  
  158.             oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlExtractData)
  159.  
  160.         Catch ex As Exception
  161.             MessageBox.Show(ex.Message)
  162.         End Try
  163.     End Sub
  164.  
  165.     Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  166.         Me.Close()
  167.     End Sub
  168.  
  169.     Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
  170.         Me.WindowState = FormWindowState.Minimized
  171.     End Sub
  172.  
  173.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  174.  
  175.     End Sub
  176.  
  177.  
  178.     Private allowCoolMove As Boolean = False
  179.     Private dx, dy As Integer 'I used this two integers as I could use the function new POint due to the Import of the Excel
  180.  
  181.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
  182.         allowCoolMove = True
  183.         dx = Cursor.Position.X - Me.Location.X '// get coordinates.
  184.         dy = Cursor.Position.Y - Me.Location.Y '// get coordinates.
  185.     End Sub
  186.  
  187.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  188.         If allowCoolMove = True Then
  189.             Dim cls As New Class1
  190.             Me.Location = cls.Pts(Cursor.Position.X - dx, Cursor.Position.Y - dy) '// set coordinates.
  191.         End If
  192.     End Sub
  193.  
  194.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
  195.         allowCoolMove = False
  196.     End Sub
  197.  
  198.     Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  199.  
  200.     End Sub
  201.  
  202.     Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  203.  
  204.     End Sub
  205.  
  206.     Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  207.  
  208.     End Sub
  209.  
  210.     Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
  211.  
  212.     End Sub
  213.  
  214.     Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
  215.  
  216.         Dim oExcel As Excel.ApplicationClass
  217.         Dim oBook As Excel.WorkbookClass
  218.         Dim oBooks As Excel.Workbooks
  219.         Dim sFile As String = PathTb.Text
  220.  
  221.         oExcel = CreateObject("Excel.application")
  222.         oExcel.Visible = True
  223.  
  224.         oBooks = oExcel.Workbooks
  225.         oBook = oBooks.Open(sFile)
  226.  
  227.         oExcel.Calculation = Excel.XlCalculation.xlCalculationManual
  228.     End Sub
  229.  
  230.     Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
  231.         Try
  232.             Dim sFile As String = PathTb.Text
  233.  
  234.             Dim oExcel As New Excel.Application
  235.             Dim oBooks As Excel.Workbooks = Nothing
  236.             Dim oWB As Workbook = Nothing
  237.  
  238.             'Start Excel and open the workbook.
  239.             If Path.GetExtension(sFile) = ".xls" Then
  240.                 oExcel.Visible = True
  241.                 oBooks = oExcel.Workbooks
  242.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  243.                 MsgBox("Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.")
  244.             Else
  245.                 oExcel.Visible = True
  246.                 oBooks = oExcel.Workbooks
  247.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  248.             End If
  249.  
  250.  
  251.         Catch ex As Exception
  252.             MessageBox.Show(ex.Message)
  253.         End Try
  254.     End Sub
  255.  
  256. End Class
  257.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement