Open an external EXE. file using VBA Powerpoint - vba

Please help me make this code works. I want to open an external EXE file using Powerpoint VBA.
Sub open_test()
Dim FileName, FSO, MyFile
FileName = "C:\test.exe"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set MyFile = FSO.OpenTextFile(FileName, 1)
End Sub
There are no compiler errors, but when i execute the macro, it does nothing ... I have another alternatives to this code? please help me, thanks!

If you just want to start the program, try using
Sub open_test()
Dim sFullPathToExecutable as String
sFullPathToExecutable = "C:\test.exe"
Shell sFullPathToExecutable
End Sub

Related

Delete all files in a folder

I have the below code to try search for all files in my downloads folder and then delete them all however it's returning an error message based on the kill function not having enough arguments, any ideas?
Sub Kill ()
Dim aFile As String
aFile = "C:\Test\Test\Downloads\*.*"
If Len(Dir$(aFile)) > 0 Then
Kill aFile
End If
End Sub
Thanks,
A more simple way:
Sub Del()
Kill "C:\FolderName\*.*"
End Sub
Add a reference to Microsoft Scripting Runtime in the VBA environment
The declare in a Module the following line
Global fso As New FileSystemObject
Now you can use all the nice and modern I/O functions. For example:
Public Sub TDELFOL()
Dim path As String, f As File
path = fso.GetSpecialFolder(TemporaryFolder)
path = fso.BuildPath(path, "MyTempFolder")
If fso.FolderExists(path) Then
For Each f In fso.GetFolder(path).Files
f.Delete Force = True
Next
fso.DeleteFolder path, Force = True
End If
End Sub
You should not name macros as the in built functions. Just changing the macros with the same coding resolves the issues...
Sub Kill1 ()
Dim aFile As String
aFile = "C:\Test\Test\Downloads\*.*"
If Len(Dir$(aFile)) > 0 Then
Kill aFile
End If
End Sub

Display Merged PDF Files

is there a way to display/publish the merged pdf file after running the excel vba macro to merge them? OpenAfterPublish:= True doesn't seem to work?
You can open files in excel-vba like
Sub test()
Dim myShell As Object
Set myShell = CreateObject("WScript.Shell")
myShell.Run "C:\test.pdf" //replace with your path
End Sub

How to use the FileSystemObject in VBA

So I'm quite new to VB and I'm just trying to create something that will open up a .txt file then read the first line and output it.
I've put my code below but when I run it I get the error
Object variable or with block variable not set
because of the line
objTXT=objFSO.OpenTextFile("C:\...",ForReading)
Any help, I feel like I'm missing something quite basic.
Private Sub Text_Reader()
Dim objFSO As FileSystemObject
Dim objTXT As TextStream
Dim str$
Set objFSO = New FileSystemObject
objTXT = objFSO.OpenTextFile("C:\...", ForReading)
str = objTXT.ReadLine
MsgBox (str)
End Sub
The problem is not use Set for opening. Try as follow:
Set objTXT = objFSO.OpenTextFile("C:\...", ForReading)
You don't need FileSystemObject to read textfile.
You can do it like that (without any external libraries):
Public Sub readTextFile(filepath As String)
Dim intFile As Integer
Dim text As String
'------------------------------------------------------------------------------------------------------
intFile = VBA.FreeFile()
Open filepath For Input As #intFile
Line Input #intFile, text
Close intFile
Call MsgBox(text)
End Sub

Why doesn't this shell code work?

