Thrillho
02-21-2013, 03:15 PM
OS: Windows 7
Office: 2010
I have a standard template (template.docx) for a report which gets filled out about a few hundred times a year which I'm trying to automate. One of the elements of these reports is who the report is done by. Generally one person fills out the report, but up to four people can have their names on it.
My goal is to create a macro to fill out each person's Name, Title, and Email based on another document (signatures.docx). This document will contain about 50 names / titles / emails.
In the code below, I've been able to crudely get the first name filled in successfully. Upon running, a window pops up asking who you're looking for (initials of the individual), then it opens signatures.docx, searches in the table for those initials, moves to the next cell to where the signature is, copies the signature, closes signatures.docx, then pastes the signature in the appropriate spot in template.docx.
My problem is that while I can copy multiple names (not shown in code), I cannot paste each one. I don't know how to say something like "as = Person A signature" in VBA, and have "as" pasted somewhere in Word (and bs, cs, ds).
A very crude solution to this would be to have it repeat itself four times. But I would rather not have the document open and close four times.
Sub signature()
a = InputBox("Name")
ChangeFileOpenDirectory "C:\Users\thrillho\Desktop\Report\"
Documents.Open FileName:="Signatures.docx", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = a
.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
ActiveWindow.Close
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Submitted By"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.PasteAndFormat wdFormatOriginalFormatting
Selection.Delete Unit:=wdCharacter, Count:=7
End Sub
Thoughts on how I should go about solving this?
Thrillho
Office: 2010
I have a standard template (template.docx) for a report which gets filled out about a few hundred times a year which I'm trying to automate. One of the elements of these reports is who the report is done by. Generally one person fills out the report, but up to four people can have their names on it.
My goal is to create a macro to fill out each person's Name, Title, and Email based on another document (signatures.docx). This document will contain about 50 names / titles / emails.
In the code below, I've been able to crudely get the first name filled in successfully. Upon running, a window pops up asking who you're looking for (initials of the individual), then it opens signatures.docx, searches in the table for those initials, moves to the next cell to where the signature is, copies the signature, closes signatures.docx, then pastes the signature in the appropriate spot in template.docx.
My problem is that while I can copy multiple names (not shown in code), I cannot paste each one. I don't know how to say something like "as = Person A signature" in VBA, and have "as" pasted somewhere in Word (and bs, cs, ds).
A very crude solution to this would be to have it repeat itself four times. But I would rather not have the document open and close four times.
Sub signature()
a = InputBox("Name")
ChangeFileOpenDirectory "C:\Users\thrillho\Desktop\Report\"
Documents.Open FileName:="Signatures.docx", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = a
.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
ActiveWindow.Close
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Submitted By"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
Selection.PasteAndFormat wdFormatOriginalFormatting
Selection.Delete Unit:=wdCharacter, Count:=7
End Sub
Thoughts on how I should go about solving this?
Thrillho