下面的VBA代碼應該打開一個文本文件,將其中的數字讀入文本文件,在文本文件中將數字加1,然後將新數字保存在文本文件中。錯誤52-壞文件編號
我目前得到錯誤52在指定的行(While Not EOF(filenum)
)。我該如何解決?
Public Sub Items_ItemAdd(Item As Outlook.MailItem)
Dim filenum As Integer
Dim current_number As String
Dim fileName As String
Dim objNS As Outlook.NameSpace
Set objNS = GetNamespace("MAPI")
Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
filenum = FreeFile()
fileName = "G:\Infrastructure Services\Engineering Services\Hazard Report Number.txt"
Open fileName For Input As filenum
While Not EOF(filenum) '*error 52 - Bad file number
Line Input #filenum, current_number
Close filenum
Wend
If Item.Class = olMail Then
If Left$(Item.Subject, 29) = "Hazard Identification Report" Then
Dim Msg As Outlook.MailItem
Dim NewForward As Outlook.MailItem
Dim myFolder As Outlook.MAPIFolder
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Set Msg = Item
Set NewForward = Msg.Forward
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
strSender = ""
strsenderName = Msg.SenderEmailAddress
If strsenderName = "EX" Then
Set objSender = itm.Sender
If Not (objSender Is Nothing) Then
Set objExchUser = Sender.GetExchangeUser()
If Not (objExchUser Is Nothing) Then
strSender = objExchUser.PrimarySmtpAddress
End If
End If
Else
strSender = strsenderName
End If
With NewForward
.Subject = "Hazard report reciept number: & "
.To = strSender
.HTMLBody = "TYhank you for your email"
.Send
End With
End If
End If
Close filenum
ExitProc:
Set NewForward = Nothing
Set Msg = Nothing
Set olApp = Nothing
Set olNS = Nothing
End Sub
嘗試'打開文件名爲輸入爲#filenum' –
@TimWilliams:我想過,但'#'是可選的,所以不能這樣做。 –