Conversion jpg en pdf via VBA

D'accord je vois, si vous comptez partir sur Microsoft Print to PDF comme une option d'imprimante. Vous pourrez partir sur ça :

Sub PrintToPDF()

    Dim strFilePath As String
    strFilePath = "C:\path\to\your\image.jpg"

    ' Change the active printer to Microsoft Print to PDF
    Application.ActivePrinter = "Microsoft Print to PDF"

    ' Open the picture file with the default associated application
    RetVal = ShellExecute(0, "print", strFilePath, 0, 0, 0)

End Sub

Cela utilisera l'application par défaut associée aux fichiers JPG pour imprimer l'image à l'aide de Microsoft Print to PDF.

Ok merci je vais tester

Et sans vouloir abuser comment configurer le répertoire de destination?

Bonjour,

Le choix de l'emplacement de sauvegarde du fichier PDF généré par l'imprimante "Microsoft Print to PDF" n'est pas directement contrôlable par le code VBA.

Mais il est possible d'automatiser cette interaction en utilisant une bibliothèque d'automatisation Windows comme SendKeys. Un exemple de comment vous pourrez le faire :

Sub PrintToPDF()

    Dim strFilePath As String, strPDFPath As String
    strFilePath = "C:\path\to\your\image.jpg"
    strPDFPath = "C:\path\to\your\output.pdf"

    ' Change the active printer to Microsoft Print to PDF
    Application.ActivePrinter = "Microsoft Print to PDF"

    ' Open the picture file with the default associated application
    RetVal = ShellExecute(0, "print", strFilePath, 0, 0, 0)

    ' Wait a bit for the dialog to appear
    Application.Wait (Now + TimeValue("0:00:02"))

    ' Use SendKeys to automate the save dialog
    SendKeys strPDFPath & "{ENTER}", True

End Sub

Bonjour et merci pour ce retour

je viens de tester et cela semble planter sur le shellexecute

image

Ok donc ça signifie que la fonction ShellExecute n'est pas reconnue. C'est une fonction d'API Windows que nous devons déclarer explicitement.
Vous devrez placer ces lignes de code en haut de votre module VBA :

#If VBA7 Then
    Public 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
#Else
    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
#End If

Testez et dites moi si ça fonctionne cette fois-ci.

je vous remercie pour votre retour

j'ai bien intégré votre code en amont dans mon module

désormais j'ai cette erreur

image

cela apparait a cette ligne

    ' Change the active printer to Microsoft Print to PDF
    Application.ActivePrinter = "Microsoft Print to PDF"

j'ai tente de voir a quoi cela pouvait etre lié et il semble au port aussi j'ai effectué certain checks

si je tente une conversion directe via l'imprimante cela affiche ceci:

sans titre

aussi j'ai tenté = "Microsoft Print to PDF sur PORTPROMPT" ou encore = "Microsoft Print to PDF sur PORTPROMPT:"

mais cela ne change pas...

L'erreur que vous obtenez est probablement due à une mauvaise référence au nom de l'imprimante ou à un problème avec le pilote d'impression PDF de Microsoft.

Le nom de l'imprimante doit correspondre exactement à ce qui est indiqué dans la liste des imprimantes de votre système. En particulier, la chaîne
"Microsoft Print to PDF"
est le nom généralement utilisé, mais cela peut varier selon les paramètres régionaux et la langue de votre système. Hésitez pas à vérifier le nom exact. Si vous continuez à rencontrer des erreurs, il est peut être utile d'essayer un autre pilote d'impression PDF, comme Adobe PDF ou PDFCreator…
image image image

J'ai trouvé une macro qui permet de lister les imprimantes et les ports associés je retrouve donc Imrpimante : Microsoft Print to PDF Port : PORTPROMPT: Imrpimante : Adobe PDF Port : Documents\*.pdf pourtant quand je teste dans le code j'ai toujours l'erreur 1004 qui apparait

Sub PrintToPDF(strFilePath, strPDFPath)

    ' Change the active printer to Microsoft Print to PDFOn Error Resume Next
