How to make a Loop in an If-Statement - excel

Short explanation: There are 3.Letter Templates and i want them to print per Button. But the main problem here is, that the Code is Printing the Template for every Person in the Worksheet also if the Person already had a Letter. It should look something like this.
-If the selected letter in "G3" is 1. Letter then send them only to People where the Cell Range in "Z" is Empty
-If the selected letter in "G3" is 2. Letter then send them only to People where the Cell in Range "Z" is 1.Letter
-If the selected letter in "G3" is 3. Letter then send them only to People where the Cell in Range "Z" is 2.Letter
What do i need to write right here?
Thank you for your answer in Advance!
https://i.stack.imgur.com/1NRbv.png
Option Explicit
Sub CreateWordDocuments()
Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim OutApp, OutMail As Object
Dim WordContent As Word.Range
Dim WordDoc As Word.Document
Dim WordApp As Word.Application
With Tabelle1
If IsEmpty(Range("G3").Value) = True Then
MsgBox "Bitte wählen sie eine Vorlage aus"
.Range("G3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value
TemplName = .Range("G3").Value
FrDays = .Range("L3").Value
ToDays = .Range("N3").Value
DocLoc = Tabelle2.Range("F" & TemplRow).Value
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
End If
***LastRow = .Range("E9999").End(xlUp).Row
For CustRow = 8 To LastRow
DaysSince = .Range("P" & CustRow).Value
If TemplName <> .Range("Z" & CustRow).Value And DaysSince >= FrDays And DaysSince <= ToDays Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False)
For CustCol = 5 To 26
TagName = .Cells(7, CustCol).Value
TagValue = .Cells(CustRow, CustCol).Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With***
Next CustCol
If .Range("I3").Value = "PDF" Then
FileName = "Filename" & "\" & .Range("H" & CustRow).Value & " " & .Range("G" & CustRow).Value & " " & .Range("G3").Value & ".pdf"
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else:
FileName = ThisWorkbook.Path & "\" & .Range("H" & CustRow).Value & "_" & .Range("G" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
.Range("Z" & CustRow).Value = TemplName
.Range("AA" & CustRow).Value = Now
If .Range("P3").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Tabelle1.Range("K" & CustRow).Value
.Subject = "Hallo, " & Tabelle1.Range("F" & CustRow).Value & "Test Test Test"
.Body = "Hallo, " & Tabelle1.Range("F" & CustRow).Value & "Test Test Test Test"
.Attachments.Add FileName
.Display
End With
Else:
WordDoc.PrintOut
WordDoc.Close
End If
Kill False '(FileName)
End If
Next CustRow
WordApp.Quit
End With
End Sub

Try the following: (not tested)
Sub CreateWordDocuments()
Dim CustRow As Long, CustCol As Long, LastRow As Long, TemplRow As Long, DaysSince As Long, FrDays As Long, ToDays As Long
Dim DocLoc As String, TagName As String, TagValue As String, TemplName As String, FileName As String
Dim CurDt As Date, LastAppDt As Date
Dim OutApp As Object, OutMail As Object
Dim WordContent As Word.Range
Dim WordDoc As Word.Document
Dim WordApp As Word.Application
'*~
Dim sLastSentTemplate As String
With Tabelle1
If IsEmpty(Range("G3").Value) = True Then
MsgBox "Bitte wählen sie eine Vorlage aus"
.Range("G3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value
TemplName = .Range("G3").Value
FrDays = .Range("L3").Value
ToDays = .Range("N3").Value
DocLoc = Tabelle2.Range("F" & TemplRow).Value
'*~ workout the last sent template name
'* this is what you'll be searching for in column Z
sLastTemplateTarget = GetLastSentTemplate(TemplName)
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
End If
'***LastRow = .Range("E9999").End(xlUp).Row
'*~
LastRow = .Cells(.Rows.Count, "E").End(xlUp).Row
For CustRow = 8 To LastRow
DaysSince = .Range("P" & CustRow).Value
'*~ changed TemplName to sLastSentTemplate
If sLastSentTemplate = .Range("Z" & CustRow).Value And DaysSince >= FrDays And DaysSince <= ToDays Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False)
For CustCol = 5 To 26
TagName = .Cells(7, CustCol).Value
TagValue = .Cells(CustRow, CustCol).Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With '***
Next CustCol
If .Range("I3").Value = "PDF" Then
FileName = "Filename" & "\" & .Range("H" & CustRow).Value & " " & .Range("G" & CustRow).Value & " " & .Range("G3").Value & ".pdf"
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else:
FileName = ThisWorkbook.Path & "\" & .Range("H" & CustRow).Value & "_" & .Range("G" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
.Range("Z" & CustRow).Value = TemplName
.Range("AA" & CustRow).Value = Now
If .Range("P3").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = Tabelle1.Range("K" & CustRow).Value
.Subject = "Hallo, " & Tabelle1.Range("F" & CustRow).Value & "Test Test Test"
.Body = "Hallo, " & Tabelle1.Range("F" & CustRow).Value & "Test Test Test Test"
.Attachments.Add FileName
.Display
End With
Else:
WordDoc.PrintOut
WordDoc.Close
End If
Kill False '(FileName)
End If
Next CustRow
WordApp.Quit
'*~ cleanup after finishing
Set WordApp = Nothing
Set OutApp = Nothing
End With
End Sub
'*~
Function GetLastSentTemplate(sTemplate As String) As String
Dim lPrefixNumber As Long
If Len(sTemplate) > 0 Then
lPrefixNumber = Val(Left(sTemplate, InStr(sTemplate, ".") - 1))
If lPrefixNumber > 1 Then
GetLastSentTemplate = Replace(sTemplate, lPrefixNumber, lPrefixNumber - 1)
End If
End If
End Function

