Advertisement
socrtwo

Excel Recovery 1.0

Dec 11th, 2011
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 14.50 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.         Dim oExcel As Excel.ApplicationClass
  37.         Dim oBook As Excel.WorkbookClass
  38.         Dim oBooks As Excel.Workbooks
  39.  
  40.         Dim sFile As String = PathTb.Text
  41.  
  42.         'Start Excel and open the workbook.
  43.  
  44.         oExcel = CreateObject("Excel.application")
  45.         oExcel.Visible = True
  46.  
  47.         oBooks = oExcel.Workbooks
  48.         oBook = oBooks.Open(sFile)
  49.  
  50.         oExcel.Run("OpenAndRepairWorkbook")
  51.  
  52.         ' Clean-up: Close the workbook and quit Excel.
  53.         oBook.Close(False)
  54.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
  55.         oBook = Nothing
  56.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
  57.         oBooks = Nothing
  58.         oExcel.Quit()
  59.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
  60.         oExcel = Nothing
  61.  
  62.         GC.Collect()
  63.     End Sub
  64.  
  65.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  66.         Dim OFD As New OpenFileDialog
  67.         With OFD
  68.             .ShowDialog()
  69.             filename = .FileName
  70.             PathTb.Text = .FileName
  71.         End With
  72.     End Sub
  73.  
  74.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
  75.  
  76.         Dim oWord As Word.ApplicationClass
  77.         Dim sFile As String = PathTb.Text
  78.  
  79.         'Start Word and open the document.
  80.         If Path.GetExtension(sFile) = ".xlsx" Then
  81.             MsgBox("You can not open files with extension .xlsx in Word")
  82.         Else
  83.             oWord = CreateObject("Word.Application")
  84.             oWord.Visible = True
  85.             oWord.Documents.Open(sFile)
  86.         End If
  87.  
  88.     End Sub
  89.  
  90.  
  91.     Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
  92.         Dim sFile As String = PathTb.Text
  93.         Shell("EXCEL.EXE /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  94.     End Sub
  95.  
  96.     Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  97.         Try
  98.             Dim sFile As String = PathTb.Text
  99.  
  100.             Dim oExcel As New Excel.Application
  101.             Dim oBooks As Excel.Workbooks = Nothing
  102.             Dim oWB As Workbook = Nothing
  103.  
  104.             'Start Excel and open the workbook.
  105.             If Path.GetExtension(sFile) = ".xls" Then
  106.                 oExcel.Visible = True
  107.                 oBooks = oExcel.Workbooks
  108.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  109.                 MsgBox("Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.")
  110.             Else
  111.                 oExcel.Visible = True
  112.                 oBooks = oExcel.Workbooks
  113.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  114.             End If
  115.  
  116.  
  117.         Catch ex As Exception
  118.             MessageBox.Show(ex.Message)
  119.         End Try
  120.     End Sub
  121.  
  122.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
  123.         Dim sFile As String = PathTb.Text
  124.  
  125.         If File.Exists("C:\Program Files\Microsoft Office\Office12\XLVIEW.EXE") Then
  126.             Shell("XLVIEW.EXE /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  127.         Else
  128.             MsgBox("You Have Not Downloaded The Microsoft Excel Viewer. Select The Destination to Save the file." & vbNewLine & "Please install it after download")
  129.             Dim SFa As New SaveFileDialog
  130.  
  131.             SFa.Filter = "Executable Files|*.Exe"
  132.             SFa.ShowDialog()
  133.  
  134.             My.Computer.Network.DownloadFile("http://download.microsoft.com/download/e/a/9/ea913c8b-51a7-41b7-8697-9f0d0a7274aa/ExcelViewer.exe", SFa.FileName)
  135.  
  136.         End If
  137.     End Sub
  138.  
  139.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
  140.         Try
  141.             Dim sFile As String = PathTb.Text
  142.  
  143.             Dim oExcel As New Excel.Application
  144.             Dim oBooks As Excel.Workbooks = Nothing
  145.             Dim oWB As Workbook = Nothing
  146.  
  147.             'Start Excel and open the workbook.
  148.  
  149.             oExcel.Visible = True
  150.  
  151.             oBooks = oExcel.Workbooks
  152.  
  153.             oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlExtractData)
  154.  
  155.         Catch ex As Exception
  156.             MessageBox.Show(ex.Message)
  157.         End Try
  158.     End Sub
  159.  
  160.     Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  161.         Me.Close()
  162.     End Sub
  163.  
  164.     Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
  165.         Me.WindowState = FormWindowState.Minimized
  166.     End Sub
  167.  
  168.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  169.  
  170.     End Sub
  171.  
  172.  
  173.     Private allowCoolMove As Boolean = False
  174.     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
  175.  
  176.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
  177.         allowCoolMove = True
  178.         dx = Cursor.Position.X - Me.Location.X '// get coordinates.
  179.         dy = Cursor.Position.Y - Me.Location.Y '// get coordinates.
  180.     End Sub
  181.  
  182.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  183.         If allowCoolMove = True Then
  184.             Dim cls As New Class1
  185.             Me.Location = cls.Pts(Cursor.Position.X - dx, Cursor.Position.Y - dy) '// set coordinates.
  186.         End If
  187.     End Sub
  188.  
  189.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
  190.         allowCoolMove = False
  191.     End Sub
  192.  
  193.     Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  194.  
  195.     End Sub
  196.  
  197.     Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  198.  
  199.     End Sub
  200.  
  201.     Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  202.  
  203.     End Sub
  204.  
  205.     Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
  206.  
  207.     End Sub
  208.  
  209.     Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
  210.  
  211.         Dim oExcel As Excel.ApplicationClass
  212.         Dim oBook As Excel.WorkbookClass
  213.         Dim oBooks As Excel.Workbooks
  214.         Dim sFile As String = PathTb.Text
  215.  
  216.         oExcel = CreateObject("Excel.application")
  217.         oExcel.Visible = True
  218.  
  219.         oBooks = oExcel.Workbooks
  220.         oBook = oBooks.Open(sFile)
  221.         oExcel.Calculation = Excel.XlCalculation.xlCalculationManual
  222.  
  223.     End Sub
  224.  
  225.     Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
  226.         Try
  227.             Dim sFile As String = PathTb.Text
  228.  
  229.             Dim oExcel As New Excel.Application
  230.             Dim oBooks As Excel.Workbooks = Nothing
  231.             Dim oWB As Workbook = Nothing
  232.  
  233.             'Start Excel and open the workbook.
  234.             If Path.GetExtension(sFile) = ".xls" Then
  235.                 oExcel.Visible = True
  236.                 oBooks = oExcel.Workbooks
  237.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  238.                 MsgBox("Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.")
  239.             Else
  240.                 oExcel.Visible = True
  241.                 oBooks = oExcel.Workbooks
  242.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  243.             End If
  244.  
  245.  
  246.         Catch ex As Exception
  247.             MessageBox.Show(ex.Message)
  248.         End Try
  249.     End Sub
  250.  
  251.     Private Sub Label2_Click(sender As System.Object, e As System.EventArgs) Handles Label2.Click
  252.  
  253.     End Sub
  254.  
  255.     Private Sub PictureBox9_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox9.Click
  256.         Dim oExcel As Excel.ApplicationClass
  257.         Dim oBook As Excel.WorkbookClass
  258.  
  259.         Dim sFile As String = PathTb.Text
  260.  
  261.         oExcel = CreateObject("Excel.application")
  262.         oBook = oExcel.Workbooks.Add
  263.         oExcel.Visible = True
  264.         oBook.Activate()
  265.         oExcel.Calculation = Excel.XlCalculation.xlCalculationAutomatic
  266.         oExcel.Range("A1").Value = "=" & "'" & sFile & "'" & "!A1"
  267.  
  268.         Dim rRange As Range
  269.  
  270.         On Error Resume Next
  271.  
  272.         oExcel.DisplayAlerts = False
  273.  
  274.         rRange = oExcel.InputBox(Prompt:= _
  275.             "Please select a range similar in size to your corrupt data that you wish to recover.", _
  276.                 Title:="SPECIFY RANGE", Type:=8)
  277.  
  278.         On Error GoTo 0
  279.  
  280.         oExcel.DisplayAlerts = True
  281.  
  282.         If rRange Is Nothing Then
  283.  
  284.             Exit Sub
  285.  
  286.         Else
  287.             oExcel.Range("A1").Copy()
  288.             oBook.ActiveSheet.Paste(rRange)
  289.  
  290.         End If
  291.     End Sub
  292.  
  293.     Private Sub PictureBox10_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox10.Click
  294.         Dim oExcel As New Excel.Application
  295.         Dim oBooks As Excel.Workbooks = Nothing
  296.         Dim oBook As Workbook = Nothing
  297.         Dim oWSheet As Worksheet = Nothing
  298.         Dim sFile As String = PathTb.Text
  299.         Dim sFileName As String = Path.GetFileNameWithoutExtension(sFile)
  300.         Dim sDirName As String = Path.GetDirectoryName(sFile)
  301.         Dim sFileSylkName As String = sDirName & "\" & sFileName & ".slk"
  302.         'Start Excel and open the workbook.
  303.  
  304.         oExcel.Visible = False
  305.         oBooks = oExcel.Workbooks
  306.         oBook = oBooks.Open(Filename:=sFile)
  307.         oWSheet = oBook.ActiveSheet()
  308.         oWSheet.SaveAs(Filename:=sFileSylkName, FileFormat:=Excel.XlFileFormat.xlSYLK)
  309.         If File.Exists(sFileSylkName) Then
  310.             oBook.Close()
  311.             oExcel.Visible = True
  312.             oBook = oBooks.Open(Filename:=sFileSylkName)
  313.         Else
  314.             MessageBox.Show("Failed to create " & sFileSylkName)
  315.  
  316.         End If
  317.  
  318.     End Sub
  319.  
  320.     Private Sub Label5_Click(sender As System.Object, e As System.EventArgs) Handles Label5.Click
  321.  
  322.     End Sub
  323.  
  324.     Private Sub PictureBox11_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox11.Click
  325.         Dim sFile As String = PathTb.Text
  326.         Shell("C:\Program Files\Windows NT\Accessories\wordpad.exe" + " """ + sFile + """", AppWinStyle.NormalFocus)
  327.  
  328.         Dim regVersion As Microsoft.Win32.RegistryKey
  329.         regVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\wordpad.exe", True)
  330.         If regVersion IsNot Nothing Then
  331.             Dim proc As New Process
  332.             With proc.StartInfo
  333.                 .FileName = regVersion.GetValue("").ToString
  334.                 .Arguments = sFile
  335.             End With
  336.             proc.Start()
  337.         End If
  338.  
  339.     End Sub
  340.  
  341.     Private Sub PictureBox12_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox12.Click
  342.         Dim oExcel As New Excel.Application
  343.         Dim oBooks As Excel.Workbooks = Nothing
  344.         Dim oBook As Workbook = Nothing
  345.         Dim oWSheet As Worksheet = Nothing
  346.         Dim oChart As Chart = Nothing
  347.         Dim sFile As String = PathTb.Text
  348.         Dim NumberOfRows As Integer
  349.         Dim X As Object
  350.         Dim Counter As Integer = 2
  351.         oExcel.Visible = True
  352.         oBooks = oExcel.Workbooks
  353.         oBook = oBooks.Open(Filename:=sFile)
  354.         oWSheet = oBook.Worksheets.Add()
  355.         oWSheet.Name = "ChartData"
  356.         oWSheet.Activate()
  357.         MsgBox("Select the chart you wish to extract data from.")
  358.         oChart = oBook.ActiveChart
  359.         ' Calculate the number of rows of data.
  360.         NumberOfRows = UBound(oChart.SeriesCollection(1).Values)
  361.  
  362.         oWSheet.Cells(1, 1) = "X Values"
  363.  
  364.         ' Write x-axis values to worksheet.
  365.         With oWSheet
  366.             .Range(.Cells(2, 1), _
  367.             .Cells(NumberOfRows + 1, 1)).Value = _
  368.             oExcel.WorksheetFunction.Transpose(oChart.SeriesCollection(1).XValues)
  369.         End With
  370.  
  371.         ' Loop through all series in the chart and write their values to
  372.         ' the worksheet.
  373.         For Each X In oChart.SeriesCollection
  374.             oWSheet.Cells(1, Counter) = X.Name
  375.  
  376.             With oWSheet
  377.                 .Range(.Cells(2, Counter), _
  378.                 .Cells(NumberOfRows + 1, Counter)).Value = _
  379.                 oExcel.WorksheetFunction.Transpose(X.Values)
  380.             End With
  381.  
  382.             Counter = Counter + 1
  383.         Next
  384.     End Sub
  385.  
  386.     Private Sub releaseObject(ByVal obj As Object)
  387.         Try
  388.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  389.             obj = Nothing
  390.         Catch ex As Exception
  391.             obj = Nothing
  392.         Finally
  393.             GC.Collect()
  394.         End Try
  395.     End Sub
  396.  
  397.     Private Sub Label11_Click(sender As System.Object, e As System.EventArgs) Handles Label11.Click
  398.  
  399.     End Sub
  400.  
  401.     Private Sub Button1_Click_2(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  402.         Alphaleonis.Win32.Vss.VssImplementation.Equals()
  403.     End Sub
  404. End Class
  405.  
  406.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement