Allegare File XPS una alternativa ai PDF (Prima parte)

Transcript

Allegare File XPS una alternativa ai PDF (Prima parte)
Il Blog di Excelvba
Allegare File XPS una alternativa ai PDF (Prima parte)
Inviato da Roberto
martedì 24 marzo 2009
Ultimo aggiornamento martedì 24 marzo 2009
Il codice che vi propongo utilizza Microsoft XPS Document Writer per stampare un file in formato XPS ovvero l'alternativa
Microsoft al formato PDF.
Per chi prima vuole saperne di più consiglio di fare un giro sul web:
http://www.google.it/search?hl=it&q=Microsoft+XPS+Document+Writer&btnG=Cerca&meta=lr%3Dlang_it
Per scaricare Microsoft XPS Document Writer
http://www.microsoft.com/windows/windows-vista/features/xps.aspx
Se lo avete già installato quando stampate potete scegliere tra le stampanti Microsoft XPS Document Writer, l'utilizzo è
molto simile alle stampanti PDF.
Purtroppo non è possibile (io non ci sono riuscito) controllare le dll di riferimento, quindi utilizzo semplicemente il metodo
PrintOut.
Il codice che riporto offre alcuni spunti anche su come lavorare con Outlook, creare un messaggio di posta elettronica
con allegato un file, e come utilizzare la proprietà HTMLBody (per esempio con la routine RangeInHtml, che recupera una
stringa html originata dal foglio salvato come pagina web).
La funzione PrinterList che utilizza l'API GetProfileString e restituisce la lista delle stampanti installate. Io la uso
sopratutto per recuperare, prima di lanciare la stampa, il nome delle stampanti in rete.
Come sempre alla fine troverete 2 routine (test_Mail_con_allegato_XPS_1, test_Mail_con_allegato_XPS_2) che servono
a testare e capire il modo con cui chiamare il resto del codice, a questo riguardo potete incollare tutto il codice qui sotto
in un modulo di una nuova cartella di lavoro.
Buon lavoro
Saluti
r
Option Explicit
'Funzione che recupera e legge una stringa di testo
'in un file INI in relazione alla chiave indicata.
'Minimum supported: client Windows 2000 Professional
'Minimum supported: server Windows 2000 Server
'Header: Winbase.h (include Windows.h)
'Library: kernel32.lib
'dll: kernel32.dll
'Unicode and ANSI names: GetProfileStringW (Unicode) and GetProfileStringA (ANSI)
'Per saperne di più:
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
'http://msdn.microsoft.com/en-us/library/ms724366(VS.85).aspx
Private Declare Function GetProfileString _
Lib "kernel32" _
Alias "GetProfileStringA" ( _
ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long) As Long
Function PrinterList() As String()
Dim lRet As Long
Dim sBuffer As String
Dim lSize As Long
Dim avTmp() As String
Dim aPrn() As String
Dim n%, sPrn$, sConn$, sPort$
'Funzione che utilizza l'API GetProfileString
'e restituisce la lista delle stampanti
avTmp = Split(Excel.ActivePrinter)
sConn = " " & avTmp(UBound(avTmp) - 1) & " "
lSize = 1024
sBuffer = Space(lSize)
lRet = GetProfileString("devices", _
vbNullString, _
vbNullString, _
sBuffer, lSize)
sBuffer = Left(sBuffer, lRet)
avTmp = Split(sBuffer, Chr(0))
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
ReDim Preserve avTmp(UBound(avTmp) - 1)
For n = 0 To UBound(avTmp)
lSize = 128
sBuffer = Space(lSize)
lRet = GetProfileString("devices", _
avTmp(n), vbNullString, _
sBuffer, lSize)
sPort = Mid(sBuffer, InStr(sBuffer, ",") + 1, _
lRet - InStr(sBuffer, ","))
avTmp(n) = avTmp(n) & sConn & sPort
Next
PrinterList = avTmp
End Function
Function Print_SelectedSheets_XPS() As String
Dim StampAttiva As String, s As String
Dim listaS() As String, temp
Dim TempFile As String
Dim b As Boolean
'funzione che crea un file xps e ne restituisce
'il percorso
'viene creato il file xps del foglio attivo
'nome della stampante XPS
'per scaricare Microsoft XPS Viewer
'http://www.microsoft.com/windows/windows-vista/features/xps.aspx
Const MiaStamp As String = _
"Microsoft XPS Document Writer"
'assegno il nome per stampa su file
TempFile = Environ$("temp") & _
Application.PathSeparator & _
ActiveSheet.Name & _
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
".xps"
'ricordo la stampante attiva
StampAttiva = Application.ActivePrinter
'recupero la lista delle stampanti installate
listaS = PrinterList
'recupero il nome completo della stampante
For Each temp In listaS
s = CStr(temp)
If Left(s, 29) = MiaStamp Then
b = True
Exit For
End If
Next
'se la stampante non esiste
'Print_SelectedSheets_XPS =""
If b Then
'stampo su file in formato xps
ActiveSheet.PrintOut _
, , , , s, True, False, TempFile
'riporto la stampante attiva
Application.ActivePrinter = StampAttiva
'restituisco il nome del file salvato
Print_SelectedSheets_XPS = TempFile
End If
End Function
Sub Mail_con_allegato_XPS( _
Temp_file_name As String, _
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
Optional sTo As String, _
Optional sCC As String, _
Optional sSubject As String, _
Optional sHtmlBody As String)
'Temp_file_name è il percorso completo
'del file da allegare
'i parametri opzionali indicano
'destinatario
'in copia
'oggetto
'corpo del messaggio
'per maggiori dettagli consultare la
'guida in linea riguardo l'oggetto
'MailItem di Outlook Application
Dim oOutApp As Object
Dim oMail As Object
Const olMailItem As Long = 0
Const olFormatHTML As Long = 2
'setto gli oggetti applicazione e nuova mail
Set oOutApp = CreateObject("Outlook.Application")
Set oMail = oOutApp.CreateItem(olMailItem)
With oMail
.To = sTo
.CC = sCC
.Subject = sSubject
.BodyFormat = olFormatHTML
.HTMLBody = sHtmlBody
'allego il file
.Attachments.Add Temp_file_name
'mostro il messaggio di posta
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
'appena creato
.Display
End With
'elimino il file xps
Kill Temp_file_name
End Sub
Function Testo_Mail_Stampa_XPS() As String
'Funzione che restituisce una stringa testo
'HTML per la creazione del corpo del messsaggio
Dim h As String
h = "<html>"
h = h & Chr(13) & "<head>"
h = h & Chr(13) & "<meta http-equiv=""Content-Language"" cont"
h = h & "ent=""it"">"
h = h & Chr(13) & "<meta http-equiv=""Content-Type"" content="
h = h & """text/html; charset=windows-1252"">"
h = h & Chr(13) & "<title>Nuova pagina 1</title>"
h = h & Chr(13) & "</head>"
h = h & Chr(13) & "<body>"
h = h & Chr(13) & "<p>Testo HTML nel messaggio di posta e"
h = h & "lettronica ...</p>"
h = h & Chr(13) & "<p>Per scaricare "Microsoft XPS Doc"
h = h & "ument Writer" link:"
h = h & Chr(13) & "<a href=""http://www.microsoft.com/whdc/x"
h = h & "ps/viewxps.mspx"">"
h = h & Chr(13) & "http://www.microsoft.com/whdc/xps/viewxp"
h = h & "s.mspx</a></p>"
h = h & Chr(13) & "<p>Per saperne di più sul formato XPS li"
h = h & "nk:"
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
h = h & Chr(13) & "<a href=""http://www.microsoft.com/italy/"
h = h & "pmi/comefare/tecnologia/xps/default.mspx"">"
h = h & "http://www.microsoft.com/italy/pmi/come"
h = h & "fare/tecnologia/xps/default.mspx</a></p>"
h = h & Chr(13) & "<p>&nbsp;</p>"
h = h & Chr(13) & "<p>&nbsp;</p>"
h = h & Chr(13) & "</body>"
h = h & Chr(13) & "</html>"
Testo_Mail_Stampa_XPS = h
End Function
Function RangeInHtml(rng As Range) As String
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
Const ForReading As Long = 1
'Funzione a cui viene passato un range
'restituisce una stringa html che visualizza
'il range
'utilizzabile per creare il testo di messaggi
'di posta elettronica
'Creo un nome di file temporaneo
TempFile = Environ$("temp") & "/" & _
Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'recupero la cartella del Range passato alla
'funzione
Set TempWB = rng.Parent.Parent
'salvo la cartella come pagina web
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=rng.Parent.Name, _
Source:=rng.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'setto FSO
Set fso = CreateObject("Scripting.FileSystemObject")
'recupero il file in lettura
Set ts = fso.GetFile(TempFile). _
OpenAsTextStream(ForReading)
'lo leggo tutto impostando il risultato della funzione
RangeInHtml = ts.ReadAll
'chiudo il file
ts.Close
'di defoult il testo viene disposto al centro
'... meglio a sinistra
RangeInHtml = Replace(RangeInHtml, _
"align=center x:publishsource=", _
"align=left x:publishsource=")
'cancello la pagina web creata in precedenza
Kill TempFile
End Function
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
Sub test_Mail_con_allegato_XPS_1()
Dim s As String, rng As Excel.Range
'routine che testa la procedura
'utilizza la funzione Testo_Mail_Stampa_XPS
'per scrivere il testo del messaggio
'crea un foglio da stampare
'con un testo di prova
Set rng = ActiveWorkbook.Worksheets.Add.Range("A1")
rng.Parent.Name = "Per_stampa_XPS" & _
Format(Now, "dd-mm-yy h-mm-ss")
rng.Value = "Prova testo per stampa in formato XPS"
s = Print_SelectedSheets_XPS
If Len(s) Then
Mail_con_allegato_XPS _
s, _
"[email protected]", , _
"Allegare File XPS", _
Testo_Mail_Stampa_XPS
Else
MsgBox "Microsoft XPS Document Writer" & _
", non istallata!"
End If
End Sub
Sub test_Mail_con_allegato_XPS_2()
Dim s As String, rng As Excel.Range
'routine che testa la procedura
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51
Il Blog di Excelvba
'utilizza la funzione RangeInHtml
'con il testo del range A1:A10 del
'foglio attivo
'per scrivere il testo del messaggio
'crea un foglio da stampare
'con un testo di prova
Set rng = ActiveWorkbook.Worksheets.Add.Range("A1")
rng.Parent.Name = "Per_stampa_XPS" & _
Format(Now, "dd-mm-yy h-mm-ss")
rng.Value = "Prova testo per stampa in formato XPS"
rng.EntireColumn.AutoFit
s = Print_SelectedSheets_XPS
If Len(s) Then
Mail_con_allegato_XPS _
s, _
"[email protected]", , _
"Allegare File XPS", _
RangeInHtml(rng)
Else
MsgBox "Microsoft XPS Document Writer" & _
", non istallata!"
End If
End Sub
http://excelvba.altervista.org/blog
Realizzata con Joomla!
Generata: 29 September, 2016, 23:51