Hello, I've done a macro but everytime that works it takes too long, probably I've been making poor pratices, can someone help? thanks
The purpose it's to generate a word template. Example: If I have two rows in the excel it should create two different letters (creating a loop)... For 2 letters is taking 3min
The purpose it's to generate a word template. Example: If I have two rows in the excel it should create two different letters (creating a loop)... For 2 letters is taking 3min
VBA Code:
'PRIME DI_LetterCreation
Sub NAMEOFTHEPROJECT_LetterCreation()
Dim oPara3 As Word.Paragraph
Dim oDoc As Word.Document
For i = 2 To Sheet1.Range("A65536").End(xlUp).Row
'Checking for the uprocessed letter referance to colum where status of completion gets updated
'AL represent colum where status to be updated
If Sheet1.Range("E" & i) <> "" Then
GoTo Next_Letter
End If
Set WordApp = CreateObject("word.Application")
'Opens the Letter Template- AQ represent the cell referance where doc template is saved
WordApp.Documents.Open ("C:\Users\z0037tnu\Desktop\MACRO\Lettre Prime DI.docx")
WordApp.Visible = True
'update cell referance where doc template is saved
Set oDoc = GetObject(ThisWorkbook.Sheets("DataSheet").Range("J1").Value)
'Set oDoc = GetObject("C:\Users\z0037tnu\Desktop\MACRO\Lettre Prime DI.docx")
With oDoc
' copy the below formula basis
'number of bookmarked fileds in template
'For Name of bookmark referance and data cell to read
If oDoc.Bookmarks.Exists("Nom") Then
oDoc.Bookmarks("Nom").Range.Text = Trim(Sheet1.Range("A" & i))
End If
'For Name of bookmark referance and data cell to read
If oDoc.Bookmarks.Exists("Adresse") Then
oDoc.Bookmarks("Adresse").Range.Text = Trim(Sheet1.Range("B" & i))
End If
'For Name of bookmark referance and data cell to read
If oDoc.Bookmarks.Exists("Civilite") Then
oDoc.Bookmarks("Civilite").Range.Text = Trim(Sheet1.Range("C" & i))
End If
'For Name of bookmark referance and data cell to read
If oDoc.Bookmarks.Exists("Civilite2") Then
oDoc.Bookmarks("Civilite2").Range.Text = Trim(Sheet1.Range("C" & i))
End If
'For Name of bookmark referance and data cell to read
If oDoc.Bookmarks.Exists("Amount") Then
oDoc.Bookmarks("Amount").Range.Text = Trim(Sheet1.Range("D" & i))
End If
'example for date fileds can be mapped to bookmark as needed
'date formating
D1 = Format((Trim(Sheet1.Range("F" & i))), "dd/mm/yyyy")
'copy data to word
If oDoc.Bookmarks.Exists("Date") Then
oDoc.Bookmarks("Date").Range.Text = D1
End If
End With
'SaveAs the letter- update cell referance where its mentioned where to save and also file name to be used
'AQ2 represent cell where Updated DocPath - To Save Letter is stored
'AM represent the letter file name colum you wnat to pickup
oDoc.SaveAs ThisWorkbook.Sheets("DataSheet").Range("J2").Value & (Sheet1.Range("G" & i)) & ".docx"
' Closes the document and Quits the application
With oDoc
.Close
End With
With WordApp
.Quit
End With
Set oDoc = Nothing
Set WordApp = Nothing
' update the cell name (AL) where you want to update status once letter is completed
Sheet1.Range("E" & i) = "Completed"
Next_Letter:
Next i
' Type custom message you want to be reflected once action complete
MsgBox "Prime DI_LetterCreation - COMPLETED!!!", vbInformation, "Graduate Letter Gen"
End Sub
Last edited by a moderator: