2017-10-04 4 views
0

J'essaie d'enregistrer du HTML en format PDF en utilisant VBA.Imprimer Wesite au format PDF en utilisant VBA

Je ne sais pas comment vérifier quelle est l'imprimante par défaut, changez-le en Microsoft Print en PDF, puis revenez à l'ancienne imprimante.

Voici mon code: Je recherche quelque chose sur Google, puis saisis les liens sur la première page de recherche Google, puis je souhaite imprimer les recherches au format PDF.

Sub Main() 

    Dim search As String 
    Dim save_path As String 
    Dim number As Integer 

    number = 5 
    save_path = "" 

    Dim query As String 
    query = InputBox("Enter here your search", "Google Search") 
    search = query 
    search = Replace(search, " ", "+") 
    PDFFolderSelection save_path, search 

    Debug.Print save_path, search 
    Google_Search search, save_path, number 
End Sub 

Sub Google_Search(search As String, save_path As String, number As Integer)  
    Dim IE As InternetExplorerMedium 
    Set IE = CreateObject("InternetExplorer.Application") 
    Dim HTMLDoc As MSHTML.HTMLDocument 

    IE.Navigate "http://google.com/#q=" & search 
    IE.Visible = True 

    Do While IE.ReadyState <> READYSTATE_COMPLETE 
    Loop 

    Set HTMLDoc = IE.Document 


    Dim RCS As MSHTML.IHTMLElementCollection 
    Dim RC As MSHTML.IHTMLElement 
    Set RCS = HTMLDoc.getElementsByClassName("rc") 
    Dim Atags As MSHTML.IHTMLElementCollection 
    Dim Atag As MSHTML.IHTMLElement 

    Dim URLs As New Collection 
    Dim URL As MSHTML.IHTMLElement 
    Set URLs = Nothing 


    For Each RC In RCS 
    Dim RS As MSHTML.IHTMLElementCollection 
    Dim R As MSHTML.IHTMLElement 
    Set RS = RC.getElementsByClassName("r") 
     For Each R In RS 
     Set Atags = R.getElementsByTagName("a") 
      For Each Atag In Atags 
      URLs.Add Atag 
      Next Atag 
     Next R 
    Next RC 

    For Each URL In URLs 
     Dim IEs As InternetExplorerMedium 
     Set IEs = CreateObject("InternetExplorer.Application") 
     str_text = Replace(URL.getAttribute("href"), "", "") 
     str_text = Replace(str_text, "", "") 
     'Debug.Print str_text 
     IEs.Navigate str_text 
     IEs.Visible = True 
     Do While IEs.ReadyState <> READYSTATE_COMPLETE Or IEs.Busy 
     Loop 

     IEs.Quit 
     Set IEs = Nothing 
    Next URL 

    IE.Quit 
    Set IE = Nothing 

End Sub 
+0

utiliser Google pour savoir comment vérifier l'imprimante par défaut et comment le changer – jsotola

+0

j'ai essayé de trouver le solution mais je n 'ai trouvé aucun thats pourquoi je pose la question sur la pile – linke95

Répondre

0

cela a pris moins de 10 minutes pour trouver

simplement changer votre default printer dans les fenêtres et vérifier la valeur de Application.ActivePrinter pour obtenir le nom exact de l'imprimante que vous souhaitez utiliser

Il existe des moyens d'obtenir une liste d'imprimantes système en exécutant des appels système.

Option Explicit 

Sub switchPrinters() 
    Dim ptr As String 

    ptr = Application.ActivePrinter 
    Application.ActivePrinter = "HP Deskjet 3520 USB on Ne03:" ' "printer_name on port_name" 
    Activesheet.Printout 
    Application.ActivePrinter = ptr 

End Sub 
+0

Merci pour votre réponse, mais comment puis-je imprimer le site Web à un dossier spécifique. J'ai un chemin vers le dossier. – linke95

+0

ce n'est pas votre question initiale. veuillez soumettre un autre post pour cette question. – jsotola

0

Mon code ressemble à ceci et maintenant je voudrais imprimer site au format PDF

Dim sPrinter As String 
Dim sDefaultPrinter As String 
Debug.Print "Default printer: ", Application.ActivePrinter 
sDefaultPrinter = Application.ActivePrinter ' store default printer 
sPrinter = GetPrinterFullName("Microsoft Print to PDF") 
If sPrinter = vbNullString Then ' no match 
    Debug.Print "No match" 
Else 
    Application.ActivePrinter = sPrinter 
    Debug.Print "Temp printer: ", Application.ActivePrinter 
    ' do something with the temp printer 
    ' Przechodzenie przez strony wraz z zapisem 
    For Each URL In URLs 
    Dim IEs As InternetExplorerMedium 
    Set IEs = CreateObject("InternetExplorer.Application") 
    str_text = Replace(URL.getAttribute("href"), "", "") 
    str_text = Replace(str_text, "", "") 

    IEs.Navigate str_text 
    IEs.Visible = True 
    Do While IEs.ReadyState <> READYSTATE_COMPLETE Or IEs.Busy 
    Loop 



    'HERE I WOLULD LIKE TO PRINT IEs TO PDF (specific path, and filename:) 


    IEs.Quit 
    Set IEs = Nothing 
    i = i + 1 
    If i = 5 Then Exit For 
    Next URL 
    Application.ActivePrinter = sDefaultPrinter ' restore default printer 
End If 
Debug.Print "Default printer: ", Application.ActivePrinter