I am extracting text between parens in the active window title bar. This part works great (thanks to some help I got here earlier!). Now I want to create two separate macros: one that returns only the first name, and the other that returns only the last name.
My active window title bar looks something like this:
some text on the left (HENDERSON, TOM) some text on the right (there is no space after the comma)
The last macro works fine. It looks like this:
Sub a1LastName() 'Extract last name of patient from title bar (between parens) Dim strPatientName As String Dim OpenPosition As Integer '(open paren marker) Dim closeposition As Integer '(close paren marker) OpenPosition = InStr(ActiveDocument.ActiveWindow.Caption, "(") closeposition = InStr(ActiveDocument.ActiveWindow.Caption, ")") strPatientName = Mid(ActiveDocument.ActiveWindow.Caption, _ OpenPosition + 1, closeposition - OpenPosition - 1) Dim c As Long c = InStr(strPatientName, ",") strPatientName = Left(strPatientName, c - 1) Selection.TypeText strPatientName End Sub
The second macro is identical to the first, except that the second or last line of code has a βrightβ and not a βleftβ command:
Sub a1FirstName() 'Extract first name of patient from title bar (between parens) Dim strPatientName As String Dim OpenPosition As Integer '(open paren marker) Dim closeposition As Integer '(close paren marker) OpenPosition = InStr(ActiveDocument.ActiveWindow.Caption, "(") closeposition = InStr(ActiveDocument.ActiveWindow.Caption, ")") strPatientName = Mid(ActiveDocument.ActiveWindow.Caption, _ OpenPosition + 1, closeposition - OpenPosition - 1) Dim c As Long c = InStr(strPatientName, ",") strPatientName = Right(strPatientName, c - 1) Selection.TypeText strPatientName End Sub
Here's my problem: the "first name" macro always returns the last name minus the first four characters, followed by the first name, not just the first name.
The only examples I can find anywhere on Google are specifically for Excel. I have combined my VBA guides and they all give similar examples that I used to extract the text to the right of the character.
What am I doing wrong?