Bom dia!
Meu código é basicamente uma mala direta.
Ele pega os valores dos campos do "Userform1" e quando se clica em "gerar ar" ele é executado
Estou com um problema numa parte do código que não consigo solucionar na seguinte linha
tb1 = Controls("TextBox" & num & " " & "TextBox" & 140 + num & "-" & "TextBox" & 160 + num).Value 'cep + cidade + uf
O código é esse e o arquivo segue anexo
Private Sub CommandButton2_Click()
Dim Word As Word.Application
Dim DOC As Word.Document
Dim num
Dim tb1, tb2, tb3, tb4, tb5, tb6, tb7, tb8, cm2, cm3, cm4, cm5, cm41
Set Word = CreateObject("Word.Application")
Word.Visible = True
Set DOC = Word.Documents.Open("C:UsersSecDesktopMODELO.docx")
With DOC
For num = 1 To 20
If Controls("TextBox" & num).Value <> "" Then
tb1 = Controls("TextBox" & num & " " & "TextBox" & 140 + num & "-" & "TextBox" & 160 + num).Value 'cep + cidade + uf
tb2 = Controls("TextBox" & 100 + num & " " & "TextBox" & 20 + num).Value 'endereço + número
tb3 = Controls("TextBox" & 40 + num).Value 'complemento
tb4 = Controls("TextBox" & 60 + num).Value 'nome1
tb5 = Controls("TextBox" & 80 + num).Value 'nome2
tb6 = Controls("TextBox" & 120 + num).Value 'bairro
tb7 = Controls(tb3 & " " & tb7).Value 'complemento + bairro
tb8 = Controls("TextBox" & num).Value
If tb3 <> "" Then
cm41 = tb7
Else
cm41 = tb6
End If
If tb5 <> "" Then
cm2 = tb5
cm3 = tb2
cm4 = cm41
cm5 = tb1
Else
cm2 = tb2
cm3 = cm41
cm4 = tb1
cm5 = ""
End If
.Application.Selection.Find.Text = "#CMP1"
.Application.Selection.Find.Execute
.Application.Selection.Range = tb4
.Application.Selection.Find.Text = "#CMP2"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm2
.Application.Selection.Find.Text = "#CMP3"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm3
.Application.Selection.Find.Text = "#CMP4"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm4
.Application.Selection.Find.Text = "#CMP5"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm5
.Application.Selection.Find.Text = "@CMP1"
.Application.Selection.Find.Execute
.Application.Selection.Range = tb4
.Application.Selection.Find.Text = "@CMP2"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm2
.Application.Selection.Find.Text = "@CMP3"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm3
.Application.Selection.Find.Text = "@CMP4"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm4
.Application.Selection.Find.Text = "@CMP5"
.Application.Selection.Find.Execute
.Application.Selection.Range = cm5
End If
Next num
If Dir("C:UsersSecDesktopMODELO2.docx") <> "" Then
Kill "C:UsersSecDesktopMODELO2.docx"
End If
.SaveAs ("C:UsersSecDesktopMODELO2.docx")
'.Close
End With
'WORD.Quit
Set DOC = Nothing
Set Word = Nothing
End Sub
Desde já, obrigado!
Postado : 20/01/2015 7:48 am