Related

How do I remove the %20 from a file name?

I set up my Excel workbook to generate Outlook emails with either a Word or pdf attachment from data entered into a table using VBA.
When I enter the criteria to generate the email with attachment, the attachment name puts "John%20Doe" instead of "John Doe".
How can I get rid of the %20 and have the space between first and last name instead?
Sub CreateWordDocuments()
'CREATE A WORD DOCUMENT TO TRANSFER INFORMATION FROM FILTERED DATA INTO A WORD
TEMPLATE
Dim VSCRow, VSCCol, LastRow, TemplRow, MonthNumber, FromMonth, ToMonth, DaysOfMonth,
FromDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet5
If .Range("B3").Value = Empty Then
MsgBox "Please select the correct template from the drop down list"
.Range("F4").Select
Exit Sub
End If
TemplRow = .Range("B3").Value ' Set the Template Value
TemplName = .Range("F4").Value ' Set Template Name
MonthNumber = .Range("V4").Value 'Set the Month Number
FromMonth = .Range("W4").Value
ToMonth = .Range("Y4").Value
DaysOfMonth = .Range("AA4").Value
FromDays = .Range("AC4").Value
ToDays = .Range("AF4").Value
DocLoc = Sheet10.Range("F" & TemplRow).Value ' Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already open
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
' Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("E99999").End(xlUp).Row 'Determine the last Row
For VSCRow = 8 To LastRow
MonthNumber = .Range("X" & VSCRow).Value
DaysOfMonth = .Range("AF" & VSCRow).Value
If TemplName <> .Range("Z" & VSCRow).Value And MonthNumber >= FromMonth And
MonthNumber <= ToMonth And DaysOfMonth >= FromDays And DaysOfMonth <= ToDays Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) ' Open
Template
For VSCCol = 5 To 42 'Move through the colunms for information
TagName = .Cells(7, VSCCol).Value 'Tag Name
TagValue = .Cells(VSCRow, VSCCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Forward:True, Wrap:=wdFindContinue
End With
Next VSCCol
If .Range("H4").Value = "PDF" Then
FileName = ThisWorkbook.Path & "\" & .Range("E" & VSCRow).Value & ".pdf" '
Create full filename and path with current workbook
WordDoc.ExportAsFixedFormat OutputFileName:=FileName,
ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else:
FileName = ThisWorkbook.Path & "\" & .Range("E" & VSCRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
.Range("Z" & VSCRow).Value = TemplName 'Template Name to use
.Range("AA" & VSCRow).Value = Now
If .Range("S4").Value = "Email" Then
Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
Set OutMail = OutApp.CreateItem(0) 'Create The Email
With OutMail
.To = Sheet5.Range("Y" & VSCRow).Value
.Subject = "Performance Metrics Verification, " & Sheet5.Range("R" & VSCRow).Value & "
- " & Sheet5.Range("S" & VSCRow).Value & ", " & Sheet5.Range("T" & VSCRow).Value
.Body = "Good afternoon, " & Sheet5.Range("E" & VSCRow).Value & ", here are your " &
Sheet5.Range("R" & VSCRow).Value & " - " & Sheet5.Range("S" & VSCRow).Value & ", " &
Sheet5.Range("T" & VSCRow).Value & " performance metrics as captured by the WFW database
systems. Please review and sign. Comments may be included in the email body. Please
return to me by COB " & Sheet5.Range("AG" & VSCRow).Value & ", If this date falls on a
holiday, return on the next business day following the holiday."
.Attachments.Add FileName
.Display 'To send without displaying .Display to .Send
End With
Else
WordDoc.PrintOut
WordDoc.Close
End If
Kill (FileName) 'Deletes the PDF or Word that was just created
End If '3 conditions are met
Next VSCRow
WordApp.Quit
End With
End Sub

Insert adjustable table in Word document from Excel

I will have two situations either:
Otherwise the first cell, will contain more values separated by ";" as follows:
These situations should result in different tables which should be inserted in a pre-existing Word document I open with the VBA from Excel.
The resulting tables are shown below:
I just inserted a "fixed" table in the Word document and replace the inside values, this isn't sufficient anymore.
This is the code I use to open a Word document and replace certain words and save the newly made Word documents as a new file in both docx and pdf format:
Sub Sample()
Const wdFindContinue As Long = 1
Const wdReplaceAll As Long = 2
Const StrNoChr As String = """*./\:?|"
Dim oWordApp As Object, oWordDoc As Object, rngStory As Object
Dim sFolder As String, strFilePattern As String
Dim strFileName As String, sFileName As String
Dim cant As Integer
Dim tex As String
Dim max As Integer
Dim total As Integer
Dim final As Integer
sFolder = "C:\Users\name\folder\"
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWordApp.Visible = False
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Data")
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
For i = 2 To last_row
sFileName = sFolder & "wordfile.docx"
Set oWordDoc = oWordApp.Documents.Open(sFileName)
For Each rngStory In oWordDoc.StoryRanges
With rngStory.Find
If sh.Range("C" & i).Value <> "" Then
.Text = "_Name1"
.Replacement.Text = sh.Range("C" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
If sh.Range("D" & i).Value <> "" Then
.Text = "_Name2"
.Replacement.Text = sh.Range("D" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
End With
Next
StrName = Sheets(1).Cells(i, 2)
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next j
StrName = Trim(StrName)
With oWordDoc
.SaveAs Filename:=sFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
'.SaveAs Filename:=sFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.ExportAsFixedFormat sFolder & StrName & ".pdf", 17
.Close SaveChanges:=False
End With
Next i
oWordApp.Quit
Set oWordDoc = Nothing
Set oWordApp = Nothing
MsgBox "Succes"
End Sub
The code isn't relevant for the specific problem, but may give some inspiration or other ideas.
EDIT:
I tried with this:
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= 4
As suggested by MacroPod, but it doesn't work.
For example, assuming the basic tables are already there and you have code to populate the rows with the pre-processed data:
Sub Demo()
Dim oWdApp As Object, oWdDoc As Object, oWdRng As Object, oWdTbl As Object
Dim sFolder As String, sFileName As String, StrTxt As String
Dim last_row As Long, r As Long, c As Long, i As Long, j As Long
Const wdFindContinue As Long = 1: Const wdReplaceAll As Long = 2
Const wdFormatXMLDocument As Long = 12: Const wdFormatPDF As Long = 17
Const StrNoChr As String = """*./\:?|"
sFolder = "C:\Users\name\folder\"
Dim sh As Worksheet: Set sh = ThisWorkbook.Sheets("Data")
last_row = Application.WorksheetFunction.CountA(sh.Range("A:A"))
On Error Resume Next
Set oWdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set oWdApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0
oWdApp.Visible = False
For i = 2 To last_row
sFileName = sFolder & "wordfile.docx"
Set oWdDoc = oWdApp.Documents.Add(sFileName)
With oWdDoc
For Each oWdRng In .StoryRanges
With oWdRng.Find
If sh.Range("C" & i).Value <> "" Then
.Text = "_Name1"
.Replacement.Text = sh.Range("C" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
If sh.Range("D" & i).Value <> "" Then
.Text = "_Name2"
.Replacement.Text = sh.Range("D" & i).Value
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End If
End With
Next
For Each oWdTbl In .Tables
With oWdTbl
For r = .Rows.Count To 2 Step -1
For c = 1 To .Rows(r).Cells.Count Step 2
StrTxt = Split(.Cell(r, c).Range.Text, vbCr)(0)
If InStr(StrTxt, ";") > 0 Then
For j = 1 To UBound(Split(StrTxt, ";"))
If r = .Rows.Count Then
.Rows.Add
Else
.Rows.Add .Rows(r + 1)
End If
.Cell(r + j, c).Range.Text = Split(Trim(Split(StrTxt, ";")(j)), " ")(0)
.Cell(r + j, c + 1).Range.Text = Replace(Replace(Split(Trim(Split(StrTxt, ";")(j)), " ")(1), ")", ""), "(", "")
Next
End If
If InStr(StrTxt, " ") > 0 Then
.Cell(r, c).Range.Text = Split(Trim(Split(StrTxt, ";")(0)), " ")(0)
.Cell(r, c + 1).Range.Text = Replace(Replace(Split(Trim(Split(StrTxt, ";")(0)), " ")(1), ")", ""), "(", "")
End If
Next
Next
End With
Next
StrName = Sheets(1).Cells(i, 2).Text
For j = 1 To Len(StrNoChr)
StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
Next j
StrName = Trim(StrName)
.SaveAs Filename:=sFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
.SaveAs Filename:=sFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=False
End With
Next i
oWdApp.Quit
Set oWordDoc = Nothing: Set oWdApp = Nothing: Set oWdRng = Nothing: Set oWdTbl = Nothing: Set sh = Nothing
MsgBox "Succes"
End Sub

Inserting an image on in an email with ActiveWorkbook.Path

I have Excel VBA code in my spreadsheet that takes a list of names and email addresses, creates a PowerPoint certificate and emails each person their certificate.
I can add a logo to the end of the email if I give it a specific path such as
C:\Users\User\Desktop\Folder\img.png
but if I say
ActiveWorkbook.Path & '\img.png'
it inserts an empty box.
Public Function generateCerts()
Dim CurrentFolder As String
Dim fileName As String
Dim myPath As String
Dim UniqueName As Boolean
Dim sh As Worksheet
Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application
Dim shp As PowerPoint.Shape
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application
Set PowerPointApp = CreateObject("PowerPoint.Application")
Set myPresentation = PowerPointApp.Presentations.Open(ActiveWorkbook.Path & "\Certificate3.pptx")
Set shp = myPresentation.Slides(1).Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=250, Width:=825, Height:=68)
shp.TextFrame.TextRange.Font.Size = 36
shp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
Set sh = Sheets("CertNames")
For Each rw In sh.Rows
If rw.Row > 1 Then
If sh.Cells(rw.Row, 1).Value = "" Then
Exit For
End If
shp.TextFrame.TextRange.Text = sh.Cells(rw.Row, 1) & " " & sh.Cells(rw.Row, 2).Value & " " & sh.Cells(rw.Row, 3).Value
fileName = ActiveWorkbook.Path & "\" & sh.Cells(rw.Row, 2).Value & " " & sh.Cells(rw.Row, 3).Value & " " & rw.Row & ".pdf"
myPresentation.ExportAsFixedFormat fileName, _
ppFixedFormatTypePDF, ppFixedFormatIntentPrint, msoCTrue, ppPrintHandoutHorizontalFirst, _
ppPrintOutputSlides, msoFalse, , ppPrintAll, , False, False, False, False, False
Set myMail = outlookApp.CreateItem(olMailItem)
myMail.Attachments.Add fileName
myMail.SentOnBehalfOfName = ""
' myMail.BCC = ""
myMail.To = sh.Cells(rw.Row, 4).Value
myMail.Subject = "Thank you for attending"
myMail.HTMLBody = "Hello" & " " & sh.Cells(rw.Row, 1).Value & ","
myMail.HTMLBody = myMail.HTMLBody & "<p>Thank you for participating in <b><i>Session 7
myMail.HTMLBody = myMail.HTMLBody & "<p>Support</p>"
myMail.HTMLBody = myMail.HTMLBody & "<img src='ActiveWorkbook.Path & '\img.png''"
myMail.Display
' myMail.Send
End If
Next rw
myPresentation.Saved = True
PowerPointApp.Quit
End Function
Declare the path as a string so you can inspect it:
Dim imagePath As String
imagePath = ActiveWorkbook.Path & "\img.png"
Now that you know it's correct, use it like this:
myMail.HTMLBody = myMail.HTMLBody & "<img src='" & imagePath & "'>"

Export row to PDF/ Word using cell number selected in a dropdown

I am using the code below to export a row in an Excel sheet into a Word/pdf file.
It is downloading all non-blank rows.
I want when I select a reference number in a cell dropdown ("CA2"), it only downloads that selected row.
Sub Download_Click()
Dim CustRow, CustCol, LastRow, TemplRow, Reference, RefRow As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet3
TemplRow = .Range("CI1").Value
TemplName = .Range("BV2").Value
Reference = .Range("CA2").Value
DocLoc = Sheet3.Range("CG2").Value
On Error Resume Next
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
End If
LastRow = .Range("A9999").End(xlUp).Row
For CustRow = 3 To LastRow
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False)
For CustCol = 1 To 70
TagName = .Cells(2, CustCol).Value
TagValue = .Cells(CustRow, CustCol).Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next CustCol
If .Range("BX2").Value = "PDF" Then
FileName = ThisWorkbook.Path & "\" & .Range("B" & CustRow).Value & "_" & .Range("C" & CustRow).Value & ".pdf"
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else
FileName = ThisWorkbook.Path & "\" & .Range("B" & CustRow).Value & "_" & .Range("C" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
Next CustRow
WordApp.Quit
End With
End Sub
As I understand now, your code will export all rows from row 3 to the last row:
For CustRow = 3 To LastRow
If you want only a selected row to be exported, then all you need to do is remove that loop and replace it with a single value, so it only runs once for that value. Remove the above line and replace with this:
CustRow = .Range("CA2").Value
Make sure to remove Next CustRow. Also make sure that cell CA2 contains a numerical value which is a direct reference to the row you need.

