我正在嘗試制作一個修剪 excel 檔案的程式。我想保留原始檔案,并將新副本另存為具有修改名稱的新目的地。我的代碼給了我一個錯誤,我不確定我錯過了什么。另外,有沒有什么辦法實際上只是添加一個范圍來洗掉列/行而不是一個一個地洗掉?
拋出例外:PO Trimmer.exe 中的“System.Runtime.InteropServices.COMException” PO Trimmer.exe 中發生型別為“System.Runtime.InteropServices.COMException”的未處理例外 無法訪問該檔案。嘗試以下方法之一:
確保指定的檔案夾存在。
確保包含該檔案的檔案夾不是只讀的。
確保檔案名和檔案夾路徑不包含以下任何字符:< > ? [ ] : | 或者 *
確保檔案名和檔案夾路徑不超過 218 個字符。
Dim xlApp As Application Dim xlBook As Workbook Dim xlSheet As Worksheet If txtTarget.Text = "" Then MsgBox("Please select the file you wish to convert!", vbExclamation vbOKCancel, "Missing Target File") btnBrowseTarget.PerformClick() ElseIf txtDestination.Text = "" Then MsgBox("Please select the folder to save your file!", vbExclamation vbOKCancel, "Missing Destination Folder") btnBrowseDestination.PerformClick() Else xlApp = CreateObject("Excel.Application") xlBook = xlApp.Workbooks.Open(txtTarget.Text) xlSheet = xlBook.Worksheets(1) If rbtnSeca.Checked = True Then Dim rg As Excel.Range rg = xlSheet.Columns("N") rg.Select() rg.Delete() rg = xlSheet.Columns("M") rg.Select() rg.Delete() rg = xlSheet.Columns("L") rg.Select() rg.Delete() rg = xlSheet.Columns("K") rg.Select() rg.Delete() rg = xlSheet.Columns("J") rg.Select() rg.Delete() rg = xlSheet.Columns("I") rg.Select() rg.Delete() rg = xlSheet.Columns("H") rg.Select() rg.Delete() rg = xlSheet.Columns("F") rg.Select() rg.Delete() rg = xlSheet.Columns("E") rg.Select() rg.Delete() rg = xlSheet.Columns("D") rg.Select() rg.Delete() rg = xlSheet.Columns("B") rg.Select() rg.Delete() rg = xlSheet.Columns("A") rg.Select() rg.Delete() rg = xlSheet.Rows(1) rg.Select() rg.Delete() Dim convertSuccess As Integer = MsgBox("Trimming success.", vbInformation vbOKOnly, "Excel Trim") xlBook.SaveAs(txtDestination.Text & "PO_" & Today & "_" & TimeOfDay, XlFileFormat.xlOpenXMLWorkbook) xlApp.Quit() ElseIf rbtnMS.Checked = True Then End If End If
uj5u.com熱心網友回復:
編譯器不知道WorkbookOption Strict 是什么(它應該總是打開)。Excel由于我的進口宣告,我能夠獲得資格。
我更改了訊息框格式。這些值需要按位Or組合正確的標志。
呼叫事件不是一個好主意。這可能會產生其他影響。將代碼移動到一個單獨的方法,并從此代碼和按鈕單擊代碼呼叫它。
Excel在滿足條件之前不要創建物件。通過組合連續范圍,我能夠稍微縮短代碼。
Quit 不會完全清理互操作物件。互聯網上有很多頁面處理這個問題。
Imports Excel = Microsoft.Office.Interop.Excel
Private Sub btnBrowseDestination_Click(sender As Object, e As EventArgs) Handles btnBrowseDestination.Click
BrowseDestination()
End Sub
Private Sub btnBrowseTarget_Click(sender As Object, e As EventArgas) Handles btnBrowseTarget.Click
BrowseTarget()
End Sub
Private Sub BrowseTarget()
'Code moved from btnBrowseTarget.Click
End Sub
Private Sub BrowseDestination()
'Code moved from btnBrowseDestination.Click
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If txtTarget.Text = "" Then
MsgBox("Please select the file you wish to convert!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkCancel, "Missing Target File")
BrowseTarget()
ElseIf txtDestination.Text = "" Then
MsgBox("Please select the folder to save your file!", MsgBoxStyle.Exclamation Or MsgBoxStyle.OkCancel, "Missing Destination Folder")
BrowseDestination()
Else
If rbtnSeca.Checked = True Then
Dim xlApp As New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlBook = xlApp.Workbooks.Open(txtTarget.Text)
xlSheet = DirectCast(xlBook.Worksheets(1), Excel.Worksheet)
Dim rg = DirectCast(xlSheet.Columns("H:N"), Excel.Range)
rg.Select()
rg.Delete()
rg = DirectCast(xlSheet.Columns("D:F"), Excel.Range)
rg.Select()
rg.Delete()
rg = DirectCast(xlSheet.Columns("A:B"), Excel.Range)
rg.Select()
rg.Delete()
rg = DirectCast(xlSheet.Rows(1), Excel.Range)
rg.Select()
rg.Delete()
Dim convertSuccess As Integer = MsgBox("Trimming success.", MsgBoxStyle.Information Or MsgBoxStyle.OkOnly, "Excel Trim")
xlBook.SaveAs(txtDestination.Text & "PO_" & Today & "_" & TimeOfDay, Excel.XlFileFormat.xlOpenXMLWorkbook)
xlApp.Quit()
End If
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/378924.html
