Sub my_Dynamic_Document_Contract_Generation()
Dim bCreatedWordInstance As Boolean
Dim wdApp As Word.Application
Dim wdSC_Generation As Word.Document
Dim sDir As String
Dim sNewFileName As String
Dim wdDoc As Word.Document
Dim strName As String
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim loTable As ListObject
Dim sJobNo As String
Dim sPath As String
Dim wdbmRange As Word.Range
Dim sBookmark As String
'Stores the Project number
sJobNo = ThisWorkbook.Worksheets(gsSC).Range("H1")
Call myInit
' Setup directory
sDir = gsDRIVE + gsDIR_DOC_TEMPLATE
On Error Resume Next
' Create a Word Application instance
bCreatedWordInstance = False
Set wdApp = GetObject(, "Word.Application")
If wdApp Is Nothing Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
bCreatedWordInstance = True
End If
If wdApp Is Nothing Then
MsgBox "Could not start Word"
Err.Clear
On Error GoTo 0
Exit Sub
End If
'Let Word trap the errors
On Error GoTo 0
' Set to True if you want to see the Word Doc flash past during construction
wdApp.Visible = False
'Open Word Template
Set wdSC_Generation = wdApp.Documents.Open(sDir + gsWTempl_SC_Generation)
wdSC_Generation.Activate
'"\\etsi\fileservices\Units\RES\FPS\CONTRACTS\NAV_Documents\Final_versions\MOCKUP_SC_TEMPLATE.dotx"
Set wdDoc = wdApp.Documents.Add(wdSC_Generation.FullName)
'Insert Sentences
Call Insert_at_Bookmark("Head1", wdDoc)
Call Insert_at_Bookmark("Head2", wdDoc)
Call Insert_at_Bookmark("Date", wdDoc)
Call Insert_at_Bookmark("Definition", wdDoc)
'insert Type "Plugtests" before Service Contract when needed
If ThisWorkbook.Worksheets("SC_Active").Range("D2").Value Like "Plugtests Service Contract - Lump sum" Then
Call Insert_at_Bookmark("Type", wdDoc)
End If
Call Insert_at_Bookmark("Ref", wdDoc)
Call Insert_at_Bookmark("Ref_2", wdDoc)
Call Insert_at_Bookmark("Title", wdDoc)
Call Insert_at_Bookmark("Vendor_Presentation", wdDoc)
Call Insert_at_Bookmark("Vendor_Registration", wdDoc)
Call Insert_at_Bookmark("Vendor_Headquarter", wdDoc)
Call Insert_at_Bookmark("Vendor_Signatory", wdDoc)
Call Insert_at_Bookmark("Role", wdDoc)
Call Insert_at_Bookmark("Presentation1", wdDoc)
Call Insert_at_Bookmark("Presentation2", wdDoc)
Call Insert_at_Bookmark("Purpose", wdDoc)
Call Insert_at_Bookmark("Contract_Start", wdDoc)
Call Insert_at_Bookmark("Contract_End", wdDoc)
Call Insert_at_Bookmark("Directives", wdDoc)
Call Insert_at_Bookmark("Insurance", wdDoc)
Call Insert_at_Bookmark("Compliance", wdDoc)
Call Insert_at_Bookmark("Contract_Financial", wdDoc)
Call Insert_at_Bookmark("Contract_Travel", wdDoc)
Call Insert_at_Bookmark("Invoicing", wdDoc)
'insert support index when supported
If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value Like "None" Then
Call Insert_at_Bookmark("Index_Annexes", wdDoc)
Else
Call Insert_at_Bookmark("Index_Support", wdDoc)
End If
Call Insert_at_Bookmark("Bank_Account", wdDoc)
Call Insert_at_Bookmark("Confidentiality", wdDoc)
Call Insert_at_Bookmark("Owner", wdDoc)
Call Insert_at_Bookmark("Use", wdDoc)
Call Insert_at_Bookmark("Annex_1", wdDoc)
Call Insert_at_Bookmark("Allocation", wdDoc)
Call Insert_at_Bookmark("Manager_Title", wdDoc)
Call Insert_at_Bookmark("Tech_Contact_ETSI", wdDoc)
Call Insert_at_Bookmark("Admin_Contact_ETSI", wdDoc)
Call Insert_at_Bookmark("Contract_PM_Name", wdDoc)
Call Insert_at_Bookmark("Contract_PM_Role", wdDoc)
Call Insert_at_Bookmark("Contract_PM_Email", wdDoc)
Call Insert_at_Bookmark("Contract_PM_Tel", wdDoc)
'Call Insert_at_Bookmark("Contract_PM_Mobile", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Name", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Role", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Email", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Tel", wdDoc)
'Call Insert_at_Bookmark("Contract_Admin_Mobile", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Address", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_City", wdDoc)
Call Insert_at_Bookmark("Contract_Admin_Country", wdDoc)
'Select pages 3 to 9
Dim rgePages As Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Select
'Dim rgePages As Range
Selection.wdDoc.Bookmark(Presentation1).Range
Set rgePages = Selection.Range
Selection.wdDoc.Bookmark(Use).Range
rgePages.End (Selection.Bookmarks("\Page").Range.End)
rgePages.Select
'Justitfy pages 3 to 9
Selection.Paragraphs.Alignment = 3
Selection.Collapse
'Set rgePages = wdDoc.Range(Start:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3), End:=wdDoc.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9))
'wdDoc.PageSetup(8).Range.Select
'ActiveDocument.PageSetup
'insert page breaks before support annexe only when supported
If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value <> "None" Then
wdDoc.Bookmarks("NextPage1").Range.InsertBreak Type:=wdPageBreak
End If
'insert support title only when supported
If ThisWorkbook.Worksheets("_TABLES").Range("AH10").Value <> "None" Then
Call Insert_at_Bookmark("Support", wdDoc)
End If
'Initialize table Milestones
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("_TABLES")
Set loTable = wsSheet.ListObjects("IG_TABLE_MIL_BY_CONTRACT")
sBookmark = "Table_Milestones"
'Copy Paste Table
Call Export_Table_Word(wdDoc, loTable, "*", sBookmark)
'insert ToR
Call insert_Docs("TOR", wdDoc, "Contract")
' Close without saving the Template Document
wdSC_Generation.Close savechanges:=wdDoNotSaveChanges
Set wdSC_Generation = Nothing
'Define the name of the file to be saved
With ThisWorkbook.Worksheets("_TABLES").ListObjects("IG_TABLE_CONTRACT_INFO")
strName = .DataBodyRange.Cells(1, .ListColumns("Contract_No").Index) & "_"
strName = Replace(strName, "/", "_")
strName = strName & .DataBodyRange.Cells(1, .ListColumns("Contract_Version").Index) & "_"
strName = strName & .DataBodyRange.Cells(1, .ListColumns("Contract_Vendor_Name").Index)
strName = Replace(strName, ".", "")
strName = Replace(strName, "/", " ")
End With
If Len(Dir(gsDRIVE & gsDIR_WORD_TEMPLATE & strName & ".docx")) = 0 Then
wdDoc.SaveAs2 gsDRIVE & gsDIR_WORD_TEMPLATE & strName
Else
wdDoc.SaveAs2 gsDRIVE & gsDIR_WORD_TEMPLATE & strName & "_" & Format(Now, "dd-mm-yy hh mm ss")
End If
Application.StatusBar = ""
MsgBox "Document Generated & Saved!"
Application.StatusBar = ""
0:
Set wdApp = Nothing
End Sub