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.
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?
Sincerely, Richard
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
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.
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.
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.
You just want to F5 a window, basically, right? That's easy. Just focus it using
WshShell.AppActivate
and send F5 withWshShell.SendKeys
.Returning the focus to the console window can be a little tricky, though. The easiest way to do this is to set the console window's title, then refocus based on title after you've performed the refresh.
If you'd rather not mess with your console window title, you could focus by PID instead. This isn't terribly easy, though. If you have multiple console windows open, how do you determine which PID belongs to the active console?
This example
.bat
script that demonstrates spawning a child process, then walk up the ParentProcessID line until the root cmd process is found and use that PID. It takes a couple of seconds since WMI queries toWin32_Process
are slow, but it is what it is. *shrug*With the cmd window's PID known, the rest is easy. Activate your window you wish to refresh, send F5, then re-activate PID.
Sincerely, Not Richard