Sub Secondments()
' Declare variables
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wsInput As Worksheet
Dim wsSecondments As Worksheet
Dim lastRow As Long
Dim i As Long
Dim targetPath As String
Dim fileName As String
Dim found As Boolean
Dim oRng As Word.Range ' Declare the Word Range object
Dim para As Word.Paragraph 'Declare the Word Paragraph object
' Set object variables
Set wdApp = New Word.Application
Set wsInput = ThisWorkbook.Worksheets("User Input")
Set wsSecondments = ThisWorkbook.Worksheets("Secondments")
' Set Word application to be visible (optional, for debugging)
wdApp.Visible = True
' Get the last row from "Secondments" sheet
lastRow = wsSecondments.Cells(wsSecondments.Rows.Count, "A").End(xlUp).Row
' Define the target path. Use a constant for the template path.
Const TEMPLATE_PATH As String = "\\Hbap.adroot.hsbc\au\IT Operations\DATA\Restricted\HeadOffice\HPE\Recruitment Centre\Recruitment Process Australia\ _
Offers\Secondments\Automated Letters\Secondme Template.docx"
targetPath = ThisWorkbook.Path & "\" ' Corrected path concatenation
' Loop through the rows in "Secondments" sheet
For i = 2 To lastRow
' Open the Word document (use the constant)
Set wdDoc = wdApp.Documents.Open(TEMPLATE_PATH)
' Check if the document was opened successfully
If Not wdDoc Is Nothing Then
' Optimize: Get values from the worksheet into variables. This is much faster.
Dim H As String, V As String, P As String
H = wsSecondments.Cells(i, 20).Value
V = wsSecondments.Cells(i, 31).Value
P = wsSecondments.Cells(i, 33).Value
' Delete paragraphs based on conditions (H, V, P)
' Combine the logic for deleting paragraphs for efficiency
Set oRng = wdDoc.Content 'Set the range to the entire document.
oRng.Find.Wrap = wdFindStop 'Set the Wrap property of the Find object
If H = "N" Then
DeleteParagraphs oRng, "<<HDACopy1>>"
DeleteParagraphs oRng, "<<HDACopy5>>"
End If
If V = "N" Then
DeleteParagraphs oRng, "<<VisaCopy>>"
End If
If P = "N" Then
DeleteParagraphs oRng, "<<PTCopy>>"
End If
' Perform find and replace operations
With oRng.Find 'Use the same range object
.Execute FindText:="<<CandidateName>>", ReplaceWith:=wsSecondments.Cells(i, 1).Value, Replace:=wdReplaceAll
.Execute FindText:="<<Date>>", ReplaceWith:=wsSecondments.Cells(i, 39).Value, Replace:=wdReplaceAll
.Execute FindText:="<<Address1>>", ReplaceWith:=wsSecondments.Cells(i, 3).Value, Replace:=wdReplaceAll
.Execute FindText:="<<Address2>>", ReplaceWith:=wsSecondments.Cells(i, 4).Value, Replace:=wdReplaceAll
.Execute FindText:="<<Address3>>", ReplaceWith:=wsSecondments.Cells(i, 5).Value, Replace:=wdReplaceAll
.Execute FindText:="<<EmployeeFirstName>>", ReplaceWith:=wsSecondments.Cells(i, 6).Value, Replace:=wdReplaceAll
.Execute FindText:="<<PositionTitle>>", ReplaceWith:=wsSecondments.Cells(i, 7).Value, Replace:=wdReplaceAll
.Execute FindText:="<<Salary>>", ReplaceWith:=wsSecondments.Cells(i, 8).Value, Replace:=wdReplaceAll
.Execute FindText:="<<StartDate>>", ReplaceWith:=wsSecondments.Cells(i, 43).Value, Replace:=wdReplaceAll
.Execute FindText:="<<GCBChange>>", ReplaceWith:=wsSecondments.Cells(i, 11).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HoursChange>>", ReplaceWith:=wsSecondments.Cells(i, 14).Value, Replace:=wdReplaceAll
.Execute FindText:="<<ManagerName>>", ReplaceWith:=wsSecondments.Cells(i, 17).Value, Replace:=wdReplaceAll
.Execute FindText:="<<ManagerTitle>>", ReplaceWith:=wsSecondments.Cells(i, 18).Value, Replace:=wdReplaceAll
.Execute FindText:="<<CostCentre>>", ReplaceWith:=wsSecondments.Cells(i, 19).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HDACopy1>>", ReplaceWith:=wsSecondments.Cells(i, 24).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HDACopy2>>", ReplaceWith:=wsSecondments.Cells(i, 25).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HDACopy3>>", ReplaceWith:=wsSecondments.Cells(i, 26).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HDACopy4>>", ReplaceWith:=wsSecondments.Cells(i, 27).Value, Replace:=wdReplaceAll
.Execute FindText:="<<HDACopy5>>", ReplaceWith:=wsSecondments.Cells(i, 28).Value, Replace:=wdReplaceAll
.Execute FindText:="<<VisaCopy>>", ReplaceWith:=wsSecondments.Cells(i, 32).Value, Replace:=wdReplaceAll
.Execute FindText:="<<PTCopy>>", ReplaceWith:=wsSecondments.Cells(i, 34).Value, Replace:=wdReplaceAll
.Execute FindText:="<<EndDate>>", ReplaceWith:=wsSecondments.Cells(i, 47).Value, Replace:=wdReplaceAll
End With
' Construct the filename
fileName = wsSecondments.Cells(i, 1).Value & " " & wsSecondments.Cells(i, 35).Value & " " & wsSecondments.Cells(i, 39).Value
' Save the Word document
wdDoc.SaveAs2 Filename:=targetPath & fileName & ".docx"
' Export to PDF
wdDoc.ExportAsFixedFormat OutputFileName:=targetPath & fileName & ".pdf", ExportFormat:=wdExportFormatPDF
' Close the Word document
wdDoc.Close SaveChanges:=False
Else
MsgBox "Failed to open document: " & TEMPLATE_PATH, vbCritical
End If
Next i
' Quit Word
wdApp.Quit
Set wdApp = Nothing ' Release the object variable
End Sub
' Subroutine to delete paragraphs containing specific text
Sub DeleteParagraphs(ByRef oRng As Word.Range, ByVal searchText As String)
Dim found As Boolean
Dim para As Word.Paragraph
With oRng.Find
.Text = searchText
.Wrap = wdFindStop
found = .Execute
Do While found
Set para = oRng.Paragraphs(1) 'Gets the paragraph where the search text was found
para.Range.Delete
oRng.Collapse wdCollapseEnd
found = .Execute
Loop
End With
End Sub