错误消息:System.InvalidCastException:从字符串“”到类型“日期”的转换无效。
Sub Main()
Dim OutApp As Object
Dim OutMail As Object
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim fecha As String
Dim proc As System.Diagnostics.Process
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
If proc.MainWindowTitle.Trim.Length = 0 Then
proc.Kill()
End If
Next
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open("\\file.xlsm")
xlWorkSheet = xlWorkBook.Worksheets("Sheet")
Try
xlApp.Range("E41").Activate()
xlApp.Range("E4").Select()
xlWorkSheet.Range("E4").Select()
xlWorkSheet.Range("E4").Activate()
fecha = xlApp.ActiveCell.Value
Do While xlApp.ActiveCell.Value <> ""
'If (DateTime.Today - CDate(fecha)).Days = 180 Then
If (DateTime.Today - DateTime.ParseExact(fecha, "dd/mm/yyyy", CultureInfo.InvariantCulture)).Days = 180 Then
' --more lines ---
'...
End Sub
在此行中出现错误:
Do While xlApp.ActiveCell.Value <> ""
由于某种原因,似乎 xlApp.ActiveCell.Value 被视为日期而不是字符串。
另一件事,如果今天和单元格值之间有 6 个月的差异,这是比较正确的方法吗?
If (DateTime.Today - DateTime.ParseExact(fecha, "dd/mm/yyyy", CultureInfo.InvariantCulture)).Days = 180