Apologies for all me recent bumps, I have now found two solution that slightly differ from eachother and they both work.
I would like your views on each one, any bugs or performance issues so I know I am using the correct one.
Summary: The macro will use cells from the excel sheet and find/replace the match in a word letter to auto generate a pre-filled letter then print and close.
Macro 1:
Macro 2:
Many thanks and feel free to use yourselves if this is something your looking for.
I would like your views on each one, any bugs or performance issues so I know I am using the correct one.
Summary: The macro will use cells from the excel sheet and find/replace the match in a word letter to auto generate a pre-filled letter then print and close.
Macro 1:
Code:
Sub Replacing()
Dim sFile As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim sInput(7) As String, sOutput(7) As String
sFile = "Pack"
Set wrdApp = New Word.Application
With wrdApp
.Visible = True
Set wrdDoc = .Documents.Open("C:\Users\Admin\Desktop\" + sFile + ".doc")
.Selection.Find.ClearFormatting
.Selection.Find.Replacement.ClearFormatting
sInput(0) = "C2"
sInput(1) = "C3"
sInput(2) = "C8"
sInput(3) = "C9"
sInput(4) = "C10"
sInput(5) = "C11"
sInput(6) = "C12"
sOutput(0) = "NAME1"
sOutput(1) = "NAME2"
sOutput(2) = "Address Line 1"
sOutput(3) = "Address Line 2"
sOutput(4) = "Address Line 3"
sOutput(5) = "Address Line 4"
sOutput(6) = "Address Line 5"
For i = 0 To UBound(sInput) - 1
With .Selection.Find
.Text = sOutput(i)
.Replacement.Text = Range(sInput(i))
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next
End With
wrdDoc.PrintOut
wrdDoc.Close False
wrdApp.Quit False
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub
Macro 2:
Code:
Sub Replacing()
Dim sFile As String
sFile = "Pack"
Set objword = CreateObject("Word.Application")
objword.Visible = True
Set objdoc = objword.Documents.Open("C:\Users\Admin\Desktop\" + sFile + ".doc")
Set objSelection = objword.Selection
objSelection.Find.Forward = True
objSelection.Find.MatchWholeWord = True
objSelection.Find.Text = "NAME1"
objSelection.Find.Replacement.Text = Range("C2")
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
objSelection.Find.Text = "NAME2"
objSelection.Find.Replacement.Text = Range("C3")
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
objSelection.Find.Text = "Address Line 1"
objSelection.Find.Replacement.Text = Range("C8")
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
objSelection.Find.Text = "Address Line 2"
objSelection.Find.Replacement.Text = Range("C9")
objSelection.Find.Execute , , , , , , , , , , wdReplaceAll
objdoc.PrintOut
objdoc.Close SaveChanges:=False
objword.Quit
End Sub
Many thanks and feel free to use yourselves if this is something your looking for.