I'm working in VBA. Right now, I'm in UserForm3. There is a text box that displays a user-defined path.
What I need to do next is actually get the file to open. I was trying to use a shell but it isn't working. Anyone know why?
Private Sub Open_Button_Click()
Dim myPath As String
myPath = FileName.Text 'Gets the string, FileName, from module 1
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open myPath
End Sub
The alternative version, and the one I'd prefer to use, is this:
Private Sub Open_Button_Click()
Dim shell As Object
Set shell = CreateObject("Shell.Application")
shell.Open FileName
End Sub
Maybe I'm just tired, but I'm not seeing why it isn't working. I've been toying with it for awhile.
I'm using Autodesk Inventor 2011...running it through VBA Editor
Thanks ahead of time,
Alyssa
JPEG:
http://i.stack.imgur.com/YkHfF.jpg
EDIT 1, What Has Been Tried So Far:
(from help in comments)
-setting it to modeless from modal (nothing happens)
-double-clicking the file to make sure it opens (it does)
-putting file in separate module and running (nothing happens)
Got it!
Private Sub Open_Button_Click()
Dim myPath As String
myPath = FileName.Text 'Gets the string, FileName, from module 1
Dim Shell As Object
Set Shell = CreateObject("Shell.Application")
Shell.Open (myPath)
End Sub
I just enclosed "myPath" in the Shell.Open function.

How to create and write to a txt file using VBA

I have a file which is manually added or modified based on the inputs. Since most of the contents are repetitive in that file, only the hex values are changing, I want to make it a tool generated file.
I want to write the c codes which are going to be printed in that .txt file.
What is the command to create a .txt file using VBA, and how do I write to it
Use FSO to create the file and write to it.
Dim fso as Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile as Object
Set oFile = FSO.CreateTextFile(strPath)
oFile.WriteLine "test"
oFile.Close
Set fso = Nothing
Set oFile = Nothing
See the documentation here:
http://technet.microsoft.com/en-us/library/ee198742.aspx
http://technet.microsoft.com/en-us/library/ee198716.aspx
Open ThisWorkbook.Path & "\template.txt" For Output As #1
Print #1, strContent
Close #1
More Information:
Microsoft Docs : Open statement
Microsoft Docs : Print # statement
Microsoft Docs : Close statement
wellsr.com : VBA write to text file with Print Statement
Office Support : Workbook.Path property
To elaborate on Ben's answer:
If you add a reference to Microsoft Scripting Runtime and correctly type the variable fso you can take advantage of autocompletion (Intellisense) and discover the other great features of FileSystemObject.
Here is a complete example module:
Option Explicit
' Go to Tools -> References... and check "Microsoft Scripting Runtime" to be able to use
' the FileSystemObject which has many useful features for handling files and folders
Public Sub SaveTextToFile()
Dim filePath As String
filePath = "C:\temp\MyTestFile.txt"
' The advantage of correctly typing fso as FileSystemObject is to make autocompletion
' (Intellisense) work, which helps you avoid typos and lets you discover other useful
' methods of the FileSystemObject
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim fileStream As TextStream
' Here the actual file is created and opened for write access
Set fileStream = fso.CreateTextFile(filePath)
' Write something to the file
fileStream.WriteLine "something"
' Close it, so it is not locked anymore
fileStream.Close
' Here is another great method of the FileSystemObject that checks if a file exists
If fso.FileExists(filePath) Then
MsgBox "Yay! The file was created! :D"
End If
' Explicitly setting objects to Nothing should not be necessary in most cases, but if
' you're writing macros for Microsoft Access, you may want to uncomment the following
' two lines (see https://stackoverflow.com/a/517202/2822719 for details):
'Set fileStream = Nothing
'Set fso = Nothing
End Sub
an easy way with out much redundancy.
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\your_path\vba.txt", True, True)
Fileout.Write "your string goes here"
Fileout.Close
Dim SaveVar As Object
Sub Main()
Console.WriteLine("Enter Text")
Console.WriteLine("")
SaveVar = Console.ReadLine
My.Computer.FileSystem.WriteAllText("N:\A-Level Computing\2017!\PPE\SaveFile\SaveData.txt", "Text: " & SaveVar & ", ", True)
Console.WriteLine("")
Console.WriteLine("File Saved")
Console.WriteLine("")
Console.WriteLine(My.Computer.FileSystem.ReadAllText("N:\A-Level Computing\2017!\PPE\SaveFile\SaveData.txt"))
Console.ReadLine()
End Sub()