I'm trying to open up and print a number of PDF files in Excel. For various reasons, I need to open the file and use the "Microsoft Print to PDF" printer so that I can do some manipulation later. My idea was to use a shell command to invoke Adobe Acrobat and open the file, print the file to a new .pdf, and then close the file. The problem is that I can't figure out how to rename my file.
So far, I'm using this code:
Dim command As String
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 7
'Grabbed Application.ActivePrinter earlier and used this below
command = "acrobat /t " & tempName "Microsoft Print to PDF on NE03:" "Microsoft Print to PDF on NE03:"
wsh.Run command, windowStyle, waitOnReturn
The file opens and closes as expected in Adobe, but it doesn't seem to print anything (and I can't figure out a way to give it a name). Is there a way to feed it a new file name? Thanks everyone for your suggestions.
Related
I am using a macro that reads every excel file in one folder and subfolders and refreshes it by opening the file and closing it with 'save changes' attribute as True. The problem is that VBA doesn't read non-english letters correctly and it causes error when trying to save the spreadsheet. My region settings in Windows control panel are correct. When I try to use the beta option of using Unicode UTF-8 for every language it works but that causes a lot of other programs I use to display some weird characters. The language I try to incorporate is polish. Any idea what to do?
Sub RefreshExcelDocs()
Const startFolder As String = "C:\Users\Patryk\Downloads\Przykład óżęą\Folder\"
Dim file As Variant, wb As Excel.Workbook
For Each file In Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & startFolder & "*.xl*"" /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
Set wb = Workbooks.Open(file)
wb.Close SaveChanges:=True '
Set wb = Nothing
Next
End Sub
I have to run an .exe in VBA Excel and write in the input window "in.txt" "out.txt" in order to make the process automatic inside a macro. I tried to use shell but it works asynchrounous and I also don't know how to tell her to write inside the .exe.
I've also tried with SendKeys but apperently it doesen't work.
How could I make the VBA calling my .exe, open it, write inside the command window of the .exe, wait for the output and use it to go on?
thank you in advance
here are two attempts (both failed):
Sub write()
prog = Shell("C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe", 1)
Application.Run "'AXTUR&EXCEL.xlsm'!inserisci_dati_input"
SendKeys.send "in.txt~", True
SendKeys.send "out.txt~", True
SendKeys "%{F4}", True
End Sub
Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strProgramName = "C:\Users\arancia\Pictures\Camera Roll\axtur\axtur\AXTUR_64.exe"
strArgument = "in.txt~out.txt~"
Call Shell("""" & strProgramName & """ """ & strArgument & """", vbNormalFocus)
End Sub
One solution would be to write a batch file that includes all the parameters and then run the batch.
I have used WshShell (Windows scripting host) to run batch files to do what you want in the past but WshShell does not work on our computers since the Nov 2020 updates. WshShell allows you to wait for the outcome of the external program.
One way I found to go around it is to write a simple text file at the end of the batch and wait for it to show up. This is crude but it works.
In the code below, I write a simple batch file in the folder of the Excel sheet. The last line of the batch writes the content of the folder in a text file. The Do Until loop waits for the text file to show up in 1 second increments. When the code resumes after the loop, the text file is deleted. If you write the command line you would type in cmd instead of "echo Hello World" this should work.
You need to reference the Microsoft Scripting Runtime (scrrun) to use the file system object.
Good Luck!
Public Sub RunBatch()
Dim i As Integer
Dim k As Integer
Dim xlWB As Workbook
Dim fso1 As New FileSystemObject
Dim BatFile As Object
Dim IsDone As Boolean
Dim OutFileName As String
Set xlWB = ThisWorkbook
OutFileName = xlWB.Path & "\" & "HW.bat"
Set BatFile = fso1.CreateTextFile(OutFileName)
BatFile.WriteLine "cd /d " & xlWB.Path
BatFile.WriteLine "echo Hello World"
BatFile.WriteLine "dir > Done.txt"
BatFile.Close
IsDone = False
Call Shell(OutFileName, vbNormalFocus)
Do Until IsDone
If fso1.FileExists(xlWB.Path & "\Done.txt") Then IsDone = True
Application.Wait (Now + TimeValue("00:00:01"))
Loop
fso1.DeleteFile (OutFileName)
End Sub
I need to save the txt file, which is embedded as an object in the Excel workbook.
I had a look at Saving embedded OLE Object (Excel workbook) to file in Excel 2010.
In the workbook, I have an embedded text file and pdf files (for test purposes, but ultimately I need to use txt file). When I point the OLEObject to xlsm and update the fName accordingly, the below command works correctly.
But when I point the OLEObject to txt file and change the extension in fName to txt, it fails with
Run-time error 1004.
What is the command for txt files? I tried SaveAs2 as per the other threads.
Dim uName As String
Dim fName As String
Dim oEmbFile As Object
uName = Left(Environ("AppData"), Len(Environ("AppData")) - 16)
fName = uName & "\Desktop\OTPReport_Vin" & ".xlsm"
Set oEmbFile = wbkCurrent.Worksheets("Compare the changes").OLEObjects("Object 1")
oEmbFile.Object.SaveAs fName
You need to specify the format, not just change the extension.
Here's the list of different ones (though I don't think it is fully complete):
https://learn.microsoft.com/en-us/office/vba/api/excel.xlfileformat
You would want xlTextWindows so your code would be:
oEmbFile.Object.SaveAs fName, xlTextWindows
I tried to run Command in CMD from excel VBA to combine file to the only single file but its not wrok. And that command work properly when I run directly in folder
Excel VBA 2013
Sub CombineFiles()
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run "D:\Excel\FT\cmd.bat"
End Sub
MY Code Inside CMD
copy *.txt combined.txt
I expect to get the combination of all text file into single file named Combined.txt
Within your .bat file, you need to change the directory to point to the folder location of the .txt files:
d:
cd\Excel\FT
copy *.txt combined.txt
PAUSE
or
copy D:\Excel\FT\*.txt D:\Excel\FT\combined.txt
PAUSE
I have added a pause so you can see the result of the running bat file, if you wish you can remove the pause command.
I am having an issue finding/understanding how to solve the following issue when running a .bat file in VBA in Excel. I am getting an Open File - Security Warning prompt when I try and execute it.
The issue is i don't have administrative privileges, so I can't set .bat files as being ignored from this prompt, and this .bat file is one that I created myself so I don't understand why my computer is flagging it as potentially harmful.
If anyone could explain a workaround, and why such an issue arises despite it being a custom generated .bat file, I would greatly appreciate it. By a workaround I mean modifying the code I currently have to deal with this problem, without having to go through Administrators.
Below is the snippet of code pertaining to the problem, thanks:
sub test()
'some stuff
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, FileContents
Print #FileNumber, "exit"
Close #FileNumber
'run batch file
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim rtn As Integer
rtn = wsh.Run(Chr(34) & MY_FILENAME & Chr(34), windowStyle, waitOnReturn)
end sub
I should add that if run the shell like so I have no issues:
rtn = Shell(MY_FILENAME, vbNormalFocus)
Except that the code will continue running despite the shell not closing and completing its execution. I need the shell to halt until it has executed it's task.