我是Outlook VBA的新手,我正在尝试比较来自两个不同的已关闭工作簿的同一单元格中的值。
我在收到电子邮件中的附件后创建了Excel对象,该附件将附件另存为CSV文件。
然后,我想检查文件中的标题行是否与第一行中具有相同标题的主副本匹配,但我不确定如何使用对象引用工作表名称和单元格。 我已经尝试了很多使用VBA for Excel的方法,但它似乎在Outlook中不起作用。
如果有人可以帮助我,我将不胜感激。
Function ConvertXls2CSV(sXlsFile As String)
On Error Resume Next
Dim oExcel As Object
Dim oExcelWrkBk As Object
Dim bExcelOpened As Boolean 'Was Excel already open or not
Dim OriginalFile As String
Dim MasterFile As String
Dim Fault As Integer
Set oExcel = GetObject(, "Excel.Application") 'Bind to existing instance of Excel
If Err.Number <> 0 Then 'Could not get instance of Excel, so create a new one
Err.Clear
On Error GoTo Error_Handler
Set oExcel = CreateObject("excel.application")
bExcelOpened = False
Else 'Excel was already running
bExcelOpened = True
End If
Set oExcelWrkBk = oExcel.Workbooks.Open(sXlsFile)
oExcelWrkBk.SaveAs Left(sXlsFile, InStrRev(sXlsFile, ".")) & "csv", xlCSVWindows
/*THIS IS WHERE I WANT TO REFERENCE THE CELLS IN THE WORKBOOKS*/
OriginalFile = oExcelWrkBk.Sheets("PK Price Data").Cells(1, 1).Value
MasterFile = oExcelWrkBk."MasterFile.xls".Sheets("PK Price Data").Cells(1, 1).Value
if OriginalFile = MasterFile then
fault = 1
else fault = 0
end if
oExcelWrkBk.Close False
If bExcelOpened = False Then
oExcel.Quit
End If
End Function
非常感谢 梅林达
答案 0 :(得分:0)
据我所知,无法解决您对已关闭工作簿的引用问题。但是,您可以在不显示application.screenupdating = false
的情况下打开它。当您将引用存储在某些变量中时,只需关闭工作簿并设置application.screenupdating = true