Re: copy date from excel userform to word from templateo for simple example of what I
Hello,
So the final document should look something like this:
Code
Option Explicit
Sub TestOpenDoc() 'Open new document based on SOA template
Dim wdApp As Object
Dim wdDoc As Object
Dim SaveName As String
Dim FileExt As String
' Set the variables
Set wdApp = CreateObject("Word.Application")
' Check that the excel object was created
If Not wdApp Is Nothing Then
' Open new word document using existing template.
wdApp.Visible = True
wdApp.Activate
Set wdDoc = wdApp.Documents.Add("H:\KiwiSaver\SOA\AdviceCheck_Kiwisaver SOA 11March 2015 testv2.dotx", , , True)
'Check which version of word user is using before saving file extension
If wdApp.Version < 11 Then
FileExt = ".doc"
Else
FileExt = ".docx"
End If
'Cover letter and Page 1 SOA
With wdDoc
' 'Cover letter and Page 1 SOA
If .Bookmarks.Exists("ADD1") Then
.Bookmarks("ADD1").Range.Text = TextBox4.Value 'addr line1
End If
' If .Bookmarks.Exists("Add2") Then
' .Bookmarks("Add2").Range.Text = TextBox5.Value 'addr line2
' ' .Bookmarks("Add2").Range.Text = "Hastings"
' End If
' If .Bookmarks.Exists("Add3") Then
' .Bookmarks("Add3").Range.Text = TextBox6.Value 'addr line3
' '.Bookmarks("Add3").Range.Text = "Hawkes Bay" 'addr line3
' End If
' Create the file name and save
SaveName = Environ("userprofile") & "\Desktop\" & frmPersonalData.TextBox2 & ", " & frmPersonalData.TextBox1 & " KiwiSaverSOA " & Format(Date, "dd mmm yyyy") & FileExt
.SaveAs2 SaveName
End With
' Inform the user
Beep
MsgBox "Data successfully transferred. File has been saved."
Else
MsgBox "There was a problem opening Word.", vbCritical
End If
' Clean up
Unload frmPersonalData
Set wdApp = Nothing
End Sub
Display More