2017-08-14 2715 views
0

我希望你能帮上忙。我有一个宏,它很广泛。它基本上是它有一个用户窗体参见图1VBA Excel错误运行时错误'53'

此用户窗体允许用户导航到存储数据的模板(请参见图2)并选择此完成的模板。

在图1中的用户表单上。然后,用户单击单选按钮DTOV(没有会议信息),然后单击按钮Pre-Validate。

单击此预验证按钮后,Excel模板上的数据将从此Excel文件转换为文本文件复制到| (管道)分隔符,并且要求用户确认该新创建的文本文件将去往的目录路径。参见图三

什么应该发生的是,这个新创建的文本文档被放入其中的文本文档再度回升,并发送到另一个数据库的文件夹

我遇到的问题是,新创建的文本文件不会将其传送到由其他数据库拾取的文件夹。

我得到一个错误运行时错误“53”,这是上线发生

' create the file on the network folder Name DTOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt"

这是坐落在私人小组Private Sub Pre_validate_Click()

有大量的其他代码在此宏,你可以在Pic 4中看到很多模块。其他代码的话可以提供必要的,但错误是出现在EFPIA_Macro用户表单

所以我只在其整体提供这种用户表单代码下面请参阅代码

再次错误是发生在' create the file on the network folder Name DTOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt"

这是坐落在私人小组Private Sub Pre_validate_Click()

我真的在努力解决这一

,以便始终任何及所有的帮助是很大的一个ppreciated。

谢谢

图片1

enter image description here

产品图2 enter image description here

图三

enter image description here

PIC 4

enter image description here

CODE

Dim DTOV_Directory As String 
Dim DTOV_fname As String 
Dim ITOV_Directory As String 
Dim ITOV_fname As String 
Dim txtFileName As String 


Private Sub Clear_form_Click() 
Unload Me 
EFPIA_Macro.Show 
End Sub 

Private Sub Close_form_Click() 
Unload Me 
ThisWorkbook.Close savechanges:=False 
Application.Quit 


End Sub 

Private Sub DTOV_chkbox_Change() 
    If txtFileName = "" Then 
     DTOV_chkbox = False 
     DTOV_filename = "" 
     Call dtov_file_processing 
    End If 
    txtFileName = "" 
End Sub 

Private Sub DTOV_chkbox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 
    Call dtov_file_processing 
End Sub 

Private Sub dtov_file_processing() 
    Dim fd As Office.FileDialog 
    Dim varFile As Variant 

    ' check if either of graphical/raw file is selected 
    If Graphical_file.Value <> True And Raw_file.Value <> True Then 
     MsgBox "Please select the type of file - Graphical/Raw" 
     DTOV_chkbox = False 
     DTOV_filename = "" 
     txtFileName = "" 
    ElseIf DTOV_filename <> "" Then 
     DTOV_chkbox = False 
     DTOV_filename = "" 
     txtFileName = "" 
    Else 
     txtFileName = "" 
     Set fd = Application.FileDialog(msoFileDialogFilePicker) 
     With fd 
      .AllowMultiSelect = False 
      .Title = "Please select the file." 

      ' Clear out the current filters, and add our own. 
      .Filters.Clear 
      .Filters.Add "All Files", "*.*" 
      .Filters.Add "Excel 2003", "*.xls" 

      If .Show = True Then 
       txtFileName = .SelectedItems(1) 
      End If 
     End With 

     If Graphical_file.Value = True And (InStr(txtFileName, "DTOV") = 0 Or InStr(txtFileName, ".xls") = 0 Or txtFileName = "") Then 
      MsgBox "Incorrect DTOV file selected. Please re-select the file" 
      DTOV_chkbox = False 
      DTOV_filename = "" 
     ElseIf Raw_file.Value = True And InStr(txtFileName, ".xls") = 0 Then 
      MsgBox "Incorrect RAW file selected. Please re-select the file" 
      DTOV_chkbox = False 
      DTOV_filename = "" 
     Else 
      DTOV_filename = txtFileName 
      DTOV_chkbox = True 
     End If 
    End If 
End Sub 

Private Sub Graphical_file_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 
    File_category_frame_1.Caption = "Choose DTOV File" 
    DTOV_chkbox.Caption = "DTOV (Without Meeting Information)" 
    File_category_frame_2.Visible = True 
    ITOV_chkbox.Visible = True 
    DTOV_chkbox = False 
    DTOV_filename = "" 
    ITOV_chkbox = False 
    ITOV_filename = "" 
    txtFileName = "" 
End Sub 


Private Sub ITOV_chkbox_Change() 
    If txtFileName = "" Then 
     ITOV_chkbox = False 
     ITOV_filename = "" 
     Call itov_file_processing 
    End If 
    txtFileName = "" 
End Sub 

Private Sub ITOV_chkbox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 
    Call itov_file_processing 
End Sub 

