OK, well after doing a lot of research and fiddling around, I managed to make this work. I'm posting it here in case others want to know how. I'm sure it's not the most elegant or efficient code but it does the job perfectly. I don't know how to make the title say "Solved" now.
Sub PrintWhileLoop()
Dim FullName As String
Dim c As Range
Dim MyList As Range
Dim DropRange As Range
Dim wsPrint As Range
Dim TaskName As String
Dim MyPathName As String
Dim MySheetName As String
Dim MyWorkbookName As String
MyPathName = Application.ActiveWorkbook.Path
MySheetName = ActiveWorkbook.ActiveSheet.Name
MyWorkbookName = Replace(ThisWorkbook.Name, ".xlsm", "")
'Where is list of names?
Set MyList = Worksheets("Students").Range("F2:F26")
'Where is dropdown?
Set DropRange = ActiveWorkbook.ActiveSheet.Range("A172")
'What is the name of the task?
TaskName = ActiveWorkbook.ActiveSheet.Range("A1") & " " & ActiveWorkbook.ActiveSheet.Range("C1")
'Which sheet to print?
Set wsPrint = ActiveWorkbook.ActiveSheet.Range("A172:F179")
'No dialog boxes during printing
Application.ScreenUpdating = False
'Make feedback folder if it doesn't exist
'Make the folder for the assessment task within feedback if it doesn't exist
On Error Resume Next
MkDir MyPathName & "\" & MyWorkbookName
MkDir MyPathName & "\" & MyWorkbookName & "\Feedback\"
MkDir MyPathName & "\" & MyWorkbookName & "\Feedback\" & TaskName
On Error GoTo 0
'Loop through each name
For Each c In MyList
FullName = c.Value
DropRange.Value = FullName
'Extract first and last name from the full name, so we can name the pdf with last name first
Dim FirstName As String
Dim LastName As String
Dim SpacePos As Integer
SpacePos = InStr(FullName, " ")
FirstName = Left(FullName, SpacePos - 1)
LastName = Right(FullName, Len(FullName) - Len(FirstName))
'Save a correctly named pdf file for each student
wsPrint.ExportAsFixedFormat xlTypePDF, MyPathName & "\" & MyWorkbookName & "\Feedback\" & TaskName & "\" & LastName & ", " & FirstName & " - " & MyWorkbookName & " - " & TaskName & ".pdf", , , , , , False
Next
'Turn back on any dialog boxes
Application.ScreenUpdating = True
End Sub
Bookmarks