Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - stand-alone vb script for automation

Reply
 
Old 12-16-2008   #1 (permalink)
EdStevens


 
 

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 SpecsSystem Spec
Old 12-17-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

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?
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


My System SpecsSystem Spec
Old 12-17-2008   #3 (permalink)
Zach


 
 

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 -
I agree with this. I've been working with cscript/wscript a bit lately
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 SpecsSystem Spec
Old 12-17-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

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 -
I agree with this. I've been working with cscript/wscript a bit lately
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 SpecsSystem Spec
Old 12-17-2008   #5 (permalink)
EdStevens


 
 

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
yes, that is my intent
Quote:

> and invoke it like so:
> cscript //nologo c:\myscript.vbs or
> wscript c:\myscript.vbs
>
Ok, I had seen somewhere that .vbs could be executed directly at the dos
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:
>
<snip>

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 SpecsSystem Spec
Old 12-17-2008   #6 (permalink)
Pegasus \(MVP\)


 
 

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
>>
> Ok, I had seen somewhere that .vbs could be executed directly at the dos
> prompt (C:> myprog.vbs) that the command processor understood .vbs. But
> that's a nit.
"DOS"? That's an operating system introduced some 30 years ago. It
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:
>>
> <snip>
>
> 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?
Can't tell - you're not telling us what development environment you're
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46