Hi all, first I apologize for my English (I'm Italian), i need help with this vba, i use it to save a mail merge in pdf / doc, but i can't use the same name as the original file + variable part
Thanks
Code
'Option Explicit
Option Compare Text
Public Sub Stampa_Word_Pdf()
On Error GoTo ErrH
Dim objWdMailMerge As Word.MailMerge
Dim lngRecNum As Long, I As Long
Dim sPath As String
Dim dPath As String
Dim sFilename As String
Dim sName As String
sPath = ActiveDocument.Path & "\"
dPath = ActiveDocument.Path & "\xyz"
Set objWdMailMerge = ThisDocument.MailMerge
With objWdMailMerge
.Destination = wdSendToNewDocument
With .DataSource
.ActiveRecord = wdLastRecord
lngRecNum = .ActiveRecord
.ActiveRecord = wdFirstRecord
Do
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
sName = .DataFields("codice").Value
If InStr(sName, ".") <> 0 Then sName = Left(sName, InStr(sName, ".") - 1) & ".docx"
If Len(sName) Then
objWdMailMerge.Execute
With ActiveDocument
.SaveAs dPath & sName, wdFormatDoc, AddToRecentFiles:=False
.SaveAs dPath & sName, wdFormatPDF, AddToRecentFiles:=False
.Saved = True
.Close
I = I + 1
End With
End If
If .ActiveRecord = lngRecNum Then Exit Do
.ActiveRecord = wdNextRecord
Loop
End With
End With
ExitProc:
Set objWdMailMerge = Nothing
Exit Sub
ErrH:
MsgBox Err.Description
Resume ExitProc
End Sub
Display More