Excel 中的文档未保存错误

发布于 2024-08-08 11:51:14 字数 724 浏览 4 评论 0原文

这是我用于比较 2 个 Excel 文件的代码片段。我从 QTP 脚本中调用此函数。我经常收到此错误,这导致我的测试脚本失败。任何指示将不胜感激。

Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Visible = False
objExcel.DisplayAlerts = False
Set objWorkbook1= objExcel.Workbooks.Open(excelFile1)
Set objWorkbook2= objExcel.Workbooks.Open(excelFile2)

Set objWorksheet1= objWorkbook1.Worksheets(1)
Set objWorksheet2= objWorkbook2.Worksheets(1)

<Code that compares the 2 files & marks the cell in red where there is a mismatch)
objWorkbook2.Save

错误消息:
文档未保存。
函数文件:C:\Program Files\Mercury Interactive\QuickTest Professional\Tests\ReusableFunctions.qfl
第 (33) 行:“objWorkbook2.Save”。

This is my code snippet for comparing 2 excel files. I call this function from my QTP scripts. I get this error quite often which causes my test script to fail. Any pointers will be appreciated.

Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Visible = False
objExcel.DisplayAlerts = False
Set objWorkbook1= objExcel.Workbooks.Open(excelFile1)
Set objWorkbook2= objExcel.Workbooks.Open(excelFile2)

Set objWorksheet1= objWorkbook1.Worksheets(1)
Set objWorksheet2= objWorkbook2.Worksheets(1)

<Code that compares the 2 files & marks the cell in red where there is a mismatch)
objWorkbook2.Save

ERROR MESSAGE:
Document not saved.
Function file: C:\Program Files\Mercury Interactive\QuickTest Professional\Tests\ReusableFunctions.qfl
Line (33): "objWorkbook2.Save".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-08-15 11:51:14

Microsoft 知识库中有几篇文章讨论了保存 Excel 文件时可能出现的错误:

例如,错误可能是由于驱动器空间不足或权限、网络连接丢失(如果您要保存到网络驱动器)、防病毒冲突、嵌入对象等。您的情况很可能就是其中之一。

无论如何,创建一个带有标记不匹配的新工作簿比更改现有工作簿不是更好吗?

There're several articles in Microsoft knowledge base that discuss possible errors while saving Excel files:

For instance, errors can be caused by insufficient drive space or permissions, lost network connection (if you are saving to a network drive), antivirus conflict, embedded objects and so on. Your case is most probably one of these.

Anyway, woudn't it be better to create a new workbook with marked mismatches rather than change an existing one?

魂归处 2024-08-15 11:51:14

由于您只放置了代码的一部分,因此我不确定您是否放置了这两个语句。应该有 2 个调用:

objWorkbook2.Save
objWorkbook2.Close

您还可以在函数中使用以下隔离的代码。

来源:http://automation-beyond.com/2009/05/25 /excel-vbscript/

Public Function ExcelWorksheetCompare(ByVal sWorkbook1, ByVal sWorksheet1, ByVal sWorkbook2, ByVal sWorksheet2, ByVal objParameter)
Dim boolRC, boolSheetExists
Dim FSO, XLHandle
Dim XLBook1, XLBook2, XLSheet1, XLSheet2
Dim Iter, objCell

‘Verify both files exist
Set FSO = CreateObject(”Scripting.FileSystemObject”)
boolRC = FSO.FileExists(sWorkbook1)
If Not boolRC Then
ExcelWorksheetCompare = FALSE
Exit Function
End If
boolRC = FSO.FileExists(sWorkbook2)
If Not boolRC Then
ExcelWorksheetCompare = FALSE
Exit Function
End If
Set FSO = Nothing

Set XLHandle = CreateObject(”Excel.Application”)
XLHandle.DisplayAlerts = False

‘Open workbook1
Set XLBook1 = XLHandle.WorkBooks.Open(sWorkbook1)

‘Verify sheet exists (1)
If isNumeric(sWorksheet1) Then
sWorksheet1 = CInt(sWorksheet1)
If (sWorksheet1 > 0) AND (sWorksheet1 <= XLBook1.Worksheets.Count) Then
Set XLSheet1 = XLBook1.Worksheets(sWorksheet1)
boolSheetExists = TRUE
Else
boolSheetExists = FALSE
End If
Else
boolSheetExists = FALSE
For Iter = 1To XLBook1.Worksheets.Count
If XLBook1.Worksheets(Iter).Name = sWorksheet1 Then
Set XLSheet1 = XLBook1.Worksheets(Iter)
boolSheetExists = TRUE
End If
Next
End If

If Not boolSheetExists Then
XLBook1.Close
XLHandle.Quit
Set XLBook1 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = FALSE
Exit Function
End If

‘Open workbook2
Set XLBook2 = XLHandle.WorkBooks.Open(sWorkbook2)

‘Verify sheet exists (2)
If isNumeric(sWorksheet2) Then
sWorksheet2 = CInt(sWorksheet2)
If (sWorksheet2 > 0) AND (sWorksheet2 <= XLBook2.Worksheets.Count) Then
Set XLSheet2 = XLBook2.Worksheets(sWorksheet2)
boolSheetExists = TRUE
Else
boolSheetExists = FALSE
End If
Else
boolSheetExists = FALSE
For Iter = 1To XLBook2.Worksheets.Count
If XLBook2.Worksheets(Iter).Name = sWorksheet2 Then
Set XLSheet2 = XLBook2.Worksheets(Iter)
boolSheetExists = TRUE
End If
Next
End If

If Not boolSheetExists Then
XLBook1.Close
XLBook2.Close
XLHandle.Quit
Set XLSheet1 = Nothing
Set XLBook1 = Nothing
Set XLBook2 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = FALSE
Exit Function
End If

‘Mark range

‘Compare and mark mismatches red
For Each objCell In XLSheet2.UsedRange
If objCell.Value <> XLSheet1.Range(objCell.Address).Value Then
objCell.Interior.ColorIndex = 3
Else
objCell.Interior.ColorIndex = 0
End If
Next

‘Save and close

XLBook1.Close

XLBook2.Save
XLBook2.Close

XLHandle.Quit

Set XLSheet1 = Nothing
Set XLSheet2 = Nothing
Set XLBook1 = Nothing
Set XLBook2 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = TRUE

End Function

谢谢,
阿尔伯特·加里耶夫
http://automation-beyond.com/

Since you put only a fragment of your code I'm not sure whether you put both statements. There should be 2 calls:

objWorkbook2.Save
objWorkbook2.Close

You can also use the following code isolated in a function.

Origin: http://automation-beyond.com/2009/05/25/excel-vbscript/

Public Function ExcelWorksheetCompare(ByVal sWorkbook1, ByVal sWorksheet1, ByVal sWorkbook2, ByVal sWorksheet2, ByVal objParameter)
Dim boolRC, boolSheetExists
Dim FSO, XLHandle
Dim XLBook1, XLBook2, XLSheet1, XLSheet2
Dim Iter, objCell

‘Verify both files exist
Set FSO = CreateObject(”Scripting.FileSystemObject”)
boolRC = FSO.FileExists(sWorkbook1)
If Not boolRC Then
ExcelWorksheetCompare = FALSE
Exit Function
End If
boolRC = FSO.FileExists(sWorkbook2)
If Not boolRC Then
ExcelWorksheetCompare = FALSE
Exit Function
End If
Set FSO = Nothing

Set XLHandle = CreateObject(”Excel.Application”)
XLHandle.DisplayAlerts = False

‘Open workbook1
Set XLBook1 = XLHandle.WorkBooks.Open(sWorkbook1)

‘Verify sheet exists (1)
If isNumeric(sWorksheet1) Then
sWorksheet1 = CInt(sWorksheet1)
If (sWorksheet1 > 0) AND (sWorksheet1 <= XLBook1.Worksheets.Count) Then
Set XLSheet1 = XLBook1.Worksheets(sWorksheet1)
boolSheetExists = TRUE
Else
boolSheetExists = FALSE
End If
Else
boolSheetExists = FALSE
For Iter = 1To XLBook1.Worksheets.Count
If XLBook1.Worksheets(Iter).Name = sWorksheet1 Then
Set XLSheet1 = XLBook1.Worksheets(Iter)
boolSheetExists = TRUE
End If
Next
End If

If Not boolSheetExists Then
XLBook1.Close
XLHandle.Quit
Set XLBook1 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = FALSE
Exit Function
End If

‘Open workbook2
Set XLBook2 = XLHandle.WorkBooks.Open(sWorkbook2)

‘Verify sheet exists (2)
If isNumeric(sWorksheet2) Then
sWorksheet2 = CInt(sWorksheet2)
If (sWorksheet2 > 0) AND (sWorksheet2 <= XLBook2.Worksheets.Count) Then
Set XLSheet2 = XLBook2.Worksheets(sWorksheet2)
boolSheetExists = TRUE
Else
boolSheetExists = FALSE
End If
Else
boolSheetExists = FALSE
For Iter = 1To XLBook2.Worksheets.Count
If XLBook2.Worksheets(Iter).Name = sWorksheet2 Then
Set XLSheet2 = XLBook2.Worksheets(Iter)
boolSheetExists = TRUE
End If
Next
End If

If Not boolSheetExists Then
XLBook1.Close
XLBook2.Close
XLHandle.Quit
Set XLSheet1 = Nothing
Set XLBook1 = Nothing
Set XLBook2 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = FALSE
Exit Function
End If

‘Mark range

‘Compare and mark mismatches red
For Each objCell In XLSheet2.UsedRange
If objCell.Value <> XLSheet1.Range(objCell.Address).Value Then
objCell.Interior.ColorIndex = 3
Else
objCell.Interior.ColorIndex = 0
End If
Next

‘Save and close

XLBook1.Close

XLBook2.Save
XLBook2.Close

XLHandle.Quit

Set XLSheet1 = Nothing
Set XLSheet2 = Nothing
Set XLBook1 = Nothing
Set XLBook2 = Nothing
Set XLHandle = Nothing

ExcelWorksheetCompare = TRUE

End Function

Thank you,
Albert Gareev
http://automation-beyond.com/

染火枫林 2024-08-15 11:51:14

我一直在研究“文档未保存”错误的类似问题:我认为跨网络驱动器的“保存”过程存在未记录的问题,我可能会再次问您的问题,并提供一些额外的详细信息。

I've been looking into a similar problem with 'document not saved' errors: I think there's an undocumented issue with the 'save' process across network drives, and I'll probably ask your question again, with a few extra details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文