Tags: appears, command, consistent, dos, example, excel, execute, function, microsoft, msdn, run, running, shell, software, trouble, vba

Shell Function - Execute DOS Command

On Microsoft » Microsoft Excel

7,496 words with 6 Comments; publish: Fri, 23 May 2008 04:34:00 GMT; (30678.13, « »)

Excel 2000 running in XP:

Having trouble getting consistent results from VBA Shell Function.

For example, this command appears to run okay, but doesn't print cat.txt,

which is located in root directory. Is there something wrong with syntax?

Thanks ...

ID = Shell("command.com /c print cat.txt")

bill morgan

All Comments

Leave a comment...

  • 6 Comments
    • Can I suggest binning the use of shell? have a look at this link:

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsoriWshScriptExecObjectPropMeth.asp

      It's the Windows Script Host. It's much more user friendly that running

      shell (la la la).

      www.alignment-systems.com

      "bill_morgan" wrote:

      > Excel 2000 running in XP:

      > Having trouble getting consistent results from VBA Shell Function.

      > For example, this command appears to run okay, but doesn't print cat.txt,

      > which is located in root directory. Is there something wrong with syntax?

      > Thanks ...

      > ID = Shell("command.com /c print cat.txt")

      > bill morgan

      >

      #1; Fri, 23 May 2008 04:35:00 GMT
    • Would it work if you replaced the space in "print cat.txt" with say an

      underscore. If so try embracing the string in the shell function with an

      additional pair of quotes, perhaps chr(34) & "string" & chr(34)

      Regards,

      Peter T

      "bill_morgan" <billmorgan.excel.todaysummary.com.discussions.microsoft.com> wrote in message

      news:27A40074-EA0C-4CD8-A753-34698AF6CF7F.excel.todaysummary.com.microsoft.com...

      > Excel 2000 running in XP:

      > Having trouble getting consistent results from VBA Shell Function.

      > For example, this command appears to run okay, but doesn't print cat.txt,

      > which is located in root directory. Is there something wrong with syntax?

      > Thanks ...

      > ID = Shell("command.com /c print cat.txt")

      > bill morgan

      >

      #2; Fri, 23 May 2008 04:36:00 GMT
    • I think function is working - just can't see the results. Command line opens

      briefly in task bar and then closes, but no error is returned that I can see.

      Function is returning a non-zero value. Underscore you suggested below also

      works, but still cannot view results.

      "Peter T" wrote:

      > Would it work if you replaced the space in "print cat.txt" with say an

      > underscore. If so try embracing the string in the shell function with an

      > additional pair of quotes, perhaps chr(34) & "string" & chr(34)

      > Regards,

      > Peter T

      >

      > "bill_morgan" <billmorgan.excel.todaysummary.com.discussions.microsoft.com> wrote in message

      > news:27A40074-EA0C-4CD8-A753-34698AF6CF7F.excel.todaysummary.com.microsoft.com...

      > > Excel 2000 running in XP:

      > >

      > > Having trouble getting consistent results from VBA Shell Function.

      > >

      > > For example, this command appears to run okay, but doesn't print cat.txt,

      > > which is located in root directory. Is there something wrong with syntax?

      > > Thanks ...

      > >

      > > ID = Shell("command.com /c print cat.txt")

      > >

      > > bill morgan

      > >

      > >

      >

      >

      #3; Fri, 23 May 2008 04:37:00 GMT
    • On my way to that site. Thanks for the suggestion.

      "John.Greenan" wrote:

      > Can I suggest binning the use of shell? have a look at this link:

      > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsoriWshScriptExecObjectPropMeth.asp

      > It's the Windows Script Host. It's much more user friendly that running

      > shell (la la la).

      >

      > --

      > www.alignment-systems.com

      >

      > "bill_morgan" wrote:

      > > Excel 2000 running in XP:

      > >

      > > Having trouble getting consistent results from VBA Shell Function.

      > >

      > > For example, this command appears to run okay, but doesn't print cat.txt,

      > > which is located in root directory. Is there something wrong with syntax?

      > > Thanks ...

      > >

      > > ID = Shell("command.com /c print cat.txt")

      > >

      > > bill morgan

      > >

      > >

      #4; Fri, 23 May 2008 04:38:00 GMT
    • Here is what worked (simple oversight on my part):

      Sub printer2()

      m = Shell("command.com cd\")

      t = Shell("command.com /c print cat.txt")

      End Sub

      OR

      Sub print_stuff()

      FileSystem.ChDir "c:\"

      t = Shell("command.com /c print cat.txt")

      End Sub

      "Peter T" wrote:

      > Would it work if you replaced the space in "print cat.txt" with say an

      > underscore. If so try embracing the string in the shell function with an

      > additional pair of quotes, perhaps chr(34) & "string" & chr(34)

      > Regards,

      > Peter T

      >

      > "bill_morgan" <billmorgan.excel.todaysummary.com.discussions.microsoft.com> wrote in message

      > news:27A40074-EA0C-4CD8-A753-34698AF6CF7F.excel.todaysummary.com.microsoft.com...

      > > Excel 2000 running in XP:

      > >

      > > Having trouble getting consistent results from VBA Shell Function.

      > >

      > > For example, this command appears to run okay, but doesn't print cat.txt,

      > > which is located in root directory. Is there something wrong with syntax?

      > > Thanks ...

      > >

      > > ID = Shell("command.com /c print cat.txt")

      > >

      > > bill morgan

      > >

      > >

      >

      >

      #5; Fri, 23 May 2008 04:39:00 GMT
    • Thanks for the feedback

      Afraid my original suggestion was a never going to be useful. Somehow I read

      you wanted to print a file named "print cat.txt". Sorry if that lead you up

      the wrong path!

      But had that been the case, ie a space in the fullname, the extra quotes are

      necessary with some usages of Shell.

      Regards,

      Peter T

      "bill_morgan" <billmorgan.excel.todaysummary.com.discussions.microsoft.com> wrote in message

      news:EB7261C3-C65A-41C0-91C2-F4B963D6E150.excel.todaysummary.com.microsoft.com...

      > Here is what worked (simple oversight on my part):

      > Sub printer2()

      > m = Shell("command.com cd\")

      > t = Shell("command.com /c print cat.txt")

      > End Sub

      > OR

      > Sub print_stuff()

      > FileSystem.ChDir "c:\"

      > t = Shell("command.com /c print cat.txt")

      > End Sub

      >

      > "Peter T" wrote:

      > > Would it work if you replaced the space in "print cat.txt" with say an

      > > underscore. If so try embracing the string in the shell function with an

      > > additional pair of quotes, perhaps chr(34) & "string" & chr(34)

      > >

      > > Regards,

      > > Peter T

      > >

      > >

      > > "bill_morgan" <billmorgan.excel.todaysummary.com.discussions.microsoft.com> wrote in message

      > > news:27A40074-EA0C-4CD8-A753-34698AF6CF7F.excel.todaysummary.com.microsoft.com...

      > > > Excel 2000 running in XP:

      > > >

      > > > Having trouble getting consistent results from VBA Shell Function.

      > > >

      > > > For example, this command appears to run okay, but doesn't print

      cat.txt,

      > > > which is located in root directory. Is there something wrong with

      syntax?

      > > > Thanks ...

      > > >

      > > > ID = Shell("command.com /c print cat.txt")

      > > >

      > > > bill morgan

      > > >

      > > >

      > >

      > >

      > >

      #6; Fri, 23 May 2008 04:40:00 GMT