Fill in Word document from a excel spreadsheet with the ability to select which row(s) to be used to generate the doc

I'm struggling for the last 9 hrs trying to create a process that will allow me to auto fill in a Word doc(Template) with information from a excel spreadsheet. I've mapped the word doc to best of my ability using <<>> to match the needed infor from the spreadsheet. But when the Macro is ran it continues the replicate only the some of the data in the first row and the only way to stop the replication is to stop word from the "task manager". Also, I can't figure how to select just the row(s) that I would like to be used to generate the word doc...I'm burnt out at this point.
Please help me.
This is the VBA I'm working with.
Sub CreateWordDocuments()
Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet1
If .Range("B3").Value = Empty Then
MsgBox "Please select a correct template from the drop down list"
.Range("G3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value 'Set Template Row
TemplName = .Range("G3").Value 'Set Template Name
DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("E9999").End(xlUp).Row 'Determine Last Row in Table
For CustRow = 8 To LastRow
DaysSince = .Range("M" & CustRow).Value
If TemplName <> .Range("N" & CustRow).Value And Row >= FrDays And DaysSince <= ToDays Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
For CustCol = 5 To 13 'Move Through 9 Columns
TagName = .Cells(7, CustCol).Value 'Tag Name
TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With
Next CustCol
If .Range("I3").Value = "PDF" Then
FileName = ThisWorkbook.Path & "\" & .Range("E" & CustRow).Value & "_" & .Range("F" & CustRow).Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
WordDoc.Close False
Else: 'If Word
FileName = ThisWorkbook.Path & "\" & .Range("E" & CustRow).Value & "_" & .Range("F" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
End If
.Range("V" & CustRow).Value = TemplName 'Template Name
.Range("W" & CustRow).Value = Now
WordDoc.Close
End If '3 condition met
Next CustRow
WordApp.Quit
End With
End Sub```

Resources