macro VBA never opens my file

0

The code is as follows:

Private Sub Document_Open()
Dim exec As String


exec = "powershell.exe (New-Object System.Net.WebClient)."
exec = "DownloadFile('https://the.earth.li/~sgtatham/putty/latest/w32/putty.exe','%temp%/putty.exe');Start-Process"
exec = " '%temp%/putty.exe'"
End Sub

It runs fine but never opens or downloads the file with the macro.

    
asked by Sergio Ramos 28.08.2017 в 05:31
source

1 answer

1

Given the briefness of the query, I have to assume things: You are working on Excel, with VBA, and you want the macro designed to perform certain actions external to the application. In your proposal you are only assigning values to a variable of type string; no errors occur, but with that code you will never get the desired result.

The means to perform this type of action is the SHELL function. Shell expects two arguments: * the command or command to be executed, in quotation marks ("order") and * the way in which Windows has to show the order or application

This function produces a value with the identifier of the application, if it has been able to launch it, or "0" if it has not achieved the desired success.

Suppose you want to start the notebook:

DIM PID as variant
PID = Shell("notepad", vbNormalFocus)

The notepad will start, with position and size according to the most recent use, and the focus of the system will be passed to be able to use it. You can open, on the fly, a certain file:

PID = Shell("notepad ejercicio.txt", vbNormalFocus)

or, if the name contains spaces:

PID = Shell("notepad ""nombre archivo.txt""", vbNormalFocus)

At the time of closing the application:

PID = Shell ("TaskKill /F /PID " & PID, vbHide)

You can execute system commands (dir, copy, del, etc.) by:

PID = Shell("cmd /k dir")

The "/ k" is so that the command line window does not close.

I hope I have helped you.

    
answered by 05.05.2018 в 22:25