Private Sub itov_file_processing() 
    Dim fd As Office.FileDialog 

    ' check if either of graphical/raw file is selected 
    If Graphical_file.Value <> True And Raw_file.Value <> True Then 
     MsgBox "Please select the type of file - Graphical/Raw" 
     ITOV_chkbox = False 
     ITOV_filename = "" 
     txtFileName = "" 
    ElseIf ITOV_filename <> "" Then 
     ITOV_chkbox = False 
     ITOV_filename = "" 
     txtFileName = "" 
    Else 
     txtFileName = "" 
     Set fd = Application.FileDialog(msoFileDialogFilePicker) 
     With fd 
      .AllowMultiSelect = False 
      .Title = "Please select the file." 

      ' Clear out the current filters, and add our own. 
      .Filters.Clear 
      .Filters.Add "All Files", "*.*" 
      .Filters.Add "Excel 2003", "*.xls" 

      If .Show = True Then 
       txtFileName = .SelectedItems(1) 
      End If 
     End With 

     If InStr(txtFileName, "ITOV") = 0 Or InStr(txtFileName, ".xls") = 0 Then 
      MsgBox "Incorrect file selected. Please re-select the file" 
      ITOV_chkbox = False 
      ITOV_filename = "" 
     Else 
      ITOV_filename = txtFileName 
      ITOV_chkbox = True 
     End If 
    End If 
End Sub 

Private Sub Pre_validate_Click() 
Dim newfilename As String 
Dim network_path As String 
Dim final_msg As String 

' user interface to accept network folder path 
PreVal_Dir_Path.Show 
network_path = EFPIA_Macro.Pre_validate.ControlTipText 
EFPIA_Macro.Pre_validate.ControlTipText = "" 
final_msg = "The below files are submitted for the prevalidation:" 