'sPDFwriter = "Microsoft Print to PDF sur PORTPROMPT"
sPDFwriter = "Adobe PDF sur Documents\*.pdf"
' save current printer name, then change to PDF writer
    sCurrentPrinter = Application.ActivePrinter
    Application.ActivePrinter = sPDFwriter

    ' Open the picture file with the default associated application
    RetVal = ShellExecute(0, "print", strFilePath, 0, 0, 0)

    ' Wait a bit for the dialog to appear
    Application.Wait (Now + TimeValue("0:00:02"))

    ' Use SendKeys to automate the save dialog
    SendKeys strPDFPath & "{ENTER}", True
  Application.ActivePrinter = sCurrentPrinter

End Sub

j'ai ténté en faisant quelque recherche autre chose mais alors ca lance l'impression sur mon imprimante de boulot(sans aller jusqu'au bout) et pas la pdf

Sub PrintToPDF(strFilePath, strPDFPath)
   ' sCurrentPrinter = Application.ActivePrinter
    ' Change the active printer to Microsoft Print to PDFOn Error Resume Next
    For b = 0 To 9
       Err.Clear
       sPDFwriter = "Microsoft Print to PDF" & " sur PORTPROMPT:" & b & ":"
        On Error Resume Next
        Application.ActivePrinter = sPDFwriter
        If ActivePrinter = sPDFwriter Then Exit For
    Next

'sPDFwriter = "Adobe PDF sur Documents\*.pdf"
' save current printer name, then change to PDF writer

    'Application.ActivePrinter = sPDFwriter

    ' Open the picture file with the default associated application
    RetVal = ShellExecute(0, "print", strFilePath, 0, 0, 0)

    ' Wait a bit for the dialog to appear
    Application.Wait (Now + TimeValue("0:00:02"))

    ' Use SendKeys to automate the save dialog
    SendKeys strPDFPath & "{ENTER}", True
 ' Application.ActivePrinter = sCurrentPrinter

End Sub

je progresse dans mes recherches via une macro trouvé sur le web

Function FindPrinter(ByVal PrinterName As String) As String
  Dim arrH, Pr, Printers, Printer As String
  Dim RegObj As Object, RegValue As String
  Const HKEY_CURRENT_USER = &H80000001

    Set RegObj = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    RegObj.Enumvalues HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Printers, arrH

      For Each Pr In Printers
        RegObj.getstringvalue HKEY_CURRENT_USER, "Software\Microsoft\Windows NT\CurrentVersion\Devices", Pr, RegValue
        Printer = Pr & " on " & Split(RegValue, ",")(1)
        If InStr(1, Printer, PrinterName, vbTextCompare) > 0 Then
           FindPrinter = Printer
           MsgBox (Printer)
           Exit Function
        End If
      Next
End Function

Sub test()
FindPrinter ("Microsoft Print to PDF")

End Sub

ca me sort

image

du coup j'adapte ma macro:

sPDFwriter = "Microsoft Print to PDF on Ne01"
    Application.ActivePrinter = sPDFwriter

mais toujours erreur 1004

Re bonjour

j'en suis arrivé au code ci-dessous mais qui lance quelque chose mais via mon imprimante princiiapel et non la pdf

Sub PrintToPDF(strFilePath, strPDFPath)

For aa = 0 To 9
Nom = "Microsoft Print to PDF sur Ne0" & aa & ":"
On Error Resume Next
Application.ActivePrinter = Nom
If ActivePrinter = Nom Then Exit For
Next

Application.ActivePrinter = ActivePrinter

' Open the picture file with the default associated application
RetVal = ShellExecute(0, "print", strFilePath, 0, 0, 0)

' Wait a bit for the dialog to appear
Application.Wait (Now + TimeValue("0:00:02"))

' Use SendKeys to automate the save dialog
SendKeys strPDFPath & "{ENTER}", True
' Application.ActivePrinter = sCurrentPrinter

End Sub

si je tente de modifier la lisge schellexecute

et met RetVal = ShellExecute(0, "printto", strFilePath, 0, 0, 0)

la je peux selectionner l'imprimante et convertir

mais la covnersion n'est pas top c'est à dire que ca met sur fond A4.

autrement dit impossible d'avoir un rendu via vba il semblerait

Rechercher des sujets similaires à "conversion jpg pdf via vba"