2013-11-26 3 views
1

다음 VBA 코드는 텍스트 파일을 열고 텍스트 파일로 번호를 읽은 다음 텍스트 파일의 번호에 1을 더한 다음 텍스트 파일에 새 번호를 저장합니다 .오류 52 - 잘못된 파일 번호

현재 () 표시된 줄에 오류 52가 표시됩니다. 이 문제를 어떻게 해결할 수 있습니까?

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 
+0

시도해보십시오. 입력 파일 이름은 # filenum으로 입력하십시오. –

+0

@TimWilliams : 나는 그것에 대해 생각했지만'# '은 선택 사항이므로 그렇게 할 수 없습니다. –

답변

4

While ... Wend 루프 내에서 파일을 닫습니다. 따라서 첫 번째 행을 읽고 파일을 닫은 다음 EOF(filenum)을 확인하지만 filenum은 더 이상 열린 파일에 대한 유효한 핸들이 아니므로 오류가 발생합니다.

그냥 루프 밖으로 Close 문을 이동 : 나는 당신의 코드를 들여 쓰기가 제대로 같은데요

While Not EOF(filenum) '*error 52 - Bad file number 
    Line Input #filenum, current_number 
Wend 
Close filenum 

당신이 실수를 발견 도움이 것!

+0

좋은 캐치! 완전히 그걸 놓쳤다. –

+0

고마워요! 'Open fileName For Fileenum' 행에 이미 파일 열기 오류가 발생했습니다. 네가 볼 수있는 이유가 있니? – scb998

+0

파일이 이미 열려 있습니까? 어쩌면 Excel의 다른 인스턴스 나 다른 응용 프로그램이 있을까요? –

관련 문제