convert msg to html in background using vbscript - vbscript

I'm converting an Outlook msg file to an html file. So far I have:
Dim objshell,BaseName,outlookapp,emailPath
Set objshell= CreateObject("scripting.filesystemobject")
Set outlookapp = CreateObject("Outlook.Application")
Set email = outlookapp.CreateItemFromTemplate(emailPath)
emailPath = "C:\Users\makkerman\Desktop\email folder\test.msg"
BaseName = objshell.GetBaseName(emailPath)
email.saveas objshell.GetParentFolderName(emailPath) & "\" & BaseName & ".html", 5
outlookapp.Quit
I'd like this to run in the background (without disturbing the user who runs it). Do I have to start an Outlook process? As it currently stands, if the user has Outlook open, then the above script closes Outlook and I can understand why (outlookapp.Quit). If Outlook isn't open when the script is run, Outlook opens for the script's duration.
Can someone please nudge me in the right direction? Thank you!

Why do you need to call Application.Quit? If Outlook was running, it will stay running. If it was not running, Outlook will close itself when you de-reference all Outlook objects - keep in mind that Outlook is a singleton and CreateObject will connect to the already running instance; you do not get a brand new process.
If you do not want to use Outlook, you can use Redemption (I am its author, it will not start Outlook):
set Session = CreateObject("Redemption.RDOSession")
set Msg = Session.GetMessageFromMsgFile("c:\temp\test.msg")
Msg.SaveAs "c:\temp\test.html", 5

I would create an instance of Outlook regardless if it's open or not. This should help: Run program minimized. Make sure to check out the docs link in the answer to get all parameter options.

Related

VBS doesn't open Outlook

I try to run a vbs to open Outlook after i scanned a doc, it scans the document on Windows 10, but doesn't open Outlook, have you guys any idea?
BR Alex
Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Set objOutlookAttach = objOutlookMsg.Attachments.Add(output)
objOutlookMsg.Display
Reason was a missing permission on the folder where the scanned file was stored

vbscript copy all rules applied to one PST file to another [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have two pst files. I want to apply all rules that are applied to first pst file to second pst file. Is it possible to do this with vbscript and without VBA. I think VBA can be run only on Outlook itself not as independent script.
I have two pst files. I want to apply all rules that are applied to first pst file to second pst file. Is it possible to do this with vbscript and without VBA.
Yes, it's possible to do this with VBScript because VBScript can use the same COM-based Outlook Office Automation API as VBA, except objects are late-bound (so if there are any typing errors in your script you won't know about them until you run it).
To convert VBA to VBScript, you need to do the following:
Copy and paste your VBA code into a *.vbs file.
Remove type information from your variable declarations
Optionally, but strongly recommended: add Option Explicit on line 1.
Change the Office COM automation interop code to use the late-bound CreateObject and GetObject functions instead of VBA/VB6-specific constructor or COM API calls.
' Change this VBA/VB6:
Set reminder = New Outlook.Reminder
' To this VBS:
Set reminder = CreateObject("Outlook.Reminder")
' Change this VBA/VB6:
Set app = Outlook.Application
' To this VBS:
Set app = GetObject("Outlook.Application")
So this VBA:
Sub Foo()
Dim foo As String
foo = ""
Dim reminder As Outlook.Reminder
Set reminder = Outlook.Application.Reminders.Item(1)
End Sub
...becomes this VBScript:
Option Explicit
Sub Foo()
Dim foo
foo = ""
Dim reminder
Set reminder = GetObject( "Outlook.Application" ).Reminders.Item(1)
End Sub
Call Foo ' Enter into the Foo() subroutine from the top-level script.
I think VBA can be run only on Outlook itself not as independent script.
This is true, however when using VBScript (when run from cscript, wscript or any other Active Scripting host, like IIS) you're still using the same Office automation API as VBA, so you still need Outlook to be installed on your computer and run the script in a normal desktop session (and not Session 0 or as a headless process). You also need to ensure your script's host has the same ISA ("bitness") as Outlook (i.e. x86 vs x64).

Can an already running program's window be hidden?

