Macro to copy text, then save file as copied text

I have a vast number of job applications where I am copying and pasting from a "print view" from a web browser into a MS Word document, which produces a document with lots of tables and nested tables, Somewhere in there is the name of the applicant... I can record a macro to find their name, since it'll always be in the same table/cell location, but I would like to have it Save As the name of the applicant as well. I can't seem to do that second part by just recording a macro.

Sub testing()
'
' testing Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "Name:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCell
    Selection.Copy

so far so good, but then I'm having problems with this part because it wants to save everything as "John Smith.doc" every time.

    ChangeFileOpenDirectory "C:\Users\fogharty\Desktop\"
    ActiveDocument.SaveAs2 FileName:="John Smith.doc", FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:= _
        True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
        False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0
End Sub

Ideally, it would save as the applicant's name plus the current date... "John Smith 1-20-2015.doc, and to a specific folder on the desktop. But perhaps I'm dreaming.

Any help will be greatly appreciated. Thank you.

Answer
Answer

After making the correction to the code that Stefan provided, to save to a specific folder and with the date, you would need to replace:

ActiveDocument.SaveAs FileName:=FoundName & ".doc"

with

ActiveDocument.SaveAs FileName:="path" & FoundName & Format(Date, "M-d-yyyy") & ".doc"

where in place of "path", you have something like

"C:\Users\[User Name]\Documents\Applications\"

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

In the second With statement, use .Find (with a leading period) instead. That should get rid of the error message.

In a With statement, the period/dot refers to the parent object (Selection in this case).

Stefan Blom
Microsoft 365 Word MVP since 2005
Volunteer Moderator (Office)
MS 365, Win 11 Pro
~~~~
Please note that I do not work for Microsoft
MVP program info: https://mvp.microsoft.com/
~~~~

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 1,637 Applies to: