Advertisement
socrtwo

Excel Recovery New Version

Jan 4th, 2012
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit On
  2. Imports Excel = Microsoft.Office.Interop.Excel
  3. Imports Word = Microsoft.Office.Interop.Word
  4. Imports System.IO
  5. Imports System.Collections.Generic
  6. Imports Microsoft.Office.Interop.Excel
  7. Imports System.Drawing
  8. Imports System.Text.RegularExpressions
  9. Imports System.ComponentModel
  10. Imports Microsoft.Win32
  11.  
  12. Public Class Form1
  13.  
  14.     Dim filename As String
  15.  
  16. #Region "Functions"
  17.  
  18.     Public Function SaveTextToFile(ByVal strData As String, _
  19.      ByVal FullPath As String, _
  20.        Optional ByVal ErrInfo As String = "") As Boolean
  21.  
  22.  
  23.         Dim bAns As Boolean = False
  24.         Dim objReader As StreamWriter
  25.         Try
  26.  
  27.  
  28.             objReader = New StreamWriter(FullPath)
  29.             objReader.Write(strData)
  30.             objReader.Close()
  31.             bAns = True
  32.         Catch Ex As Exception
  33.             ErrInfo = Ex.Message
  34.  
  35.         End Try
  36.         Return bAns
  37.     End Function
  38.     Public Function DelFromRight(ByVal sChars As String, ByVal sLine As String) As String
  39.         'Removes unwanted characters from right of given string
  40.         ' EXAMPLE
  41.         '  MsgBox DelFromRight(" TEST", "THIS IS A TEST")
  42.         'displays "THIS IS A"
  43.  
  44.  
  45.  
  46.         sLine = ReverseString(sLine)
  47.         sChars = ReverseString(sChars)
  48.         sLine = DelFromLeft(sChars, sLine)
  49.         DelFromRight = ReverseString(sLine)
  50.         Exit Function
  51.  
  52.  
  53.     End Function
  54.  
  55.     Public Function DelFromLeft(ByVal sChars As String, _
  56.             ByVal sLine As String) As String
  57.  
  58.         ' Removes unwanted characters from left of given string
  59.         '  EXAMPLE
  60.         '      MsgBox DelFromLeft("THIS", "THIS IS A TEST")
  61.         '        displays  "IS A TEST"
  62.  
  63.  
  64.         Dim iCount As Integer
  65.         Dim sChar As String
  66.  
  67.         DelFromLeft = ""
  68.         ' Remove unwanted characters to left of folder name
  69.         If InStr(sLine, sChars) > 0 Then
  70.             For iCount = 1 To Len(sChars)
  71.                 ' Retrieve character from start string to
  72.                 'look for in folder string (sLine)
  73.                 sChar = Mid$(sChars, iCount, 1)
  74.                 ' Remove all characters to left of found string
  75.                 sLine = Mid$(sLine, InStr(sLine, sChar) + 1)
  76.  
  77.             Next iCount
  78.         End If
  79.         DelFromLeft = sLine
  80.         Exit Function
  81.  
  82.     End Function
  83.  
  84.     Public Function ReverseString(ByVal InputString As String) _
  85.       As String
  86.  
  87.         'If you have vb6, you can use
  88.         'StrReverse instead of this function
  89.  
  90.         Dim lLen As Long, lCtr As Long
  91.         Dim sChar As String
  92.         Dim sAns As String = ""
  93.  
  94.         lLen = Len(InputString)
  95.         For lCtr = lLen To 1 Step -1
  96.             sChar = Mid(InputString, lCtr, 1)
  97.             sAns = sAns & sChar
  98.         Next
  99.  
  100.         ReverseString = sAns
  101.  
  102.     End Function
  103.     Private Function ReadExeFromResources(ByVal filename As String) As Byte()
  104.         Dim CurrentAssembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()
  105.         Dim Resource As String = String.Empty
  106.         Dim ArrResources As String() = CurrentAssembly.GetManifestResourceNames()
  107.         For Each Resource In ArrResources
  108.             If Resource.IndexOf(filename) > -1 Then Exit For
  109.         Next
  110.         Dim ResourceStream As IO.Stream = CurrentAssembly.GetManifestResourceStream(Resource)
  111.         If ResourceStream Is Nothing Then
  112.             Return Nothing
  113.         End If
  114.         Dim ResourcesBuffer(CInt(ResourceStream.Length) - 1) As Byte
  115.         ResourceStream.Read(ResourcesBuffer, 0, ResourcesBuffer.Length)
  116.         ResourceStream.Close()
  117.         Return ResourcesBuffer
  118.     End Function
  119.  
  120.  
  121. #End Region
  122.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  123.  
  124.         Dim oExcel As Excel.Application
  125.         Dim oBook As Excel.Workbook
  126.         Dim oBooks As Excel.Workbooks
  127.  
  128.         Dim sFile As String = PathTb.Text
  129.  
  130.         'Start Excel and open the workbook.
  131.  
  132.         oExcel = CreateObject("Excel.application")
  133.         oExcel.Visible = True
  134.  
  135.         oBooks = oExcel.Workbooks
  136.         oBook = oBooks.Open(sFile)
  137.  
  138.         oExcel.Run("OpenAndRepairWorkbook")
  139.  
  140.         ' Clean-up: Close the workbook and quit Excel.
  141.         oBook.Close(False)
  142.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
  143.         oBook = Nothing
  144.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
  145.         oBooks = Nothing
  146.         oExcel.Quit()
  147.         System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
  148.         oExcel = Nothing
  149.  
  150.         GC.Collect()
  151.     End Sub
  152.     Dim counterVariable As Integer
  153.     Dim previousVersionCounterVariable As Integer
  154.     Dim sFileShadowPath As String
  155.     Dim sFileShadowName As String
  156.     Dim sFileShadowPathDate As String
  157.     Dim matchCount As Integer
  158.     Dim shadowLinkFolderName As New List(Of String)
  159.     Dim nonErrorShadowPathList As New List(Of String)
  160.     Dim comboBoxIndex As Integer = 0
  161.     Dim preVersionHashTable As New Hashtable
  162.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  163.         Try
  164.             Dim OFD As New OpenFileDialog
  165.             With OFD
  166.  
  167.                 .ShowDialog()
  168.                 filename = .FileName
  169.                 PathTb.Text = .FileName
  170.             End With
  171.             Dim sFile As String = PathTb.Text
  172.             shadowLinkFolderName.Clear()
  173.             nonErrorShadowPathList.Clear()
  174.             ComboBox1.Items.Clear()
  175.             'Find out the number of vss shadow snapshots (restore
  176.             'points). All shadows apparently have a linkable path
  177.             '\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy#,
  178.             'where # is a simple one or two or three digit integer.
  179.  
  180.             Dim objProcess As New Process()
  181.             objProcess.StartInfo.UseShellExecute = False
  182.             objProcess.StartInfo.RedirectStandardOutput = True
  183.             objProcess.StartInfo.CreateNoWindow = True
  184.             objProcess.StartInfo.RedirectStandardError = True
  185.             objProcess.StartInfo.FileName() = "vssadmin"
  186.             objProcess.StartInfo.Arguments() = "List Shadows"
  187.             objProcess.Start()
  188.  
  189.             Dim vssadminOutput As String = objProcess.StandardOutput.ReadToEnd
  190.             Dim strError As String = objProcess.StandardError.ReadToEnd()
  191.             objProcess.WaitForExit()
  192.  
  193.             ' Call Regex.Matches method.
  194.             Dim matches As MatchCollection = Regex.Matches(vssadminOutput, _
  195.                 "\\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy[0-9]+")
  196.             counterVariable = 0
  197.             matchCount = matches.Count
  198.             MsgBox("Please wait while Excel Recovery searches for previous versions of your file.")
  199.             ' Loop over matches.
  200.             For Each m As Match In matches
  201.                 Dim driveLetter As String = sFile.Substring(0, 2)
  202.                 shadowLinkFolderName.Add("C:\" & DelFromLeft( _
  203.                     "\\?\GLOBALROOT\Device\HarddiskVolume", (m.ToString())))
  204.                 sFileShadowPath = (shadowLinkFolderName(counterVariable) & DelFromLeft( _
  205.                     driveLetter, sFile))
  206.  
  207.                 'Here I create temporary folders off the C:
  208.                 'drive which are mapped to each snapshot.
  209.                 Dim myProcess As New Process()
  210.                 myProcess.StartInfo.FileName = "cmd.exe"
  211.                 myProcess.StartInfo.UseShellExecute = False
  212.                 myProcess.StartInfo.RedirectStandardInput = True
  213.                 myProcess.StartInfo.RedirectStandardOutput = True
  214.                 myProcess.StartInfo.CreateNoWindow = True
  215.                 myProcess.Start()
  216.                 Dim myStreamWriter As StreamWriter = myProcess.StandardInput
  217.  
  218.                 myStreamWriter.WriteLine("mklink /d " & (shadowLinkFolderName(counterVariable).ToString) _
  219.                                         & " " & (m.ToString()) & "\")
  220.                 myStreamWriter.Close()
  221.                 myProcess.WaitForExit()
  222.                 myProcess.Close()
  223.  
  224.                 'Here I compare our recovery target file against the shadow
  225.                 'copies. One shadow file copy is compared for each iteration
  226.                 'of the loop. If the string "no difference encountered is found"
  227.                 'then I know this shadow copy of the file is not worth looking
  228.                 'at, as it is the same as the recovery target.
  229.                 Dim fileCompare As New Process()
  230.                 fileCompare.StartInfo.FileName = "cmd.exe"
  231.                 fileCompare.StartInfo.UseShellExecute = False
  232.                 fileCompare.StartInfo.RedirectStandardInput = True
  233.                 fileCompare.StartInfo.RedirectStandardOutput = True
  234.                 fileCompare.StartInfo.CreateNoWindow = True
  235.                 fileCompare.Start()
  236.                 Dim fileCompareWriter As StreamWriter = fileCompare.StandardInput
  237.  
  238.                 fileCompareWriter.WriteLine("fc """ & sFile & """ """ _
  239.                                 & sFileShadowPath & """")
  240.                 fileCompareWriter.Dispose()
  241.                 fileCompareWriter.Close()
  242.                 Dim fileCompareReader As StreamReader = fileCompare.StandardOutput
  243.                 Dim fileCompareOut As String = fileCompareReader.ReadToEnd
  244.                 fileCompareReader.Close()
  245.                 fileCompare.WaitForExit()
  246.                 fileCompare.Close()
  247.                 Dim fileCompareBoolean As Boolean = fileCompareOut.Contains( _
  248.                                 "no differences encountered").ToString
  249.                 Dim fileCompBooleanError As Boolean = fileCompareOut.Contains( _
  250.                                 "FC: cannot open").ToString
  251.                 If fileCompBooleanError = "True" Then
  252.                     counterVariable = counterVariable + 1
  253.                     Continue For
  254.                 End If
  255.  
  256.                 If fileCompareBoolean = "True" Then
  257.                     counterVariable = counterVariable + 1
  258.                     Continue For
  259.                 End If
  260.                 'Here I take a positive result of a file difference between
  261.                 'the target and the shadow copy, and I write it out to a combo
  262.                 'box on the form, so it can be chosen. I also only keep the
  263.                 'first instance of a different shadow file as the others are
  264.                 'identical. I distinguish if they are the same by date.
  265.  
  266.  
  267.                 Dim sFileShadowPathInfo As New FileInfo(sFileShadowPath)
  268.                 sFileShadowPathDate = sFileShadowPathInfo.LastWriteTime
  269.                 sFileShadowName = sFileShadowPathInfo.Name
  270.  
  271.                 If ComboBox1.Items.Count = 0 Then
  272.                     ComboBox1.Items.Add("File Name: " & sFileShadowName _
  273.                                                             & " Last Modified: " & sFileShadowPathDate)
  274.                     nonErrorShadowPathList.Add(sFileShadowPath)
  275.                     preVersionHashTable.Add(sFileShadowPathDate, sFileShadowPath)
  276.                     counterVariable = counterVariable + 1
  277.                     Continue For
  278.                 End If
  279.  
  280.                 previousVersionCounterVariable = ComboBox1.Items.Count - 1
  281.                 Dim previoussFileShadowPath As String
  282.                 previoussFileShadowPath = nonErrorShadowPathList(previousVersionCounterVariable)
  283.                 Dim prevsFileShadowPathInfo As New FileInfo(previoussFileShadowPath)
  284.                 Dim prevsFileShadowPathDate As String = prevsFileShadowPathInfo.LastWriteTime
  285.  
  286.                 If String.Equals(sFileShadowPathDate, prevsFileShadowPathDate) Then
  287.                     counterVariable = counterVariable + 1
  288.                     Continue For
  289.                 Else
  290.                     ComboBox1.Items.Add("File Name: " & sFileShadowName _
  291.                                                             & " Last Modified: " & sFileShadowPathDate)
  292.                     nonErrorShadowPathList.Add(sFileShadowPath)
  293.                     preVersionHashTable.Add(sFileShadowPathDate, sFileShadowPath)
  294.                     counterVariable = counterVariable + 1
  295.                     Continue For
  296.                 End If
  297.  
  298.             Next m
  299.             MsgBox("Processing has finished and should have returned previous versions, if they exist.")
  300.         Catch ex As Exception
  301.             MessageBox.Show(ex.Message)
  302.         End Try
  303.     End Sub
  304.  
  305.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox3.Click
  306.         Try
  307.             Dim TargetKey As RegistryKey
  308.             TargetKey = Registry.ClassesRoot.OpenSubKey("word.application")
  309.  
  310.             If TargetKey Is Nothing Then
  311.                 MsgBox("Opening an Excel file in Word requires Word, however it does not appear to be installed.")
  312.                 Exit Sub
  313.  
  314.             Else
  315.                 'key is found
  316.                 TargetKey.Close()
  317.                 Dim oWord As Word.Application
  318.                 Dim sFile As String = PathTb.Text
  319.  
  320.                 'Start Word and open the document.
  321.                 If Path.GetExtension(sFile) = ".xlsx" Then
  322.                     MsgBox("You can not open files with extension .xlsx in Word.")
  323.                 Else
  324.                     oWord = CreateObject("Word.Application")
  325.                     oWord.Visible = True
  326.                     oWord.Documents.Open(sFile)
  327.                 End If
  328.  
  329.             End If
  330.  
  331.         Catch ex As Exception
  332.             MessageBox.Show(ex.Message)
  333.         End Try
  334.     End Sub
  335.  
  336.  
  337.     Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
  338.         Try
  339.             Dim excelPath As String = _
  340.                 Registry.GetValue( _
  341.                 "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", _
  342.                 "Path", "Key does not exist")
  343.  
  344.             Dim TargetKey As RegistryKey
  345.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  346.  
  347.             If TargetKey Is Nothing Then
  348.                 MsgBox("The Open in Safe Mode method requires Excel, however it does not appear to be installed.")
  349.                 Exit Sub
  350.  
  351.             Else    'key is found
  352.                 TargetKey.Close()
  353.                 Dim sFile As String = PathTb.Text
  354.                 Shell(excelPath & "excel.exe /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  355.             End If
  356.  
  357.         Catch ex As Exception
  358.             MessageBox.Show(ex.Message)
  359.         End Try
  360.     End Sub
  361.  
  362.     Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  363.         Try
  364.             Dim sFile As String = PathTb.Text
  365.  
  366.             Dim oExcel As New Excel.Application
  367.             Dim oBooks As Excel.Workbooks = Nothing
  368.             Dim oWB As Workbook = Nothing
  369.  
  370.             'Start Excel and open the workbook.
  371.             If Path.GetExtension(sFile) = ".xls" Then
  372.                 oExcel.Visible = True
  373.                 oBooks = oExcel.Workbooks
  374.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  375.                 MsgBox("Excel completed file level validation and repair. Some " _
  376.                 & "parts of this workbook may have been repaired or discarded.")
  377.             Else
  378.                 oExcel.Visible = True
  379.                 oBooks = oExcel.Workbooks
  380.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  381.             End If
  382.  
  383.  
  384.         Catch ex As Exception
  385.             MessageBox.Show(ex.Message)
  386.         End Try
  387.     End Sub
  388.  
  389.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox4.Click
  390.         Try
  391.             Dim sFile As String = PathTb.Text
  392.  
  393.             If File.Exists("C:\Program Files\Microsoft Office\Office12\XLVIEW.EXE") Then
  394.                 Shell("C:\Program Files\Microsoft Office\Office12\XLVIEW.EXE /s /r " & """" & sFile & """", AppWinStyle.NormalFocus)
  395.             Else
  396.                 MsgBox("You have not downloaded the most recent Microsoft Excel Viewer. " _
  397.                       & "Please install after downloading and try clicking this button again.")
  398.                 System.Diagnostics.Process.Start("http://www.microsoft.com/download/en/details.aspx?id=10")
  399.  
  400.             End If
  401.  
  402.         Catch ex As Exception
  403.             MessageBox.Show(ex.Message)
  404.         End Try
  405.     End Sub
  406.  
  407.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
  408.         Try
  409.             Dim TargetKey As RegistryKey
  410.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  411.  
  412.             If TargetKey Is Nothing Then
  413.                 MsgBox("The Open and Extract Data method requires Excel, however it does not appear to be installed.")
  414.                 Exit Sub
  415.  
  416.             Else    'key is found
  417.                 TargetKey.Close()
  418.  
  419.                 Dim sFile As String = PathTb.Text
  420.                 Dim oExcel As New Excel.Application
  421.                 Dim oBooks As Excel.Workbooks = Nothing
  422.                 Dim oWB As Workbook = Nothing
  423.  
  424.                 'Start Excel and open the workbook.
  425.  
  426.                 oExcel.Visible = True
  427.                 oBooks = oExcel.Workbooks
  428.                 oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlExtractData)
  429.  
  430.             End If
  431.  
  432.         Catch ex As Exception
  433.                 MessageBox.Show(ex.Message)
  434.         End Try
  435.     End Sub
  436.  
  437.     Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
  438.         Try
  439.             Dim x As Integer
  440.  
  441.             For x = 0 To shadowLinkFolderName.Count - 1
  442.  
  443.                 Dim myProcess As New Process()
  444.                 myProcess.StartInfo.FileName = "cmd.exe"
  445.                 myProcess.StartInfo.UseShellExecute = False
  446.                 myProcess.StartInfo.RedirectStandardInput = True
  447.                 myProcess.StartInfo.RedirectStandardOutput = True
  448.                 myProcess.StartInfo.CreateNoWindow = True
  449.                 myProcess.Start()
  450.                 Dim myStreamWriter As StreamWriter = myProcess.StandardInput
  451.  
  452.                 myStreamWriter.WriteLine("rmdir " & shadowLinkFolderName(x))
  453.                 myStreamWriter.Close()
  454.                 myProcess.WaitForExit()
  455.                 myProcess.Close()
  456.  
  457.             Next
  458.  
  459.             Me.Close()
  460.  
  461.         Catch ex As Exception
  462.             MessageBox.Show(ex.Message)
  463.         End Try
  464.  
  465.     End Sub
  466.  
  467.     Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
  468.         Me.WindowState = FormWindowState.Minimized
  469.     End Sub
  470.  
  471.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  472.  
  473.     End Sub
  474.  
  475.  
  476.     Private allowCoolMove As Boolean = False
  477.     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
  478.  
  479.     Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
  480.         allowCoolMove = True
  481.         dx = Cursor.Position.X - Me.Location.X '// get coordinates.
  482.         dy = Cursor.Position.Y - Me.Location.Y '// get coordinates.
  483.     End Sub
  484.  
  485.     Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
  486.         If allowCoolMove = True Then
  487.             Dim cls As New Class1
  488.             Me.Location = cls.Pts(Cursor.Position.X - dx, Cursor.Position.Y - dy) '// set coordinates.
  489.         End If
  490.     End Sub
  491.  
  492.     Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
  493.         allowCoolMove = False
  494.     End Sub
  495.  
  496.     Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  497.  
  498.     End Sub
  499.  
  500.     Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  501.  
  502.     End Sub
  503.  
  504.     Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  505.  
  506.     End Sub
  507.  
  508.     Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
  509.  
  510.     End Sub
  511.  
  512.     Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox5.Click
  513.         Try
  514.             Dim TargetKey As RegistryKey
  515.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  516.  
  517.             If TargetKey Is Nothing Then
  518.                 MsgBox("Opening the Excel file with calculations set to manual method requires Excel, however it does not appear to be installed.")
  519.                 Exit Sub
  520.  
  521.             Else
  522.                 'key is found
  523.                 TargetKey.Close()
  524.                 Dim oExcel As Excel.Application
  525.                 Dim oBook As Excel.Workbook
  526.                 Dim oBooks As Excel.Workbooks
  527.                 Dim sFile As String = PathTb.Text
  528.                 oExcel = CreateObject("Excel.application")
  529.                 oExcel.Visible = True
  530.  
  531.                 oBooks = oExcel.Workbooks
  532.                 oBook = oBooks.Open(sFile)
  533.                 oExcel.Calculation = Excel.XlCalculation.xlCalculationManual
  534.             End If
  535.  
  536.         Catch ex As Exception
  537.             MessageBox.Show(ex.Message)
  538.         End Try
  539.     End Sub
  540.  
  541.     Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox6.Click
  542.         Try
  543.             Dim TargetKey As RegistryKey
  544.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  545.  
  546.             If TargetKey Is Nothing Then
  547.                 MsgBox("The Open and Repair method requires Excel, however it does not appear to be installed.")
  548.                 Exit Sub
  549.  
  550.             Else
  551.                 'key is found
  552.                 TargetKey.Close()
  553.                 Dim sFile As String = PathTb.Text
  554.  
  555.                 Dim oExcel As New Excel.Application
  556.                 Dim oBooks As Excel.Workbooks = Nothing
  557.                 Dim oWB As Workbook = Nothing
  558.  
  559.                 'Start Excel and open the workbook.
  560.                 If Path.GetExtension(sFile) = ".xls" Then
  561.                     oExcel.Visible = True
  562.                     oBooks = oExcel.Workbooks
  563.                     oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  564.                     MsgBox("Excel completed file level validation and repair. Some " _
  565.                     & "parts of this workbook may have been repaired or discarded.")
  566.                 Else
  567.                     oExcel.Visible = True
  568.                     oBooks = oExcel.Workbooks
  569.                     oWB = oBooks.Open(Filename:=sFile, CorruptLoad:=XlCorruptLoad.xlRepairFile)
  570.                 End If
  571.  
  572.             End If
  573.         Catch ex As Exception
  574.             MessageBox.Show(ex.Message)
  575.         End Try
  576.     End Sub
  577.  
  578.     Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
  579.  
  580.     End Sub
  581.  
  582.     Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox9.Click
  583.         Try
  584.             Dim TargetKey As RegistryKey
  585.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  586.  
  587.             If TargetKey Is Nothing Then
  588.                 MsgBox("The External References method requires Excel, however it does not appear to be installed.")
  589.                 Exit Sub
  590.  
  591.             Else
  592.                 'key is found
  593.                 TargetKey.Close()
  594.                 Dim oExcel As Excel.Application
  595.                 Dim oBook As Excel.Workbook
  596.                 Dim sFile As String = PathTb.Text
  597.  
  598.                 oExcel = CreateObject("Excel.application")
  599.                 oBook = oExcel.Workbooks.Add
  600.                 oExcel.Visible = True
  601.                 oBook.Activate()
  602.                 oExcel.Calculation = Excel.XlCalculation.xlCalculationAutomatic
  603.                 oExcel.Range("A1").Value = "=" & "'" & sFile & "'" & "!A1"
  604.  
  605.                 Dim rRange As Range
  606.                 oExcel.DisplayAlerts = False
  607.                 rRange = oExcel.InputBox(Prompt:= _
  608.                     "Please select a range similar in size to your corrupt data " _
  609.                     & "that you wish to recover.", Title:="SPECIFY RANGE", Type:=8)
  610.                 oExcel.DisplayAlerts = True
  611.  
  612.                 If rRange Is Nothing Then
  613.  
  614.                     Exit Sub
  615.  
  616.                 Else
  617.                     oExcel.Range("A1").Copy()
  618.                     oBook.ActiveSheet.Paste(rRange)
  619.  
  620.                 End If
  621.             End If
  622.  
  623.         Catch ex As Exception
  624.             MessageBox.Show(ex.Message)
  625.         End Try
  626.     End Sub
  627.  
  628.     Private Sub PictureBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox10.Click
  629.         Try
  630.             Dim TargetKey As RegistryKey
  631.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  632.  
  633.             If TargetKey Is Nothing Then
  634.                 MsgBox("The save to SYLK format method requires Excel, however it does not appear to be installed.")
  635.                 Exit Sub
  636.  
  637.             Else
  638.                 'key is found
  639.                 TargetKey.Close()
  640.                 Dim oExcel As New Excel.Application
  641.                 Dim oBooks As Excel.Workbooks = Nothing
  642.                 Dim oBook As Workbook = Nothing
  643.                 Dim oWSheet As Worksheet = Nothing
  644.                 Dim sFile As String = PathTb.Text
  645.                 Dim sFileName As String = Path.GetFileNameWithoutExtension(sFile)
  646.                 Dim sDirName As String = Path.GetDirectoryName(sFile)
  647.                 Dim sFileSylkName As String = sDirName & "\" & sFileName & ".slk"
  648.  
  649.                 'Start Excel and open the workbook.
  650.                 oExcel.Visible = False
  651.                 oBooks = oExcel.Workbooks
  652.                 oBook = oBooks.Open(Filename:=sFile)
  653.                 oWSheet = oBook.ActiveSheet()
  654.                 oWSheet.SaveAs(Filename:=sFileSylkName, FileFormat:=Excel.XlFileFormat.xlSYLK)
  655.  
  656.                 If File.Exists(sFileSylkName) Then
  657.                     oBook.Close()
  658.                     oExcel.Visible = True
  659.                     oBook = oBooks.Open(Filename:=sFileSylkName)
  660.                 Else
  661.                     MessageBox.Show("Failed to create " & sFileSylkName)
  662.                 End If
  663.  
  664.             End If
  665.  
  666.         Catch ex As Exception
  667.             MessageBox.Show(ex.Message)
  668.         End Try
  669.     End Sub
  670.  
  671.     Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
  672.  
  673.     End Sub
  674.  
  675.     Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox11.Click
  676.         Try
  677.             Dim sFile As String = PathTb.Text
  678.             Dim regVersion As Microsoft.Win32.RegistryKey
  679.             regVersion = Microsoft.Win32.Registry.LocalMachine.OpenSubKey( _
  680.                 "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\wordpad.exe", True)
  681.                 If regVersion IsNot Nothing Then
  682.                     Dim proc As New Process
  683.                     With proc.StartInfo
  684.                         .FileName = regVersion.GetValue("").ToString
  685.                         .Arguments = sFile
  686.                     End With
  687.                     proc.Start()
  688.                 End If
  689.  
  690.  
  691.         Catch ex As Exception
  692.             MessageBox.Show(ex.Message)
  693.         End Try
  694.     End Sub
  695.  
  696.     Private Sub PictureBox12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  697.         Try
  698.             Dim TargetKey As RegistryKey
  699.             TargetKey = Registry.ClassesRoot.OpenSubKey("excel.application")
  700.  
  701.             If TargetKey Is Nothing Then
  702.                 MsgBox("The Macro Graph Data Recovery method requires Excel, however it does not appear to be installed.")
  703.                 Exit Sub
  704.  
  705.             Else
  706.                 'key is found
  707.                 TargetKey.Close()
  708.                 Dim oExcel As New Excel.Application
  709.                 Dim oBooks As Excel.Workbooks = Nothing
  710.                 Dim oBook As Workbook = Nothing
  711.                 Dim oWSheet As Worksheet = Nothing
  712.                 Dim oChart As Chart = Nothing
  713.                 Dim sFile As String = PathTb.Text
  714.                 Dim NumberOfRows As Integer
  715.                 Dim X As Object
  716.                 Dim Counter As Integer = 2
  717.  
  718.                 oExcel.Visible = True
  719.                 oBooks = oExcel.Workbooks
  720.                 oBook = oBooks.Open(Filename:=sFile)
  721.                 oWSheet = oBook.Worksheets.Add()
  722.                 oWSheet.Name = "ChartData"
  723.                 oWSheet.Activate()
  724.                 MsgBox("Select the chart you wish to extract data from.")
  725.                 oChart = oBook.ActiveChart
  726.  
  727.                 ' Calculate the number of rows of data.
  728.                 NumberOfRows = UBound(oChart.SeriesCollection(1).Values)
  729.                 oWSheet.Cells(1, 1) = "X Values"
  730.  
  731.                 ' Write x-axis values to worksheet.
  732.                 With oWSheet
  733.                     .Range(.Cells(2, 1), _
  734.                     .Cells(NumberOfRows + 1, 1)).Value = _
  735.                     oExcel.WorksheetFunction.Transpose(oChart.SeriesCollection(1).XValues)
  736.                 End With
  737.  
  738.                 ' Loop through all series in the chart  
  739.                 ' and write their values to the worksheet.
  740.                 For Each X In oChart.SeriesCollection
  741.                     oWSheet.Cells(1, Counter) = X.Name
  742.  
  743.                     With oWSheet
  744.                         .Range(.Cells(2, Counter), _
  745.                         .Cells(NumberOfRows + 1, Counter)).Value = _
  746.                         oExcel.WorksheetFunction.Transpose(X.Values)
  747.                     End With
  748.  
  749.                     Counter = Counter + 1
  750.                 Next
  751.             End If
  752.  
  753.         Catch ex As Exception
  754.             MessageBox.Show(ex.Message)
  755.         End Try
  756.     End Sub
  757.  
  758.     Private Sub releaseObject(ByVal obj As Object)
  759.         Try
  760.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
  761.             obj = Nothing
  762.         Catch ex As Exception
  763.             obj = Nothing
  764.         Finally
  765.             GC.Collect()
  766.         End Try
  767.     End Sub
  768.  
  769.     Private Sub Label11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  770.  
  771.     End Sub
  772.     Dim saveShadowPath As String
  773.     Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  774.         Try
  775.             Dim saveFileDialog1 As New SaveFileDialog()
  776.             saveFileDialog1.Filter = "Excel Files (*.xls;*.xlsx;*.xlt;*.xla;" _
  777.                 & "*.xlsm;*.xltx;*.xltm;*.xlsb;*.xlam)|*.xls;*.xlsx;*.xlt;*.xla;" _
  778.                 & "*.xlsm;*.xltx;*.xltm;*.xlsb;*.xlam|All Files (*.*)|*.*"
  779.             saveFileDialog1.FilterIndex = 1
  780.             saveFileDialog1.RestoreDirectory = True
  781.  
  782.             If saveFileDialog1.ShowDialog() = DialogResult.OK Then
  783.                 saveShadowPath = saveFileDialog1.FileName
  784.  
  785.                 If System.IO.File.Exists(selectedPreviousVersion) = True Then
  786.                     System.IO.File.Copy(selectedPreviousVersion, saveShadowPath, True)
  787.                     MsgBox(("The Previous version of " & sFileShadowName _
  788.                                   & " last modified on " & selectedsFileShadowPathDate _
  789.                                   & " was saved to a new location: " & saveShadowPath) & ".")
  790.                 Else
  791.                     MsgBox("Can't connect to previous version file.")
  792.                 End If
  793.             End If
  794.         Catch ex As Exception
  795.             MessageBox.Show(ex.Message)
  796.         End Try
  797.     End Sub
  798.     Dim selectedsFileShadowPathDate As String
  799.     Dim selectedPreviousVersion As String
  800.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  801.         Try
  802.             Dim sFileShadowPathInfo As New FileInfo(sFileShadowPath)
  803.             sFileShadowPathDate = sFileShadowPathInfo.LastWriteTime
  804.             sFileShadowName = sFileShadowPathInfo.Name
  805.             selectedsFileShadowPathDate = DelFromLeft("File Name: " & sFileShadowName _
  806.                             & " Last Modified: ", ComboBox1.Text.ToString)
  807.             selectedPreviousVersion = preVersionHashTable.Item(selectedsFileShadowPathDate)
  808.         Catch ex As Exception
  809.             MessageBox.Show(ex.Message)
  810.         End Try
  811.     End Sub
  812.  
  813.     Private Sub Label12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label12.Click
  814.  
  815.     End Sub
  816.  
  817.     Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
  818.  
  819.     End Sub
  820.  
  821.     Private Sub PictureBox13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox13.Click
  822.         Try
  823.             Dim extractCMD As New Process()
  824.             Dim sFile As String = PathTb.Text
  825.             Dim myPercent As Char
  826.  
  827.             myPercent = Chr(37)
  828.             Dim myZipCommand As String = """no-frills.exe " & Chr(37) _
  829.                             & "a " & Chr(37) & "d " & Chr(37) & "f"""
  830.             MsgBox("doctotext.exe " & "--fix-xml --unzip-cmd=" & myZipCommand & " """ & sFile & """")
  831.             extractCMD.StartInfo.FileName = "doctotext.exe"
  832.             extractCMD.StartInfo.Arguments = "--fix-xml --unzip-cmd=" _
  833.                             & myZipCommand & " """ & sFile & """"
  834.             extractCMD.StartInfo.UseShellExecute = False
  835.             extractCMD.StartInfo.RedirectStandardOutput = True
  836.             extractCMD.StartInfo.CreateNoWindow = True
  837.             extractCMD.Start()
  838.  
  839.             Dim sFileText As String = sFile & ".txt"
  840.             Dim doctotextOutput As String = extractCMD.StandardOutput.ReadToEnd()
  841.             Dim sErr As String = ""
  842.             'Save to different file
  843.             Dim bAns As String = SaveTextToFile(doctotextOutput, sFileText, sErr)
  844.             If bAns Then
  845.                 MsgBox("Please note: all successfuly extracted worksheets will " _
  846.                        & "appear on just one worksheet travelling vertically down.")
  847.                 Dim oExcel As New Excel.Application
  848.                 oExcel.Visible = True
  849.                 oExcel.Workbooks.Open(sFileText)
  850.  
  851.             Else
  852.                 MsgBox("Error extracting file: " & sErr)
  853.             End If
  854.  
  855.             extractCMD.Close()
  856.  
  857.         Catch ex As Exception
  858.             MessageBox.Show(ex.Message)
  859.         End Try
  860.     End Sub
  861.  
  862.     Private Sub PictureBox14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox14.Click
  863.         Try
  864.             MsgBox("If successful, each Worksheet will be saved as separate " _
  865.                    & "CSV files in the same directory as your corrupt file.")
  866.             Dim OFlD As New FolderBrowserDialog
  867.             Dim sFile As String = PathTb.Text
  868.             Dim coffecCMD As New Process()
  869.             coffecCMD.StartInfo.FileName = "coffec.exe"
  870.             coffecCMD.StartInfo.Arguments = "-t """ & sFile & """"
  871.             coffecCMD.StartInfo.UseShellExecute = True
  872.             coffecCMD.StartInfo.CreateNoWindow = True
  873.             coffecCMD.Start()
  874.             coffecCMD.WaitForExit()
  875.             coffecCMD.Close()
  876.             Dim sFileInfo As New FileInfo(sFile)
  877.             Dim sFileName As String = sFileInfo.Name
  878.             Dim sFilePath As String = DelFromRight(sFileName, sFile)
  879.             Process.Start("explorer.exe", sFilePath)
  880.  
  881.         Catch ex As Exception
  882.             MessageBox.Show(ex.Message)
  883.         End Try
  884.     End Sub
  885.  
  886.     Private Sub FolderBrowserDialog1_HelpRequest(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FolderBrowserDialog1.HelpRequest
  887.  
  888.     End Sub
  889.  
  890.     Private Sub PictureBox15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox15.Click
  891.         Try
  892.             Dim repairZip As New Process()
  893.             Dim sFile As String = PathTb.Text
  894.             Dim sFileZip As String = sFile & ".zip"
  895.             Dim sFileInfo As New FileInfo(sFile)
  896.             Dim sFileName As String = sFileInfo.Name
  897.             Dim zipRepairedsFileName As String = "zipRepaired" & sFileName & ".zip"
  898.             Dim sFileBasePath As String = DelFromRight(sFileName, sFile)
  899.             Dim zipRepairedFullPathFileName As String = sFileBasePath & zipRepairedsFileName
  900.  
  901.             FileCopy(sFile, sFileZip)
  902.             repairZip.StartInfo.FileName = "zip.exe"
  903.             repairZip.StartInfo.Arguments = "-FF """ & sFileZip & """ --out " & zipRepairedFullPathFileName
  904.             repairZip.StartInfo.UseShellExecute = False
  905.             repairZip.StartInfo.RedirectStandardOutput = True
  906.             repairZip.StartInfo.CreateNoWindow = True
  907.             repairZip.Start()
  908.  
  909.             Dim repairZipReader As StreamReader = repairZip.StandardOutput
  910.             Dim repairZipCompOut As String = repairZipReader.ReadToEnd
  911.  
  912.             repairZipReader.Close()
  913.             repairZip.WaitForExit()
  914.             repairZip.Close()
  915.  
  916.             Dim zipRepairedFullPathXlsxName As String = DelFromRight(".zip", zipRepairedFullPathFileName)
  917.             Dim oExcel As New Excel.Application
  918.             Dim oBooks As Excel.Workbooks = Nothing
  919.             Dim oBook As Workbook = Nothing
  920.             Rename(zipRepairedFullPathFileName, zipRepairedFullPathXlsxName)
  921.             oExcel.Visible = True
  922.             oBooks = oExcel.Workbooks
  923.             oBook = oBooks.Open(Filename:=zipRepairedFullPathXlsxName)
  924.  
  925.         Catch ex As Exception
  926.             MessageBox.Show(ex.Message)
  927.         End Try
  928.     End Sub
  929.  
  930.     Private Sub PathTb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PathTb.TextChanged
  931.  
  932.     End Sub
  933. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement