如何在VBA for Windows中重命名文件之前知道文件的创建时间

时间:2018-03-26 16:28:07

标签: excel-vba vba excel

我对这个问题感到非常头疼,但仍未钉牢。

我使用VBA宏创建了许多文件,在我去的时候重命名它们(这与旧版Excel(2003)中的PDF创建有关,使用外部PDF编写器,要求初始文件名是默认文件名,如果你想知道)

我希望宏在尝试重命名之前等到创建文件。然后我希望宏等到文件重命名后再创建另一个文件。这并不像听起来那么容易。尝试Do While Dir(文件名)<>"" - 循环并执行Dir(文件名)=""要么生成路径文件访问错误(再次,自行运行的结果 - 它在恢复时工作)或锁定计算机....

非常感谢任何帮助。

Sub RenameIt(Cpy As String, CName As String)

    Do
    Loop Until Dir(Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf") <> ""

    Name Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf" As Worksheets("Workpad").[b1] & _
    "NEW INVOICES\FOR EMAILING\" & CName & Cpy & ".pdf"

    Do While Dir(Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf") <> ""
    Loop

End Sub

...创建文件路径访问错误

Sub RenameIt(Cpy As String, CName As String)

    On Error Resume Next
    Do
        Err.Clear
        Do
        Loop Until Dir(Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf") <> ""

        Name Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf" As Worksheets("Workpad").[b1] & _
        "NEW INVOICES\FOR EMAILING\" & CName & Cpy & ".pdf"

        Do While Dir(Worksheets("Workpad").[b1] & "NEW INVOICES\FOR EMAILING\To Email.pdf") <> ""
        Loop
    Loop Until Err.Number = 0

End Sub

...在创建第一个文件(未重命名)

后锁定计算机

1 个答案:

答案 0 :(得分:1)

回顾。情况是您启动一个开始写入文件的异步进程,并且您希望在异步进程完成后重命名该文件。但是,您不知道该过程何时完成。看起来好像打印过程创建了文件,然后开始写入文件。在此期间,操作系统会保持文件锁定,并且无法重命名。

您当前的解决方案不会测试package eHMS_package; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; public class EHMS_Login { public WebDriver driver; public static void main(String[] args) { System.setProperty("webdriver.ie.driver", "E:/SRIKANTH/IEDriver/IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); driver.navigate().to("http://google.co.in"); } } 操作是否成功。文件被锁定时会失败。您需要在手术后立即检查。

Started InternetExplorerDriver server (64-bit)
3.9.0.0 Listening on port 48107 Only local connections are allowed Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:48107 Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z' System info: host: 'UMANATH-PC', ip: '10.1.0.25', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_161' Driver info: driver.version: InternetExplorerDriver    at 

org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:209)
        at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:223)
        at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:215)
        at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:152)
        at eHMS_package.EHMS_Login.main(EHMS_Login.java:15)
    Caused by: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:48107
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:240)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:158)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall.execute(RealCall.java:77)
        at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:101)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:101)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        ... 6 more
    Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at okhttp3.internal.platform.Platform.connectSocket(Platform.java:125)
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
        ... 28 more

应该做的伎俩。这样做会重复,直到文件不再被打印过程锁定。对于人类而言,它的睡眠时间可以忽略不计,但每次迭代都需要足够长的时间用于计算机。您可能还想添加一个循环计数器,以确保操作在一定时间后中止而不是挂起。