Guest User

Untitled

a guest
Jun 22nd, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. Sub CreateWordDocTest()
  2.  
  3. Dim wApp As Word.Application
  4. Dim wDoc As Word.Document
  5. Dim myStoryRange As Word.Range
  6. Dim InsName, InsNumber, CurrentYear, Industry, AnalysisToolPath, AnalysisToolName, FileNameFragment2, TodaysDate, TemplatePath As String
  7.  
  8. If RADType = "Full" Then
  9. TemplatePath = Sheets("Metadata").Range("D8").Value
  10. NotificationWhenDone = "Full RAD done"
  11. TodaysDate = Now() 'Variable called TodaysDate would now contain the current system date and time
  12. Else
  13. TemplatePath = Sheets("Metadata").Range("D6").Value
  14. NotificationWhenDone = "Summary RAD done"
  15. TodaysDate = Now()
  16. End If
  17.  
  18. Set wApp = CreateObject("Word.Application")
  19. wApp.Visible = True 'Creates an instance of Word an makes it visible
  20. Set wDoc = wApp.Documents.Open(TemplatePath, False) 'Opens the chosen full or summary RAD template
  21.  
  22. With wDoc 'Use the With statement to not repeat wDoc many times
  23.  
  24. 'Start at the beginning of the Word document
  25.  
  26. .Application.Selection.HomeKey Unit:=wdStory 'Moves the selection to the beginning of the current story
  27.  
  28. InsName = Sheets("Parameters").Range("D4").Value
  29. InsNumber = Sheets("Parameters").Range("D5").Value
  30. CurrentYear = Sheets("Parameters").Range("D6").Value
  31. Industry = Sheets("Parameters").Range("D7").Value
  32. AnalysisToolPath = Sheets("Metadata").Range("D2").Value
  33. FileNameFragment2 = InsNumber & " - " & InsName & " " & CurrentYear & ".xlsm"
  34. AnalysisToolName = AnalysisToolPath & FileNameFragment2
  35.  
  36. 'Write insurer name
  37.  
  38. For Each myStoryRange In ActiveDocument.StoryRanges
  39. With myStoryRange.Find
  40. .Text = "<<InsurerName>>" 'Find the exact text in the Word document
  41. .Replacement.Text = InsName 'Replace this text with the insurername as defined
  42. .Wrap = wdFindContinue 'The find operation continues when the beginning or end of the search range is reached
  43. .Execute Replace:=wdReplaceAll 'Finds all occurences and executes the replacement
  44. End With
  45.  
  46. Next myStoryRange
  47. .Application.Selection.EndOf 'Selects until the end of the document
  48.  
  49. 'Write insurer class
  50. For Each myStoryRange In ActiveDocument.StoryRanges
  51. With myStoryRange.Find
  52. .Text = "<<InsurerClass>>"
  53. .Replacement.Text = Industry
  54. .Wrap = wdFindContinue
  55. .Execute Replace:=wdReplaceAll
  56. End With
  57.  
  58. Next myStoryRange
  59. .Application.Selection.EndOf
  60.  
  61. 'Write financial year
  62. For Each myStoryRange In ActiveDocument.StoryRanges
  63. With myStoryRange.Find
  64. .Text = "<<CurrentYear>>"
  65. .Replacement.Text = CurrentYear
  66. .Wrap = wdFindContinue
  67. .Execute Replace:=wdReplaceAll
  68. End With
  69.  
  70. Next myStoryRange
  71. .Application.Selection.EndOf
  72.  
  73. 'Write significant classes
  74. For Each myStoryRange In ActiveDocument.StoryRanges
  75. With myStoryRange.Find
  76. .Text = "<<SignificantClasses>>"
  77. .Replacement.Text = SignificantclassesTxt
  78. .Wrap = wdFindContinue
  79. .Execute Replace:=wdReplaceAll
  80. End With
  81. Next myStoryRange
  82. .Application.Selection.EndOf
  83.  
  84. 'Write insurer number
  85. .Application.Selection.Find.Text = "<<InsurerNumber>>"
  86. .Application.Selection.Find.Execute
  87. .Application.Selection = Sheets("Parameters").Range("D5").Value
  88. .Application.Selection.EndOf
  89.  
  90. 'Write analyst name
  91. .Application.Selection.Find.Text = "<<AnalystName>>"
  92. .Application.Selection.Find.Execute
  93. .Application.Selection = UserFullName
  94. .Application.Selection.EndOf
  95.  
  96. 'Write RiBS Wording
  97. .Application.Selection.Find.Text = "<<RiBSWording>>"
  98. .Application.Selection.Find.Execute
  99. .Application.Selection = SignificantclassesRiBSTxt
  100. .Application.Selection.EndOf
  101.  
  102. End With
  103.  
  104. End Sub
Add Comment
Please, Sign In to add comment