I have been using wsh to run vbscripts in conjunction with iMacros at my work for going on 3 years now. I know that the iMacros browser itself has an option to run hidden but the web-based db interface I have to work with does not function in the iMacros browser. For this reason I have had to use Internet Explorer. I have no problems with the script itself or how it runs. The thing I would like to improve is how the Internet Explorer window itself is handled.
Currently, when iMacros is initiated it creates the IE window in a non-maximized state cascaded from where the previous window was created. Because of iMacros's behavior, I then resize the window using an iMacros script so that enough of the page is rendered for the script to see everything it needs.
Set iim1 = CreateObject ("imacros")
iret = iim1.iimOpen ("-ie -iePrivate", TRUE, 300)
iret = iim1.iimPlayCode("SIZE X=" & scrWidth & " Y=" & scrHeight)
This all works fine and dandy. The scripts are scheduled to run at specific times, gather information out of the db and then imports that data into Excel spreadsheets and prints them out.
What I would like to do is make the IE window hidden while these scripts run. Because I do not create the IE window first (Set objIE = CreateObject("internetexplorer.Application")) I do not have access to the IE window object. I let iMacros create the window with the -iePrivate flag so that it will not disturb my own IE window if I should be logged in and working in the db while the script executes. However, it does try to take focus and become the active application while the script executes. Which can be very annoying at time. My goal is to be able to share these scripts with my co-workers but I don't want the IE windows popping up on them while they may be working on something else.
Is there a way to get to the IE object created by iMacros so that the window can be hidden?
This does Internet Explorer and Explorer windows (they used to be the same program).
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.locationname
Next

How can I automatically cancel the Enter Network Password prompt in Outlook?

I have an unattended PC which does a very simple task : read emails from a POP server and run various tasks based the content of those emails... 24/7/365
The problem is that at least once a week "something" glitches and the processing stalls because Outlook is waiting for a user to confirm their password with the dreaded Enter Network Password dialog.
I have read so many articles in the past 6 months in an attempt to fix the root cause and nothing has worked. There's no WiFi involved and the email server is Google so it should be extremely reliable so I can only suspect that the broadband connection to my ISP is the culprit as I've read that "certain network conditions" can trigger the password prompt.
Yesterday, I gave up on the root cause approach and looked for an automated tool to cancel the annoying dialogue box when it appears. To my surprise, Windows can do this out-of-the-box via Windows Script Host. Here's how:
A. Open your preferred text editor.
B. Copy paste the following VB Script (you can also use JScript if you prefer but I'm not including the syntax here).
Set WshShell = WScript.CreateObject("WScript.Shell")
' Move focus to the network password dialog if it exists (if not, script continues)
If WshShell.AppActivate ("Enter Network Password") = True Then
' Suspend the script for 1/10th second to make sure the dialogue is in focus
WScript.Sleep 100
WshShell.SendKeys "{ESC}"
' Optionally save this event to a log file
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\Temp", ForAppending, tristateFalse)
file.Write vbCrLf & Date & " " & Time
file.Close
End If
Set WshShell = Nothing
C. Save the file with a .vbs extension e.g. EscapePrompt.vbs
D. Next, open the Windows Task Scheduler (Click the Windows Start button and type "Task" and you should see Task Schedule appear at the top of the search result list, or just type "Task" from the Windows 8 tile view).
Create a new task that runs the script file above, every day, repeating every 5 minutes, indefinitely:
General tab : give the task a name and then check Run with highest privileges
New Trigger tab : click New and select Daily and then in the Advanced Settings, check Repeat task every: and choose 5 minutes. set the for a duration of: to Indefinitely. Check Enabled
Actions Tab : click New and then Browse to choose the script file you created above.
Conditions tab : Set any conditions as required by your environment.
Settings tab : I don't change anything here but you may want to.
Now, when the Enter Network Password prompt appears, there is a maximum delay of 5 minutes before it is automatically cancelled.
For more information on WScript and in particular, the SendKeys method, check out MSDN :
http://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx
You may also be interested in our free VBA examples at:
http://youpresent.biz/category/blog/vba/
Microsoft outlook keeps prompting network password if there registry value and network password mismatches. If you change the password for your account in ISP or Control Panel.Then, configure and login outlook with the new password.Then ,it will not prompt for outlook password again and again.

How can you have a dialog box displayed when opening a program in windows xp?

Does anyone know how to have a dialog box with a custom message pop up any time that a specific program is opened? The computer is running Windows XP Professional 2002 with Service Pack 3. The program is used to operate a scientific instrument that keeps getting damaged by lab users who disregard printed instructions around it. Ideally, I could have a dialog box pop up that requires users to click an "OK" button before the program opens. I've spent some time browsing the web for ideas, but have so far come back empty-handed. The program is usually opened from a shortcut on the desktop.
Many thanks,
Jeremy
A user on another forum answered this question for me. I created a small shell script (text file saved with .vbs extension) to display the dialog box when the program is opened from a Desktop shortcut targeting the script file.
Here's the link to the post in the other forum for more details:
The script that worked for me is shown below:
startupMessage = MsgBox("...close the lid gently and only use the button to open it.", 0, "To prevent instrument damage...")
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim cmd
cmd = "C:\Program Files\InstrumentApplication.exe"
cmd = chr(34) & cmd & chr(34)
WshShell.Run(cmd)

Resources