If Not Dir(network_path, vbDirectory) = vbNullString Then 
    DTOV_fname = "" 
    ITOV_fname = "" 

    ' call the procedure to create files. 
    Call Process_template_Click 

    ' note - we need to change the destination directory (DTOV_Directory, ITOV_Directory) to the place where pre-val tool n/w folder 
    If DTOV_fname <> "" Then 
     newfilename = Left(DTOV_fname, InStrRev(DTOV_fname, ".")) 

     If Dir(DTOV_Directory & newfilename & "txt") <> "" Then 
      ' remove the file with same name if already exists on the network folder 
      If Dir(network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt") <> "" Then 
      Kill network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      End If 

      ' create the file on the network folder 
      Name DTOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      final_msg = final_msg & " " & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
     End If 

     If Dir(DTOV_Directory & Replace(newfilename, "DTOV", "CUST") & "txt") <> "" Then 
      ' get the new generated txt TOV file name 
      newfilename = Replace(newfilename, "DTOV", "CUST") 
      ' remove the file with same name if already exists on the network folder 
      If Dir(network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt") <> "" Then 
      Kill network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      End If 

      ' create the file on the network folder 
      Name DTOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      final_msg = final_msg & " " & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
     End If 
    End If 

    If ITOV_fname <> "" Then 
     newfilename = Left(ITOV_fname, InStrRev(ITOV_fname, ".")) 

     If Dir(ITOV_Directory & newfilename & "txt") <> "" Then 
      ' remove the file with same name if already exists on the network folder 
      If Dir(network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt") <> "" Then 
       Kill network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      End If 
      ' create the file on the network folder 
      Name ITOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      final_msg = final_msg & " " & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
     End If 

     If Dir(ITOV_Directory & Replace(newfilename, "ITOV", "CUST") & "txt") <> "" Then 
      ' get the new generated txt TOV file name 
      newfilename = Replace(newfilename, "ITOV", "CUST") 
      ' remove the file with same name if already exists on the network folder 
      If Dir(network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt") <> "" Then 
       Kill network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      End If 
      ' create the file on the network folder 
      Name ITOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
      final_msg = final_msg & " " & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
     End If 
    End If 

    If DTOV_fname <> "" Or ITOV_fname <> "" Then 
     final_msg = final_msg & vbNewLine & "The processing will take upto 10 minutes." 
     final_msg = final_msg & vbNewLine & "You will receive email notification once validation process is complete." 
     final_msg = final_msg & vbNewLine & "You can track the status of the file and review the error using the cognos browser - link to which will be present in the email to you" 
     MsgBox final_msg 
    End If 

Else 
    MsgBox ("Network folder is un-accessible. Please verify your access or the network folder path") 
End If 


End Sub 


Private Sub Process_template_Click() 

If DTOV_filename <> "" Then 
    DTOV_Directory = Left(DTOV_filename, InStrRev(DTOV_filename, "\")) 
    DTOV_fname = Dir(DTOV_filename) 
End If 

If ITOV_filename <> "" Then 
    ITOV_Directory = Left(ITOV_filename, InStrRev(ITOV_filename, "\")) 
    ITOV_fname = Dir(ITOV_filename) 
End If 

If DTOV_chkbox.Value = True And ITOV_chkbox.Value = True And DTOV_filename <> "" And ITOV_filename <> "" Then 
    Call Template_Process.Process_Templates(DTOV_Directory, DTOV_fname, ITOV_Directory, ITOV_fname) 
ElseIf DTOV_chkbox.Value = True And DTOV_filename <> "" And Raw_file.Value = False Then 
    Call Template_Process.Process_template(DTOV_Directory, DTOV_fname, "D") 
ElseIf DTOV_chkbox.Value = True And DTOV_filename <> "" And Raw_file.Value = True Then 
    Call Process_Raw(DTOV_Directory, DTOV_fname) 
ElseIf ITOV_chkbox.Value = True And ITOV_filename <> "" Then 
    Call Template_Process.Process_template(ITOV_Directory, ITOV_fname, "I") 
Else 
    MsgBox "No file selected. Please select a file to proceed" 
End If 

End Sub 


Private Sub Raw_file_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 
    File_category_frame_1.Caption = "Choose RAW File" 
    DTOV_chkbox.Caption = "RAW (Without Graphical Information)" 
    DTOV_chkbox = False 
    DTOV_filename = "" 
    File_category_frame_2.Visible = False 
    ITOV_chkbox.Visible = False 
    ITOV_filename.Visible = False 
End Sub 

新的错误PIC enter image description here

+1

你可以尝试写一个小例子,你在哪里收到了同样的错误?使用这种'名称DTOV_Directory&newfilename& “TXT” 作为network_path和替换(newfilename “EFPIA”, “EFPIA_PVLDTN”)和 “TXT”'因此,它会更容易复制它和帮助。 – Vityata

+1

您是否认为只通过提取[mcve]并删除其余部分可以使问题变得更加细化? – miroxlav

回答

1

一般来说,52是一个坏的文件名错误 - https://msdn.microsoft.com/en-us/library/aa231024(v=vs.60).aspx

因此,可能如果你把在文件名和txt之间的一个点.,它可以工作:

Name DTOV_Directory & newfilename & ".txt" As network_path & _ 
    Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & ".txt" 
+0

嗨Vityata。感谢您的回复我在我的问题中犯了一个错字我得到的实际错误是Run TIme error 53 file not found。不像前面说的那样错误52。我不知道这是否有所作为? –

+1

嗨Vityata。感谢您在“Private Sub Pre_validate_Click()”区域中用“.txt”替换“txt”的答案。非常尊重都柏林,再次感谢您的帮助。 :-)祝你美好的一天 –

+0

@PhilipConnell - 欢迎您:) – Vityata

1

您没有提供确切的文件路径(其中一个为无效[=错误52]),但你可以帮助自己通过更换故障声明

Name DTOV_Directory & newfilename & "txt" As network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 

与(暂时地)展开当量:

' assign file names to variables and perform renaming 
oldFileName = DTOV_Directory & newfilename & "txt" 
newFileName = network_path & Replace(newfilename, "EFPIA", "EFPIA_PVLDTN") & "txt" 
Name oldFileName As NewFileName 

当程序被停止(使用Stop声明或(更好地)使用断点 - F9键),将鼠标悬停在变量名称上以检查其内容(或在立即窗口中使用手表或print命令,...)。

也许你会发现这些潜在的问题:

  • 的文件扩展名缺少点
  • 缺少反斜杠从文件分隔目录等
  • 问题,像网络路径的问题。

编辑:错误处理

在子Pre_validate_Click(),你有3个Name命令。围绕每一个通过On Error Goto

On Error Goto RenameError ' error handling on 
Name ... As ... 
On Error Goto 0 ' error handling off 

而就在End Sub在子程序的末尾添加以下内容:

Exit Sub 

RenameError: 
If Err.Number = 53 Then 
    details = " when renaming from " & oldFileName & " to " & newFileName 
Else 
    details = "" 
EndIf 
answer = MsgBox("Error " & Err.Number & ": " & Err.Description & details, vbRetryCancel Or vbCritical) 
If answer = vbRetry Then 
    Resume ' retry problematic line again 
Else 
    End ' terminate the program 
End If 
+0

Miroxlav嗨。感谢您的回应。我得到的实际错误是未找到运行时错误53文件。没有错误52.路线是\\ NASPRDSFT \ EFPIA_COUNTRY_FOLDERS \ VALID \奇怪的是,它适用于我的团队的一些成员,但不适用于其他人。我检查了驱动器映射,所有相同 –

+0

*“它为我的团队的一些成员,但不是为别人” * - 你检查** **所有用户都具有正确的权限写入文件有问题的文件夹? – CLR

+0

@PhilipConnell - 所以现在你有明确的方向继续检查 - 它在你的网络共享/驱动器,而不是在你的代码。你可以在代码中执行什么操作:使用'On Error GoTo'语句来转移错误情况下的处理,并显示错误消息框,如'错误53:无法找到文件\\ share1 \ directory \ file.txt。'这可以让您的用户在没有程序崩溃的情况下了解问题。你知道吗,还是你需要帮助? – miroxlav