Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub CreateWordDocuments()
  4. Dim CustRow, CustCol, LastRow, TemplRow, DaysSince, FrDays, ToDays As Long
  5. Dim DocLoc, TagName, TagValue, TemplName, FileName As String
  6. Dim CurDt, LastAppDt As Date
  7. Dim WordDoc, WordApp, OutApp, OutMail As Object
  8. Dim WordContent As Word.Range
  9.  
  10. With Sheets("QA Selection")
  11. If .Range("B3").Value = Empty Then
  12. MsgBox "Please select a correct template from the drop down list"
  13. .Range("G3").Select
  14. Exit Sub
  15. End If
  16.  
  17. TemplRow = .Range("B3").Value 'Set Template Row
  18. TemplName = .Range("G3").Value 'Set Template Name
  19. FrDays = .Range("K4").Value 'Set From Days
  20. ToDays = .Range("M4").Value 'Set To Days
  21. DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
  22.  
  23. 'Open Word Template
  24. On Error Resume Next 'If Word is already running
  25. Set WordApp = GetObject("Word.Application")
  26. If Err.Number <> 0 Then
  27. 'Launch a new instance of Word
  28. Err.Clear
  29. 'On Error GoTo Erro_Handler
  30. Set WordApp = CreateObject("Word.Application")
  31. WordApp.Visible = True 'Make the application visible to the user
  32. End If
  33.  
  34. LastRow = .Range("D99999").End(xlUp).Row 'Determine Last Row in Table
  35. For CustRow = 7 To LastRow
  36. DaysSince = .Range("I" & CustRow).Value
  37. If DaysSince <= FrDays And DaysSince >= ToDays Then
  38. Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc)
  39. 'Open Template
  40. For CustCol = 4 To 16 'Move through 11 columns
  41. TagName = .Cells(5, CustCol).Value 'TagName
  42. TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
  43. With WordDoc.Content.Find
  44. .Text = TagName
  45. .Replacement.Text = TagValue
  46. .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 'Find and Replace all Instances
  47. End With
  48. Next CustCol
  49.  
  50. If .Range("I3").Value = "PDF" Then
  51. FileName = ThisWorkbook.Path & "" & .Range("D" & CustRow).Value & "_" & .Range("H" & CustRow).Value & ".pdf" 'Create full file name & Path with current workbook locations, Full Name and date
  52. WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
  53. WordDoc.Close False
  54.  
  55. Else: 'If Word
  56. FileName = ThisWorkbook.Path & "" & .Range("D" & CustRow).Value & "_" & .Range("H" & CustRow).Value & ".docx"
  57. WordDoc.SaveAs FileName
  58. End If
  59. If .Range("O3").Value = "Email" Then
  60. Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
  61. Set OutMail = OutApp.CreateItem(0) 'Create Email
  62. With OutMail
  63. .To = Sheets("QA Selection").Range("P" & CustRow).Value
  64. .Subject = "Side by Side for " & Sheets("QA Selection").Range("D" & CustRow).Value & Sheets("QA Selection").Range("H" & CustRow).Value
  65. .Body = Sheets("QA Selecetion").Range("F" & CustRow).Value & " It was great sitting with you on " & Sheets("QA Selection").Range("H" & CustRow).Value & "! I wanted to go over a few things I noticed. You can find some Areas of Opportunity and where you succeed in the attached document! Please let me know if you have any questions, " & Sheets("QA Selection").Range("E" & CustRow).Value
  66. .Attachments.Add FileName
  67. .Display 'To Send without Displaying change .Display to .Send
  68. End With
  69.  
  70. Else: 'Print Out
  71. WordDoc.PrintOut
  72. WordDoc.Close
  73. End If
  74. End If
  75. Next CustRow
  76. WordApp.Quit
  77. End With
  78. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement