Home > Software design >  Using text in front of "Name:" in filename
Using text in front of "Name:" in filename

Time:07-11

After I am done typing a Word document (in my case a clinic note), I run the following VBA to automatically save PDF and Word copies of the document.

The filename contains:
Date The first 2 words in the document. (eg. "2022-07-11 Timothy Dalton")

I want the filename to contain:
Date The first 2 words following the text "Patient Name:" in the document.

So,
Currently I need the first two words of the document to be the patient name. After (hopefully) the VBA code is modified, I will be able to write a more proper beginning:

Dear Dr. xyz,
It was a pleasure seeing Mr. Timothy Dalton to my clinic. Details as under:

Patient Name: Timothy Dalton
Age: 125 years
Gender: Male

.....
...
...
......
......

Sincerely,
Dr. Yes.

Current code:

Sub PDF_Sv_And_Pr()
    Dim InitialWords As Range
    Set InitialWords = ActiveDocument.Range(Start:=ActiveDocument.Words(1).Start, _
            End:=ActiveDocument.Words(2).End)
            
    Dim Dt As String: Dt = Format(Now(), "YYYY-MM-DD")

    With ActiveDocument
        ActiveDocument.SaveAs2 "G:\My Drive\Clinic Visits\" & Dt & " " & InitialWords & ".pdf", _
            FileFormat:=wdFormatPDF
        ActiveDocument.SaveAs2 "G:\My Drive\Clinic Visits\" & Dt & " " & InitialWords & ".docx", _
            FileFormat:=wdFormatDocumentDefault
    End With

    ActiveDocument.PrintOut
End Sub

CodePudding user response:

If you create a bookmark (called PName in this example) around the patient name, you can use this code:

Sub PDF_Sv_And_Pr()
    Dim PatientName As String
    Dim Dt As String: Dt = Format(Now(), "YYYY-MM-DD")

    PatientName = ActiveDocument.Bookmarks("PName").Range.Text

    With ActiveDocument
        .SaveAs2 "G:\My Drive\Clinic Visits\" & Dt & " " & PatientName & ".pdf", FileFormat:=wdFormatPDF
        .SaveAs2 "G:\My Drive\Clinic Visits\" & Dt & " " & PatientName & ".docx" = ActiveDocument.Bookmarks("PName").Range.Text & ".docx", FileFormat:=wdFormatDocumentDefault
    End With

    ActiveDocument.PrintOut
End Sub

CodePudding user response:

As Tom says, you'll run into problems with multi-word names if you're assuming the name consists of only two words. Presumably, your patients also have patient IDs, which you could use as part of the filename instead of their names. Nevertheless, rather than limiting things to just the first two words after 'Patient Name:' you could capture the rest of that line. For example:

Sub PDF_Sv_And_Prn()
Application.ScreenUpdating = False
Dim PatientName As String, Dt As String
Const Path As String = "G:\My Drive\Clinic Visits\": Dt = Format(Now(), "YYYY-MM-DD")
With ActiveDocument
  PatientName = " " & Trim(Split(Split(.Range.Text, "Patient Name:")(1), vbCr)(0))
  .SaveAs2 Path & Dt & PatientName & ".pdf", FileFormat:=wdFormatPDF
  .SaveAs2 Path & Dt & PatientName & ".docx", FileFormat:=wdFormatXMLDocument
  .PrintOut
End With
Application.ScreenUpdating = False
End Sub
  • Related