繁体   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