Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I capture cmd instructions? 1

Status
Not open for further replies.

falled

Programmer
Nov 3, 2006
47
ES
I want to execute "net use" command to configure the printers. How can I execute and return the results?
 
net use lpt1: \\server\printer > c:\output.txt

will produce a file called output.txt. I presume as this is the VBA forum, you will be executing this from within a Shell statement so that this can be read by your program.

John
 
Private Sub CommandButton1_Click()

Dim f As Integer, a As String, t As Single

On Error Resume Next
Kill CurDir$ & "\report.txt"
On Error GoTo 0

Shell Environ$("COMSPEC") & " /C " & "NET USE > " & Chr$(34) & CurDir$ & "\report.txt" & Chr$(34)

'delay for the file to be created
t = Timer
Do While Timer - t < 1
DoEvents
Loop

f = FreeFile
Open CurDir$ & "\report.txt" For Input As f
a = Input$(LOF(f), f)
Close f

MsgBox a
Kill CurDir$ & "\report.txt"

End Sub

HTH Hugh
 
Thank you both for your answers

HughLerwill there's something wrong in your lines,

VB don't accepted it

t = Timer
Do While Timer - t < 1
DoEvents()
Loop

f = FreeFile()
Open CurDir$ & "\report.txt" For Input As f
a = Input$(LOF(f), f)
Close(f)

may I missing something???
 
My code was tested in Excel VBA. Did you copy/ paste it from the web page; if not check for it for typos.eg. You are using Close(f) and FreeFile() ; I did not include the brackets in my code.

What problem/ error message are you getting.

Hugh
 
You must understand I just a newbie... Sorry and thank you

t = Timer 'Timer not a type
may I declare it as new timer?
-------------------------------------------------------
doEvents() 'I don't know if exists any command to wait for a file to be created meanwhile I use

System.Threading.Thread.Sleep(5000)
-----------------------------------------------------------

Open CurDir$ & "\report.txt" For Input As f
a = Input$(LOF(f), f)
Close(f)

all of this fails too, but I think I don't use it correctly

What does it mean?

Kill CurDir$ & "\report.txt"

and

Environ$("COMSPEC") & " /C
 
What if any MS Office Application are you using to write your VBA code?

If your line System.Threading.Thread.Sleep(5000) is working it leads me to think you may be using VB.NET. If you are using VB.NET you should be in forum796.

Hugh


 
You are right, sorry

How can I move this trhead?
 
I don't think you can. Just go to forum796 and re-post there. Sorry not to be of more help.

regards Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top