Dave Coram
New Member
- Joined
- Apr 18, 2016
- Messages
- 21
Hi!
Hope someone can help with the following:
The code below (with the help of Macropod - thanks again!) takes details from a single row of a worksheet (row 4) and puts them into relevant places of a Word template, then saves it.
I'd like to be able to amend it to be able to produce the Word document from the last populated row of the worksheet (could run it as part of the registration process for a new teacher) - please could someone help me?
I guess it would use 'LastRow', and turn the data cell references to something like '"B" & LastRow', but I'm not yet at a standard to be able to implement this.
In an ideal world, I'd also have a second macro, where I'm able to specify a specific row that I need to run a replacement Word document for (should any of their registered details change) - would this be possible? Isn't an essential if it's not - can work without this!
Hope someone can help with the following:
The code below (with the help of Macropod - thanks again!) takes details from a single row of a worksheet (row 4) and puts them into relevant places of a Word template, then saves it.
Code:
Public Sub ExportToWord()
Dim WkSht As Worksheet
Set WkSht = ThisWorkbook.Worksheets(1)
Dim wrdApp As Object, wrdDoc As Object
Set wrdApp = CreateObject("Word.Application")
Const wdReplaceAll As Long = 2
Const wdNoProtection As Long = -1
Const wdAllowOnlyFormFields = 2
Dim StrPath As String, StrPwd As String
StrPath = "C:\Users\Dave\Desktop\"
StrPwd = "claire020781"
With wrdApp
.Visible = True
.Application.ScreenUpdating = False
Set wrdDoc = .Documents.Add(StrPath & "NQT Assessment Record.dotx")
With wrdDoc
If .ProtectionType <> wdNoProtection Then .Unprotect Password:=StrPwd
With .Range.Find
.Text = "First Name"
.Replacement.Text = WkSht.Range("B4").Value
.Execute Replace:=wdReplaceAll
.Text = "Last Name"
.Replacement.Text = WkSht.Range("C4").Value
.Execute Replace:=wdReplaceAll
.Text = "DoB Date"
.Replacement.Text = WkSht.Range("E4").Value & " " & _
WkSht.Range("F4").Value & " " & WkSht.Range("G4").Value
.Execute Replace:=wdReplaceAll
.Text = "TRefNo"
.Replacement.Text = WkSht.Range("H4").Value
.Execute Replace:=wdReplaceAll
.Text = "School Name"
.Replacement.Text = WkSht.Range("A4").Value
.Execute Replace:=wdReplaceAll
.Text = "DpP"
.Replacement.Text = WkSht.Range("Y4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP1 Start"
.Replacement.Text = WkSht.Range("AA4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP1 End"
.Replacement.Text = WkSht.Range("AB4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP1 Due"
.Replacement.Text = WkSht.Range("AC4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP2 Start"
.Replacement.Text = WkSht.Range("AE4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP2 End"
.Replacement.Text = WkSht.Range("AF4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP2 Due"
.Replacement.Text = WkSht.Range("AG4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP3 Start"
.Replacement.Text = WkSht.Range("AI4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP3 End"
.Replacement.Text = WkSht.Range("AJ4").Value
.Execute Replace:=wdReplaceAll
.Text = "IP3 Due"
.Replacement.Text = WkSht.Range("AK4").Value
.Execute Replace:=wdReplaceAll
End With
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=StrPwd
.SaveAs (StrPath & WkSht.Range("C4").Value & " " & WkSht.Range("B4").Value & ".docx")
.Close
End With
.Quit
End With
End Sub
I'd like to be able to amend it to be able to produce the Word document from the last populated row of the worksheet (could run it as part of the registration process for a new teacher) - please could someone help me?
I guess it would use 'LastRow', and turn the data cell references to something like '"B" & LastRow', but I'm not yet at a standard to be able to implement this.
In an ideal world, I'd also have a second macro, where I'm able to specify a specific row that I need to run a replacement Word document for (should any of their registered details change) - would this be possible? Isn't an essential if it's not - can work without this!