Não tenho certeza de como se obtém o valor de #Id de um Doc.
Mas a rotina pode ser algo +/- assim:
Sub gera_pdf()
Dim Path as string
Dim nArquivo as string
Path= "C:Usersuser1DesktopGerador_Contratoscontratos"
nArquivo= #Id & #LOCATARIO&".pdf"
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
path&nArquivo, 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
ChangeFileOpenDirectory "C:Usersuser1DesktopGerador_Contratoscontratos"
End Sub
Uma outra possibilidade e passar o nome do arquivo quando do chamado da rotina Gera_Pdf
A rotina que "monta" o doc ficaria =/- assim:
Private Sub CommandButton1_Click()
Dim Word As Word.Application
Dim DOC As Word.Document
Set Word = CreateObject("Word.Application")
Word.Visible = True
Set DOC = Word.Documents.Open("C:Usersuser1DesktopGerador_Contratosbincontrato.docx")
With DOC
.Application.Selection.Find.Text = "#ID"
.Application.Selection.Find.Execute
.Application.Selection.Range = txt_id
.Application.Selection.Find.Text = "#LOCATARIO"
.Application.Selection.Find.Execute
.Application.Selection.Range = cmb_nome
.Application.Selection.Find.Text = "#CNPJ"
.Application.Selection.Find.Execute
.Application.Selection.Range = txt_cnpj
.Application.Selection.Find.Text = "#CPF"
.Application.Selection.Find.Execute
.Application.Selection.Range = txt_cpf
.Application.Selection.Find.Text = "#ENDERECO"
.Application.Selection.Find.Execute
.Application.Selection.Range = txt_end
Dim Path as string
Dim nArquivo as string
Path= "C:Usersuser1DesktopGerador_Contratoscontratos"
nArquivo= txt_id &"_" & cmb_nome&".pdf""
If Dir("C:Usersuser1DesktopGerador_Contratosbincontrato2.docx") <> "" Then
End If
.SaveAs ("C:Usersuser1DesktopGerador_Contratoscontratoscontrato2.docx")
gera_pdf1(Path&nArquivo)
.Close
End With
Word.Quit
Set DOC = Nothing
Set Word = Nothing
Kill "C:Usersuser1DesktopGerador_Contratoscontratoscontrato2.docx"
end sub
e o Gera_PDF
Sub gera_pdf1(byval Arquivo as string)
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
Arquivo, 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
ChangeFileOpenDirectory "C:Usersuser1DesktopGerador_Contratoscontratos"
End Sub
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel
Postado : 01/02/2016 1:43 pm