简体   繁体   English

如何从VBA中的shell命令读取所有信息并将其保存在.txt文件中

[英]How to read all information from shell command in VBA and save it in .txt file

I try to use this code: 我尝试使用以下代码:

Shell(nppPath & " " & fileToOpen, vbNormalFocus)

which opens txt file nppPath = "C:\\Windows\\notepad.exe" but I cannot read from it and save it as .txt file using VBA. 这将打开txt文件nppPath = "C:\\Windows\\notepad.exe"但我无法读取它,无法使用VBA将其另存为.txt文件。

Looking forward to any suggestions. 期待任何建议。

I'm presuming you copied the nppPath over wrong as it should be 我假设您复制了nppPath错误,因为它应该是

nppPath = "C:\Windows\System32\notepad.exe"

but for reading from the text file you could use free file: 但是要从文本文件中读取,您可以使用免费文件:

Dim IntFile As Integer, StrFileName As String, StrFileContent As String

StrFileName = fileToOpen 'Your file location here
IntFile = FreeFile
Open StrFileName For Input As #IntFile
    StrFileContent = Input(LOF(IntFile), IntFile) 'This now contains the text
Close #IntFile

This will read the text file into a single variable that you can then use however you wish. 这会将文本文件读入单个变量,然后您可以根据需要使用它。 If you want to write to a text file you can use the following code: 如果要写入文本文件,可以使用以下代码:

Dim StrNewLocation As String, StrFileContent As String

StrNewLocation = "" 'Put the new files name and location here
StrFileContent = "Example" 'The text to go into the new file

Open StrNewLocation For Output As #1
    Write #1, StrFileContent
Close #1

Hopefully this helps you solve your problem if not send me a message and I'll see what I can do. 如果没有给我发消息,希望这可以帮助您解决问题,我会尽力而为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM