1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| Imports Microsoft.Office.Interop
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel
Module Mdl_Naming
Dim xlsApp As Excel.Application
Dim xlsWB As Excel.Workbook
Dim xlsSheetOne As Excel.Worksheet
Dim xlsSheetTwo As Excel.Worksheet
Dim xlsSheetThree As Excel.Worksheet
Dim xlsSheetFour As Excel.Worksheet
Dim xlsrange As Excel.Range
Public Structure ExcelRows
Dim ColA As String
End Structure
Public ExcelRowList As List(Of ExcelRows) = New List(Of ExcelRows)
Public Sub Naming()
xlsApp = New Excel.Application
xlsApp.Visible = False 'replace by False for not displaying the spreadsheet
xlsWB = xlsApp.Workbooks.Open(Form1.LblFile1.Text)
'' set up the xlsSheetOne and xlsSheetTwo
xlsSheetOne = xlsWB.Worksheets("Export (Excel -> Microstation)")
xlsSheetTwo = xlsWB.Worksheets("Import (Microstation -> Excel)")
xlsSheetThree = xlsWB.Worksheets("Templates")
xlsSheetFour = xlsWB.Worksheets("Mismatch_Output")
Dim xlsrngR As Excel.Range
Dim xlsrngRR As Excel.Range
Dim xlsintI As Integer
Dim xlsstrTemp As String
Dim xlslastrow As Integer
''--- Delete all data in sheet "Mismatch_Output" and add column Header in A to C ---''
xlsSheetFour.Visible = True
xlsSheetFour.Cells.Delete(Shift:=Excel.XlDirection.xlUp)
xlsSheetFour.Cells(1, 1).Value = "Microstation Files Name"
xlsSheetOne.Select()
xlsrange = xlsSheetOne.Range("A65536").End(Excel.XlDirection.xlUp).Rows
xlsrange = xlsSheetOne.Range("A3", "A" & xlsrange.Row)
xlsrange.Select()
xlsrange.Copy()
xlsSheetFour.Range("A2").PasteSpecial()
xlslastrow = xlsSheetFour.Range("a:a").Find("*", SearchDirection:=Excel.XlSearchDirection.xlPrevious).Row
xlsrngRR = xlsSheetFour.Range("A2", "A" & xlslastrow).SpecialCells(Excel.XlCellType.xlCellTypeConstants, )
For Each xlsrngR In xlsrngRR
xlsstrTemp = ""
For xlsintI = 1 To Len(xlsrngR.Value)
If Not Mid(xlsrngR.Value, xlsintI, 1) Like "[A-Z,a-z,0-9,+() . _-]" Then 'Enter the only characters that are allowed !
xlsrngR.Interior.ColorIndex = 3 'red
End If
If xlsrngR.Interior.ColorIndex <> 3 Then
xlsrngR.Interior.ColorIndex = 6 'yellow
End If
Next xlsintI
Next xlsrngR
Dim Last As Integer, Del As Integer
Last = xlsSheetFour.Range("A" & xlsSheetFour.Rows.Count).End(Excel.XlDirection.xlUp).Row
For Del = Last To 1 Step -1
If xlsSheetFour.Cells(Del, "A").Interior.ColorIndex = 6 Then
xlsSheetFour.Rows(Del).EntireRow.Delete()
End If
Next Del
With Form8
If GetInfo() = True Then
For Each xitem In ExcelRowList
Dim lvitem As ListViewItem
lvitem = Form8.ListView1.Items.Add(xitem.ColA)
lvitem.SubItems.AddRange(New String() {xitem.ColA})
Next
End If
End With
If ExcelRowList.Count > 0 Then
Form8.Show()
End If
' xlsWB.Save()
' xlsWB.Close()
' xlsApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp)
xlsSheetOne = Nothing
xlsSheetTwo = Nothing
xlsSheetThree = Nothing
xlsSheetFour = Nothing
xlsWB = Nothing
xlsApp = Nothing
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
proc.Kill()
Next
End Sub
Public Function GetInfo() As Boolean
Dim Completed As Boolean = False
xlsSheetFour.Activate()
xlsrange = xlsSheetFour.Range("A2")
xlsrange.Activate()
Dim ThisRow As New ExcelRows
'Extract
Do
If xlsrange.Value > Nothing Or xlsrange.Text > Nothing Then
ThisRow.ColA = xlsrange.Value
ExcelRowList.Add(ThisRow)
xlsrange = xlsrange.Offset(1, 0)
Else
Completed = True
Exit Do
End If
Loop
Return (Completed)
End Function
End Module |
Partager