printing specific pages form pdf file using print scope string - excel

I need to print selected pages of *.pdf file using excel VBA.
I need to do this not by providing scope "from - to" but giving scoope of pages like "1-3,4,8, 17-25"
I can only print whole file using below code:
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
Public Function PrintThisDoc(formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(formname, "Print", FileName, 0&, 0&, 3)
End Function
Sub testPrint()
Dim printThis
Dim strDir As String
Dim strFile As String
strDir = "C:\Users\any\Desktop"
strFile = "somefile.pdf"
printThis = PrintThisDoc(0, strDir & "\" & strFile)
End Sub ```

Judging from this post, it is not possible using ShellExecute alone. What you would need is to write a script which will open the file in whatever PDF reader you have installed, find a "Print" button there and provide all the options. This will take a huge amount of work and will depend on the version of Windows and the PDF reader, so basically it will work only for you, and not for other users.
If you want to go this path though, take a look at FindWindow and SendMessage WinAPI functions.

Related

Open cmd.exe in excel 365 vba

I want to open cmd.exe in excel 365 vba.
I used to open shell in excel 2013 but it seems not to work in excel 365.
I tried this code and works for calc.exe, ... but not for cmd.exe.
Does it need a special parameter/permission to run from vba.
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr
' Edit this:
strFile = "notepad.exe" ' the file you want to open/etc.
strAction = "OPEN" ' action might be OPEN, NEW or other, depending on what you need to do
lngErr = ShellExecute(0, strAction, strFile, "", "", 0)
' optionally, add code to test lngErr
End Sub
Kind regards,
w.
use SW_SHOW = 5 as last parameter
Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
Sub ShellExec()
Dim strFile As String
Dim strAction As String
Dim lngErr As LongPtr
' Edit this:
strFile = "cmd.exe" ' the file you want to open/etc.
strAction = "OPEN" ' action might be OPEN, NEW or other, depending on what you need to do
lngErr = ShellExecute(0, strAction, strFile, "", "", 5)
' optionally, add code to test lngErr
End Sub

Download Excel file from Sharepoint, Save to Desktop

I am trying to download an excel file stored in sharepoint (no unc path available) to my desktop.
This code below seems to work and create "CST.xlsx" but I get an error msg:
Notes:
Url for excel file is taken directly from the web address bar and everthing after ".xlsx" is removed.
Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, _
ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Function DownloadFileFromWeb(strURL As String, strSavePath As String) As Long
' strSavePath includes filename
DownloadFileFromWeb = URLDownloadToFile(0, strURL, strSavePath, 0, 0)
End Function
Sub download()
Call DownloadFileFromWeb("url.xlsx", "Desktop\download\CST.xlsx")
End Sub
See my answer in this thread about the same problem for a workaround.

Download fail using VBA. Any suggestion?

Hi I'm trying to download a zip in a local server using VBA. My code works great in my PC but at the server doesn't work. Heres's the code:
PS. Cell A7 is the link of the download.
Dim downloadStatus As Variant
Dim url As String
Dim destinationFile_local As String
url = [A7]
destinationFile_local = "C:\Users\omayorga\Downloads\" & fileName([A7])
downloadStatus = URLDownloadToFile(0, url, destinationFile_local, 0, 0)
If downloadStatus = 0 Then
MsgBox "Downloaded Succcessfully!"
Else
MsgBox "Download failed"
End If
End Sub
Function fileName(file_fullname) As String
fileName = Mid(file_fullname, InStrRev(file_fullname, "/") + 1)
End Function
Any suggestion? Thanks so much
You have this in your code, you need:
Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
(ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Check your download link, the same said Raymond Wu your destinationFile_local, tested here your code works.
refer link
How do I download a file using VBA (without Internet Explorer)

Excel VBA code to download images in MacOS runs without error but doesnt download any image

I need to download images using excel vba in macOS the part i guess is having error is that this function doesnt download image. I have never made vba code for MacOS so can anybody tell me do i have to select any library for this code to work or i have used wrong library.
Option Explicit
Private Declare PtrSafe Function popen64 Lib "/usr/lib/libc.dylib" Alias "popen" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As LongPtr, ByVal lpfnCB As LongPtr) As Long
Public Function DownloadURLtoFile(sSourceURL As String, _
sLocalFileName As String) As Boolean
Debug.Print sSourceURL
Debug.Print sLocalFileName
DownloadURLtoFile = popen64(0&, _
sSourceURL, sLocalFileName, &H10, 0&) = 0&
End Function
Thanks in advance....
I am using this way to call this function and checked filepath is correct.
FolderString = FolderString & FirstName & "_" & LastName & ".jpg"
Debug.Print FolderString
If DownloadURLtoFile(cell.Value, FolderString) Then
cell.Offset(, 3).Value = "Successfully downloaded"
Else
cell.Offset(, 3).Value = "Error - no download"
End If

VBA - Username of open workbook (read only)

If an open workbook (located on a server) is in read only mode, how can I display the active username using VBA?
I've looked into .WriteReservedBy but this only shows the name of the person that last saved the file with a password.
This should probably be a comment but my reputation is too low
I've seen this but never needed the info...
Things to try:
ThisWorkbook.UserStatus - array with all current users for the file open as exclusive or shared
Environ("USERNAME")
CreateObject("WScript.NetWork").UserName
API calls:
.
Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA"
( _
ByVal lpName As String, _
ByVal lpUserName As String, _
lpnLength As Long
) As Long
Declare Function GetUserName& Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long)
.
more details about these APIs:
https://support.microsoft.com/en-us/kb/161394
http://www.vbaexpress.com/kb/getarticle.php?kb_id=768
WMI Win32_NetworkConnection:
Public Function GetActiveUser(Optional ByVal computer As String = ".") As String
Dim wmi As Object, itm As String
On Error Resume Next
Set wmi = GetObject("winmgmts:\\" & computer & "\Root\CIMv2")
itm = wmi.ExecQuery("Select UserName from Win32_NetworkConnection", , 48)
GetNetActiveUser = itm
End Function

Resources