VBA批处理脚本-刷新文件夹 [英] VBA batch script - refresh folder

查看:351
本文介绍了VBA批处理脚本-刷新文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使用脚本下载pdf文件,将其更改为txt文件,从txt文件中提取信息以重命名pdf文件并将其保存到某个文件夹.除了VBA,我还使用了一些批处理脚本来实现此目的.临时pdf文件首先保存到该文件夹​​中,然后再从该文件夹中删除.

Currently I use a script to download a pdf-file, change it to a txt-file, extract info from the txt-file to rename the pdf-file and save it to a certain folder. Besides VBA I use a couple of batch scripts to achieve this. A temporary pdf-file is at first saved to the folder and later on deleted from the folder.

如果我进入该文件夹,则需要手动刷新它,那么我将不再看到该文件.我在网上查看是否有用于刷新或更新文件夹的批处理脚本或vba,但是找不到任何有用的东西. 有没有人知道如何通过VBA或批处理脚本来完成此任务?

If I go to the folder I need to refresh it manually, then I will not see the file anymore. I looked on the web to see if there was a batch script or vba to refresh or update a folder, but I could not find anything nearly usefull. Is there someone who knows how this can be accomplished through VBA or a batch script?

此致, 理查德

从这里22:18编辑 @Rojo和其他人:我正在寻找将在后台刷新文件夹的代码.如果下面的代码处于打开状态,则将其转到该文件夹​​,然后刷新并返回Outlook.不要在Outlook的VBA编辑器中使用它,因为它会进入一个令人讨厌的循环,您可以通过单击Outlook停止该循环.但是,请使用按钮或其他按钮来执行代码.

Edited from here at 22:18 @Rojo and others: I am looking for code which will refresh a folder in the background. The code below will go to the folder if it is open, refresh it and go back to Outlook. Do not use it out of the VBA Editor in Outlook, because it will go into an annoying loop, which you can just stop by clicking on Outlook. But use a button or some else to execute the code.

Sub ActivateOutlook()
   On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")
   If err.Number = 429 Then 
      MsgBox "Outlook is not running"
   Else
      AppActivate objOutlook.ActiveExplorer.Caption
   End If
End Sub

Sub RefreshSavedFiles()
Dim oShellObject
Set oShellObject = CreateObject("Wscript.Shell")
strFolder = "C:\Users\User\Documents\PDF files saved"
oShellObject.AppActivate strFolder
oShellObject.SendKeys "{F5}"
ActivateOutlook
End Sub

推荐答案

查看Rojo和David Ruhmann的答案,并且无法在网络上找到有关此问题的有用信息,也无法在Stack Overflow之类的知名网站上找到任何有用的信息,下面的代码将是我的问题的最佳选择.

Looking at the answers from Rojo and also David Ruhmann and not being able to find anything useful on the web on this issue, also not on renowned websites like Stack Overflow, the code below will be the best alternative to my question.

下面的代码几乎看不见地刷新了文件夹,我之所以这样说,几乎是因为有时会显示一条消息,表明该文件夹未打开,在我测试它时,它只发生过两次. 在2015年2月23日我添加了一个Do .. Loop While以防止出现该消息,到目前为止,我再也没有收到该文件夹​​未打开的消息.因此,代码已按我的喜好进行了改进,并决定我自己的答案就是我的问题的答案.

The code below will refresh a folder almost invisibly, I say almost because sometimes a message will show that the folder is not open, it just happened two times when I tested it. Edit on 23.02.2015: I added a Do.. Loop While to prevent the message from coming up and so far I did not get the message again that the folder is not open. So the code has improved to my liking and decided my own answer is the answer to my question.

代码有什么作用? 在Outlook中,它将在预定义目录中打开资源管理器窗口而不显示它,刷新它并在一秒钟内再次关闭该窗口,然后返回Outlook.我将在这里和那里找到的代码放在一起并对其进行了更改,以使其对我有用.也许它也对您有用.

What does the code do? From Outlook it will open an Explorer window in a predefined directory without showing it, refresh it and close the window again within a second and go back to Outlook. I put together and altered code I found here and there to make it useful for me. Maybe it can also be of use for you.

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10

Private Sub CloseExplorer()
Dim GetOpenWindow As Long
Dim OpenFolder As String
Dim WindowSuccesfullyClosed As Boolean
OpenFolder = "C:\Users\User\Documents\PDF files saved"
GetOpenWindow = FindWindow(vbNullString, OpenFolder)
Do
WindowSuccesfullyClosed = False
Counter = Counter + 1
Sleep (5) 'milliseconds
If GetOpenWindow <> 0 Then
PostMessage GetOpenWindow, WM_CLOSE, 0&, 0&
WindowSuccesfullyClosed = True
Exit Do
End If
Loop While Not GetOpenWindow <> 0 And Counter < 100
If WindowSuccesfullyClosed = False Then
MsgBox OpenFolder & " is not open."
Else
End If
End Sub

Sub ActivateOutlook()
   On Error Resume Next
   Set objOutlook = GetObject(, "Outlook.Application")
   If err.Number = 429 Then 
      MsgBox "Outlook is not running"
   Else
      AppActivate objOutlook.ActiveExplorer.Caption
   End If
End Sub

Sub RefreshSavedFiles()
Set wsh = VBA.CreateObject("WScript.Shell")
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 0 'windowStyle = 1 when visible
    Dim OpenFolder As String
OpenFolder = "C:\Users\User\Documents\PDF files saved"
wsh.Run "%windir%\explorer.exe /n," & OpenFolder, windowStyle, waitOnReturn
wsh.AppActivate OpenFolder
wsh.SendKeys "{F5}"
CloseExplorer
ActivateOutlook
Set wsh = Nothing
End Sub

这篇关于VBA批处理脚本-刷新文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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