循环文件 - 工作得很好,停止工作 [英] Loop Through Files - worked good, stopped working

查看:113
本文介绍了循环文件 - 工作得很好,停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tom,

继续我们之前的对话后,我正在使用下面的这个VBA,它在包含59个文件的文件夹和另一个59个文件的文件夹中运行良好子项目,并且没有资源池。

Following on from our previous conversation, I am using this VBA below, and it works great in a folder with 59 files and another file where the 59 are subprojects, and there is no resource pool.

但是,当我更改路径名时,

However, when I change the path name from what works

FolderPath =" X:\ PROJECTS \ PLANNING \完美的项目计划\工艺计划\还没有OK \ 5000系列工作\"

FolderPath = "X:\PROJECTS\PLANNING\Perfect Project Planning\Draft Plans\Not Yet OK\5000 Series Jobs\"

到 

FolderPath =" X:\ PROJECTS \ PLANNING \完美的项目计划\Draft Plans \ OK \"

FolderPath = "X:\PROJECTS\PLANNING\Perfect Project Planning\Draft Plans\OK\"

它不起作用,它停在

FileOpenEx Name:= FileName,ReadOnly:= False,FormatID:=" MSProject.MPP"。

FileOpenEx Name:=FileName, ReadOnly:=False, FormatID:="MSProject.MPP".

在OK文件夹中,有一个共享资源池,这是我可以指出的唯一区别。所以我想我应该在运行宏之前打开池,但这没有帮助

In the OK folder, there is a shared resource pool, and that's the only difference I can put my finger on. So I figure I should open the pool before running the macro, but that doesn't help

推荐答案

嗨Trevor,

Hi Trevor,

我们不知道你的文件,这里是我要调试的步骤:

Not knowing anything about your files, here are the steps I would take to debug:

1。了解宏在崩溃前的距离以及崩溃时的条件。 最快的方法是在代码中输入两个命令:

1. Find out how far the macro gets before crashing and what the conditions are when it crashes.  The quickest way to do this is to enter two commands into your code:


  • 在Do While语句之后和FileOpenEx语句之前,键入:Debug.Print FileName
  • 在FileName = Dir()语句之前,键入:Debug.Print" Success"

这些命令打印到VB中的立即窗口编辑器,所以确保它可见。

These commands print to the Immediate window in the VB editor, so make sure it's visible.

2。当宏失败时,检查Locals窗口中的值是否有意外。

2. When the macro fails, inspect the values in the Locals window for anything unexpected.

由于您突出显示了与资源池文件相关的唯一性,因此您可能希望修改要生成的代码确保资源池文件是a)首先由代码打开; b)被排除在Do循环中的任何行动,包括尝试重新开启或
关闭。 (例如,If Not FileName =" ResourcePool.mpp" then ....);并且c)在循环结束后最后关闭。

Since you've highlighted a uniqueness related to the resource pool file, you may want to modify the code to make sure that the resource pool file is a)opened first by the code; b)excluded from any actions in the Do Loop, including attempts to re-open or to close. (e.g. If Not FileName = "ResourcePool.mpp" then....); and c) closed last, after the loop is finished.

最后,习惯于a)使用F8键一次一行地执行代码和/或b)输入断点暂停执行并允许在关键点检查局部变量和/或c)使用Debug.Assert命令暂停执行
并允许在满足某些条件时进行检查。

Eventually, get used to a) stepping through your code one line at a time using the F8 key and/or b) Entering breakpoints to suspend execution and allow inspection of local variables at key points and/or c) Using the Debug.Assert command to suspend execution and allow inspection when certain conditions are met.

您的代码根本没有错误处理。 几乎所有命令都可能遇到意外情况并抛出错误。 如果没有任何其他错误处理指令,VBA将暂停代码并进入调试模式,突出显示
导致错误的命令。 三个典型的错误处理命令是:

Your code has no error handling at all.  Virtually all commands can encounter unexpected conditions and throw an error.  Absent any other error-handling instructions, VBA will just suspend code and enter debug-mode, highlighting the command that causes the error.  Three typical error-handling commands are:

On Error Resume Next - 这将存储异常变量"Err"。并继续执行下一个命令。 您可以创建下一个命令:If Err.number<> 0然后[做一些特殊的事情来处理错误],或者你可以跳过那一步,只需
允许代码继续并忽略所有错误。

On Error Resume Next - this stores an exception variable "Err" and proceeds to the next command.  You can make the next command: If Err.number <> 0 then [do something special to handle the error], or you could skip that step and just allow code to proceed and ignore all errors.

On Error GoTo ErrHandler - this将控制转移到由标签标识的代码中的另一个点,在本例中为"ErrHandler:"

On Error GoTo ErrHandler - this transfers control to another point in the code identified by a label, which in this case is "ErrHandler:"

On Error GoTo 0 - 恢复为默认行为。

On Error GoTo 0 - reverts to the default behavior.

最后你可能想买Rod的书,如果你还没有。 这是一个很好的投资。

Eventually you may want to buy Rod's book, if you haven't already.  It's a good investment.

让我知道它是怎么回事。

Let me know how it goes.

   ;

  


这篇关于循环文件 - 工作得很好,停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