![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | stand-alone vb script for automation I have some code I wrote in an Excel macro that accesses both my Outlook inbox and creates Word documents. I now have a need to do a lot of the same basic techniques (accessing my Outlook mail) but simply as a command line utility rather than within Excel. I started by taking the code from the macro, cutting out all of the excel stuff, and pasting it into a project in VB 2008 Express. I immediately got a bunch of errors, but I suspect they all flow from the first. Initial few lines of code are: Sub Main() Dim objOLApp As Outlook.Application Dim objOLNameSpace As Outlook.NameSpace and the very first error is: Type 'Outlook.Application' is not defined. While I'm pretty comfortable with coding vbs, the various flavors of VB environments is not a strong suit, as that is not my primary job function. I just find myself needing to throw together a script every now and then, and try to get a little more sophisticated and knowledgable each time. Hints? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: stand-alone vb script for automation "EdStevens" <EdStevens@xxxxxx> wrote in message news:48B88D4C-AF1B-42A6-B183-21CD13C9AD1B@xxxxxx Quote: >I have some code I wrote in an Excel macro that accesses both my Outlook > inbox and creates Word documents. I now have a need to do a lot of the > same > basic techniques (accessing my Outlook mail) but simply as a command line > utility rather than within Excel. > > I started by taking the code from the macro, cutting out all of the excel > stuff, and pasting it into a project in VB 2008 Express. I immediately > got a > bunch of errors, but I suspect they all flow from the first. > > Initial few lines of code are: > > Sub Main() > Dim objOLApp As Outlook.Application > Dim objOLNameSpace As Outlook.NameSpace > > > and the very first error is: > > Type 'Outlook.Application' is not defined. > > > While I'm pretty comfortable with coding vbs, the various flavors of VB > environments is not a strong suit, as that is not my primary job function. > I > just find myself needing to throw together a script every now and then, > and > try to get a little more sophisticated and knowledgable each time. > > Hints? and invoke it like so: cscript //nologo c:\myscript.vbs or wscript c:\myscript.vbs Here are some sample lines of code that will handle Outlook or Excel applications: Option Explicit Outlook Excel '---------------------------------------------- 'Code copied from the Scripting Guys help file. ' 'List all mail items that are currently in the Inbox. 'Note that Outlook must be active at the time. ' 'See here for default folder numbers: 'http://msdn2.microsoft.com/en-us/library/aa219371(office.11).aspx ' 'For a full list of the mail object properties, see here: 'http://msdn2.microsoft.com/en-us/library/aa271898(office.11).aspx 'e.g. body, sent, size '---------------------------------------------- Sub Outlook Const InboxFolder = 6 Dim oOutlook, oNamespace, oFolder, oMail Set oOutlook = Createoect("Outlook.Application") Set oNamespace = oOutlook.GetNamespace("MAPI") Set oFolder = oNamespace.GetDefaultFolder(InboxFolder) For Each oMail In oFolder.items WScript.echo oMail.subject Next End Sub '------------------- 'Excel sample script '------------------- Sub Excel Dim oExcel, oWorkbook, oWorksheet, oRange Set oExcel = Createoect ("Excel.Application") oExcel.Visible = True Set oWorkbook = oExcel.Workbooks.Open("d:\Sample.xls") Set oWorksheet = oExcel.ActiveWorkbook.Worksheets(1) oWorksheet.Activate Set oRange = oWorksheet.range("A1:B100") End Sub |
My System Specs![]() |
| | #3 (permalink) |
| | Re: stand-alone vb script for automation On Dec 17, 8:12*am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote: Quote: > "EdStevens" <EdStev...@xxxxxx> wrote in message > > news:48B88D4C-AF1B-42A6-B183-21CD13C9AD1B@xxxxxx > > > > > Quote: > >I have some code I wrote in an Excel macro that accesses both my Outlook > > inbox and creates Word documents. *I now have a need to do a lot of the > > same > > basic techniques (accessing my Outlook mail) but simply as a command line > > utility rather than within Excel. Quote: > > I started by taking the code from the macro, cutting out all of the excel > > stuff, and pasting it into a project in VB 2008 Express. *I immediately > > got a > > bunch of errors, but I suspect they all flow from the first. Quote: > > Initial few lines of code are: Quote: > > Sub Main() > > * * * *Dim objOLApp As Outlook.Application > > * * * *Dim objOLNameSpace As Outlook.NameSpace Quote: > > and the very first error is: Quote: > > * * *Type 'Outlook.Application' is not defined. Quote: > > While I'm pretty comfortable with coding vbs, the various flavors of VB > > environments is not a strong suit, as that is not my primary job function. > > I > > just find myself needing to throw together a script every now and then, > > and > > try to get a little more sophisticated and knowledgable each time. Quote: > > Hints? > If you want a stand-alone application then you should write it in VB Script > and invoke it like so: > cscript //nologo c:\myscript.vbs * or > wscript *c:\myscript.vbs > > Here are some sample lines of code that will handle Outlook or Excel > applications: > > Option Explicit > Outlook > Excel > > '---------------------------------------------- > 'Code copied from the Scripting Guys help file. > ' > 'List all mail items that are currently in the Inbox. > 'Note that Outlook must be active at the time. > ' > 'See here for default folder numbers: > 'http://msdn2.microsoft.com/en-us/library/aa219371(office.11).aspx > ' > 'For a full list of the mail object properties, see here: > 'http://msdn2.microsoft.com/en-us/library/aa271898(office.11).aspx > 'e.g. body, sent, size > '---------------------------------------------- > Sub Outlook > *Const InboxFolder = 6 > *Dim oOutlook, oNamespace, oFolder, oMail > > *Set oOutlook = Createoect("Outlook.Application") > *Set oNamespace = oOutlook.GetNamespace("MAPI") > *Set oFolder = oNamespace.GetDefaultFolder(InboxFolder) > > *For Each oMail In oFolder.items > * WScript.echo oMail.subject > *Next > End Sub > '------------------- > 'Excel sample script > '------------------- > Sub Excel > *Dim oExcel, oWorkbook, oWorksheet, oRange > > *Set oExcel = Createoect ("Excel.Application") > *oExcel.Visible = True > *Set oWorkbook = oExcel.Workbooks.Open("d:\Sample.xls") > *Set oWorksheet = oExcel.ActiveWorkbook.Worksheets(1) > *oWorksheet.Activate > *Set oRange = oWorksheet.range("A1:B100") > End Sub- Hide quoted text - > > - Show quoted text - as a kind of backend for batch routines that just aren't complicated enough :P If nothing else, you can just use objShell.Run "Outlook" To start outlook and use a bunch of objShell.SendKeys "blahblahblah" For all the keyboard shortcuts and selections for copy and paste, obviously seperated by occasional wscript.Sleep #### To pause for load times and lag and whatnot. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: stand-alone vb script for automation "Zach" <Der.Stinktier.Meister@xxxxxx> wrote in message news:39e3f27e-f8e2-4de6-a7ff-f6eb0efce345@xxxxxx On Dec 17, 8:12 am, "Pegasus \(MVP\)" <I....@xxxxxx> wrote: Quote: > "EdStevens" <EdStev...@xxxxxx> wrote in message > > news:48B88D4C-AF1B-42A6-B183-21CD13C9AD1B@xxxxxx > > > > > Quote: > >I have some code I wrote in an Excel macro that accesses both my Outlook > > inbox and creates Word documents. I now have a need to do a lot of the > > same > > basic techniques (accessing my Outlook mail) but simply as a command > > line > > utility rather than within Excel. Quote: > > I started by taking the code from the macro, cutting out all of the > > excel > > stuff, and pasting it into a project in VB 2008 Express. I immediately > > got a > > bunch of errors, but I suspect they all flow from the first. Quote: > > Initial few lines of code are: Quote: > > Sub Main() > > Dim objOLApp As Outlook.Application > > Dim objOLNameSpace As Outlook.NameSpace Quote: > > and the very first error is: Quote: > > Type 'Outlook.Application' is not defined. Quote: > > While I'm pretty comfortable with coding vbs, the various flavors of VB > > environments is not a strong suit, as that is not my primary job > > function. > > I > > just find myself needing to throw together a script every now and then, > > and > > try to get a little more sophisticated and knowledgable each time. Quote: > > Hints? > If you want a stand-alone application then you should write it in VB > Script > and invoke it like so: > cscript //nologo c:\myscript.vbs or > wscript c:\myscript.vbs > > Here are some sample lines of code that will handle Outlook or Excel > applications: > > Option Explicit > Outlook > Excel > > '---------------------------------------------- > 'Code copied from the Scripting Guys help file. > ' > 'List all mail items that are currently in the Inbox. > 'Note that Outlook must be active at the time. > ' > 'See here for default folder numbers: > 'http://msdn2.microsoft.com/en-us/library/aa219371(office.11).aspx > ' > 'For a full list of the mail object properties, see here: > 'http://msdn2.microsoft.com/en-us/library/aa271898(office.11).aspx > 'e.g. body, sent, size > '---------------------------------------------- > Sub Outlook > Const InboxFolder = 6 > Dim oOutlook, oNamespace, oFolder, oMail > > Set oOutlook = Createoect("Outlook.Application") > Set oNamespace = oOutlook.GetNamespace("MAPI") > Set oFolder = oNamespace.GetDefaultFolder(InboxFolder) > > For Each oMail In oFolder.items > WScript.echo oMail.subject > Next > End Sub > '------------------- > 'Excel sample script > '------------------- > Sub Excel > Dim oExcel, oWorkbook, oWorksheet, oRange > > Set oExcel = Createoect ("Excel.Application") > oExcel.Visible = True > Set oWorkbook = oExcel.Workbooks.Open("d:\Sample.xls") > Set oWorksheet = oExcel.ActiveWorkbook.Worksheets(1) > oWorksheet.Activate > Set oRange = oWorksheet.range("A1:B100") > End Sub- Hide quoted text - > > - Show quoted text - as a kind of backend for batch routines that just aren't complicated enough :P If nothing else, you can just use objShell.Run "Outlook" To start outlook and use a bunch of objShell.SendKeys "blahblahblah" For all the keyboard shortcuts and selections for copy and paste, obviously seperated by occasional wscript.Sleep #### To pause for load times and lag and whatnot. ======================== It should be noted that your solution is based on keyboard macros and is therefore fragile, like all macro programs. It can (and will!) be sent off the rails by a number of events such as - A virus scanner pattern file update pop-up - An unexpected response from the application - A certain action taking much longer than anticipated If the OP avoids keyboard macros in his VB Script code then he will also avoid all of these traps. |
My System Specs![]() |
| | #5 (permalink) |
| | Re: stand-alone vb script for automation "Pegasus (MVP)" wrote: Quote: > > <snip> > > If you want a stand-alone application then you should write it in VB Script Quote: > and invoke it like so: > cscript //nologo c:\myscript.vbs or > wscript c:\myscript.vbs > prompt (C:> myprog.vbs) that the command processor understood .vbs. But that's a nit. Quote: > Here are some sample lines of code that will handle Outlook or Excel > applications: > Actually, the code I had worked fine when executed within Excel .. even the access to Outlook mail. But the same code failed from the git-go in VB Express environment. After kicking at some half-dead brain cells, I thought to check Project References ... sure enough, Outlook had to be added in. After that most of my compile issues went away and the two remaining were easy fixes. So, once I get the code fleshed out, what's the best way to deploy it as a script to execute from the command line? And since I am going for script, not a full-blown VB graphical interface program, is there some other development environment I should be using? Quote: > |
My System Specs![]() |
| | #6 (permalink) |
| | Re: stand-alone vb script for automation See below. "EdStevens" <EdStevens@xxxxxx> wrote in message news:7B912886-7C80-47F6-83F8-105248EADAAB@xxxxxx Quote: > > > "Pegasus (MVP)" wrote: > Quote: >> >> <snip> >> >> If you want a stand-alone application then you should write it in VB >> Script > yes, that is my intent > Quote: >> and invoke it like so: >> cscript //nologo c:\myscript.vbs or >> wscript c:\myscript.vbs >> > prompt (C:> myprog.vbs) that the command processor understood .vbs. But > that's a nit. could not understand VB Scripts. It's now called the Command Prompt. And yes, associations are set by default so that .vbs files invoke wscript.exe. I prefer not to rely on it for robust applications and state explicitly that I want the code to be run by wscript.exe. Quote: Quote: >> Here are some sample lines of code that will handle Outlook or Excel >> applications: >> > > Actually, the code I had worked fine when executed within Excel .. even > the > access to Outlook mail. But the same code failed from the git-go in VB > Express environment. After kicking at some half-dead brain cells, I > thought > to check Project References ... sure enough, Outlook had to be added in. > After that most of my compile issues went away and the two remaining were > easy fixes. > > So, once I get the code fleshed out, what's the best way to deploy it as a > script to execute from the command line? And since I am going for script, > not a full-blown VB graphical interface program, is there some other > development environment I should be using? using. As far as launching the script - create a shortcut to the command line I gave you in my first reply so that you an invoke it from the desktop. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| IE automation question | VB Script | |||
| stand alone for WLM | Live Mail | |||
| Need help with powerpoint automation... | VB Script | |||
| Fax Stand Alone ? | Vista General | |||
| stand by | Vista account administration | |||