簡體   English   中英

Excel 中的 SaveAs 在 VB.net 上引發錯誤

[英]SaveAs in Excel Throws an Error on VB.net

我正在嘗試制作一個修剪 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

編譯器不知道帶有 Option Strict 的Workbook是什么(它應該始終處於打開狀態)。 由於我的 Imports 聲明,我能夠獲得Excel的資格。

我更改了消息框格式。 這些值需要按位Or來組合正確的標志。

調用事件不是一個好主意。 這可能有其他含義。 將代碼移動到單獨的方法並從此代碼和按鈕單擊代碼中調用該方法。

在滿足您的條件之前,請勿創建Excel對象。 通過組合連續的范圍,我能夠稍微縮短代碼。

退出不會完全清理互操作對象。 互聯網上有很多頁面處理這個問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM