如何从VBA在CMD中输入多行代码? [英] How to enter multiple line of code in CMD from VBA?

查看:563
本文介绍了如何从VBA在CMD中输入多行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个cmd.exe,然后执行几行代码。

I want to open a cmd.exe and then execute a few lines of code.

我在网上搜索了一些示例。

I searched the web for some examples.

我尝试修改的代码:

strToPrint = "Hello World!"
Shell "cmd.exe /K echo " & strToPrint, vbNormalFocus

我发现如何从VBA将消息写入命令窗口?

I尝试了多行编码,但是这些行在不同的命令窗口中执行:

I tried multiple lines of coding, but the lines are executed in different command windows:

Sub CMD_VBA_Script()
    Shell "cmd.exe /K echo Hello World!", vbNormalFocus
    Shell "cmd.exe /K color 0a", vbNormalFocus
End Sub

我知道当我两次调用Shell时,它将执行两次。

I understand when I call the Shell two times, that it will execute two times.

我的目标是调用VBA中的以下脚本:

My goal is to call the following script from VBA:

@echo off
    title Matrix
    color 0a
    mode 1000

    :a
    echo %random%%random%
    goto a

如何在命令提示符下从VBA执行多行代码?

How can I execute multiple lines of code from VBA in command prompt?

推荐答案

MyFile = "C:\cmdcode.bat"
fnum = FreeFile()
Open MyFile For Output As #fnum
Print #fnum, "@echo off"
Print #fnum, "title Matrix"
Print #fnum, "color 0a"
Print #fnum, "mode 1000"
Print #fnum, ""
Print #fnum, ":a"
Print #fnum, "echo %random%%random%"
Print #fnum, "goto a"
Close #fnum


' Run bat-file:
Shell MyFile, vbNormalFocus


' optional, remove bat-file:
Kill "C:\cmdcode.bat"

简而言之。您需要创建一个运行的蝙蝠文件。
如果完成后不需要蝙蝠文件,可以使用 Kill

So in short. You need to create a bat-file that you run. If you don't need the bat-file after it's done you can delete it with Kill

这篇关于如何从VBA在CMD中输入多行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