Ciao,
ho provato ad usare tutte le soluzioni proposte in questo thread ma nessuna mi stampa i diversi file .pdf
che cosa c'è di sbagliato nel comando?
Perché a volte mi dice oggetto inesistente? Che oggetto è inesistente?
Uso un file word in versione macro, che usa come sorgente dati un file excel
Caso 1:
' Modulo1 - Modulo
'
Option Explicit
Public Sub Test2()
On Error GoTo ErrH
Dim objWdMailMerge As Word.MailMerge
Dim lngRecNum As Long
Dim strPath As String
Dim strFilename As String
Application.ScreenUpdating = False
strPath = "**C:\Ste\Work\Messa a Disposizione**"
Set objWdMailMerge = ThisDocument.MailMerge
With objWdMailMerge
.Destination = wdSendToNewDocument
With .DataSource
.ActiveRecord = wdLastRecord
lngRecNum = .ActiveRecord
.ActiveRecord = wdFirstRecord
Do
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
strFilename = .DataFields("Nome").Value
If Len(strFilename) Then
strFilename = strPath & strFilename
objWdMailMerge.Execute
With ActiveDocument
.SaveAs strFilename, wdFormatPDF, AddToRecentFiles:=False
.Saved = True
.Close
End With
End If
If .ActiveRecord = lngRecNum Then Exit Do
.ActiveRecord = wdNextRecord
Loop
End With
End With
ExitProc:
Application.ScreenUpdating = True
Set objWdMailMerge = Nothing
Exit Sub
ErrH:
MsgBox Err.Description
Resume ExitProc
End Sub
Caso 2:
' Modulo1 - Modulo
'
Option Explicit
Public Sub Test()
On Error GoTo ErrH
Dim mm As Word.MailMerge
Dim i As Long
Dim s As String
Application.ScreenUpdating = False
Set mm = ThisDocument.MailMerge
With mm
.Destination = wdSendToNewDocument
With .DataSource
For i = 1 To .RecordCount
.FirstRecord = i
.LastRecord = i
mm.Execute
s = "**C:\Ste\Work\Messa a Disposizione**"
s = s & "Ste.F"
s = s & "_"
s = s & "MAD"
s = s & "_"
s = s & .DataFields(.FieldNames("Nome")).Value
With Application.ActiveDocument
.SaveAs s _
, wdFormatPDF _
, AddToRecentFiles:=False , wdFormatPDF _
, AddToRecentFiles:=False
.Saved = True
.Close
End With
Next
End With
End With
ExitProc:
Application.ScreenUpdating = True
Set mm = Nothing
Exit Sub
ErrH:
MsgBox Err.Description
Resume ExitProc
End Sub
CASO 3
anche provando con quella di seguito, non so cosa e come modificare!!
- Sub Unione_in_pdf()
- Dim fd
As FileDialog
- 'Crea un oggetto FileDialog per scegliere la cartella in cui salvare i file
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- With fd
- 'Usa il metodo Show per mostrare la finestra di dialogo e restituire l'azione dell'utente
- If .Show = -1
Then
- For
Each vrtSelectedItem In .SelectedItems
- 'vrtSelectedItem è una stringa che contiene l'indirizzo di ogni elemento selezionato.
- 'E' possibile usare qualsiasi funzione di I/O sui file utilizzando questo indirizzo.
- SelectedPath = vrtSelectedItem
- Next vrtSelectedItem
- Else
- MsgBox ("Nessuna cartella è stata selezionata.")
- Exit
Sub
- End
If
- End
With
- 'Imposta la variabile oggetto a Nothing
- Set fd =
Nothing
- Application.ScreenUpdating =
False
- MainDoc = ActiveDocument.Name
- ChangeFileOpenDirectory SelectedPath
- For i = 1
To ActiveDocument.MailMerge.DataSource.RecordCount
- With ActiveDocument.MailMerge
- .Destination = wdSendToNewDocument
- .SuppressBlankLines =
True
- With .DataSource
- .FirstRecord = i
- .LastRecord = i
- .ActiveRecord = i
- 'Utilizza alcuni campi del file sorgente per impostare il nome del file pdf
- 'IMPORTANTE: tali campi vanno personalizzati in base a quelli effettivamente
- 'presenti nella sorgente dati
- docName = "Lettera_" & .DataFields("Nominativo").Value &
"_" & .DataFields("ID").Value &
".pdf"
- End
With
- .Execute Pause:=False
- Application.ScreenUpdating =
False
- End
With
- ActiveDocument.ExportAsFixedFormat OutputFileName:=docName, _
- ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
- wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1,
To:=1, _
- Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
- CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
- BitmapMissingFonts:=True, UseISO19005_1:=False
- ActiveWindow.Close SaveChanges:=False
- Next i
- Application.ScreenUpdating =
True
- End
Sub
Che cosa sbaglio? Perché mi salva un solo file in Word e non in .pdf, relativo solo alla prima riga della stampa unione?