srikanth sare
New Member
- Joined
- May 1, 2020
- Messages
- 30
- Office Version
- 2013
- Platform
- Windows
- MacOS
- Mobile
- Web
Hi
The Below code is working perfectly. However sometimes the macros are disabled.
I tried adding the location in trust center it is not accepting to add the specified location
It is actually occuring due to Temporary file of Word Document.
And the macro is taking more time to run is there any way to reduce
Application.run TurnOff And on will do the Calculation, Scrren updating, events and display alerts
The Below code is working perfectly. However sometimes the macros are disabled.
I tried adding the location in trust center it is not accepting to add the specified location
It is actually occuring due to Temporary file of Word Document.
And the macro is taking more time to run is there any way to reduce
Application.run TurnOff And on will do the Calculation, Scrren updating, events and display alerts
VBA Code:
Option Explicit
Sub SendReminders()
Dim lastRow, CustRow, CustCol, TempRow, DaysSince, FrDays, ToDays As Long
Dim TempRng, FoundTempRng As Range
Dim ColFormat, NEWFLDR, MyPath, myFile, DocLoc, VarFormat, VarName, VarValue, TempName, FileName, Subj, Mess, username, Propath As String
Dim WordDoc, WordApp, OutApp, OutMail, FileSystem, File As Object
Dim WordContent As Word.Range
username = Environ("Username")
If username = "sri CA nth Sare" Then
Propath = "J:\SARVAHITHA OFFICE\SARVAHITHA OFFICE\DEFAULT NOTICE\"
ElseIf username = "SARVAHITA" Then
Propath = "F:\DRIVE\DEFAULT NOTICE\"
Else
MsgBox "Invalid username."
Exit Sub
End If
With Sheet1
Application.Run "TurnOff"
FrDays = .Range("L3").Value ' Set From Days
ToDays = .Range("N3").Value ' Set To Days
On Error Resume Next ' If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
Err.Clear
Set WordApp = CreateObject("Word.Application")
End If ' Work Running Check
WordApp.Visible = False ' Make the application visible to the user
NEWFLDR = ThisWorkbook.Path & "\" & "DEFAULT" & Format(Date, " DD-MM-YYYY")
If Dir(NEWFLDR, vbDirectory) = "" Then
MkDir NEWFLDR
End If
lastRow = .Range("D9999").End(xlUp).Row
For CustRow = 10 To lastRow
If Sheet1.Range("F3").Value = "NO" Then
If .Range("C" & CustRow).Value = "ü" Then
R:
DaysSince = .Range("M" & CustRow).Value
TempName = .Range("Y" & CustRow).Value ' Template name
Set TempRng = Sheet2.Range("LetterTemplates")
Set FoundTempRng = TempRng.Find(TempName, , xlValues, xlWhole)
If Not FoundTempRng Is Nothing Then ' Template Found
TempRow = FoundTempRng.Row
DocLoc = Propath & TempName 'Sheet2.Range("F" & TempRow).Value ' Word Document Filename
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) ' Open Template
If Sheet1.Range("F3").Value = "YES" Then
If DaysSince >= FrDays And DaysSince <= ToDays Then
S:
For CustCol = 4 To 24 ' Move Through 9 Columns
If .Cells(7, CustCol).Value = "General" Then
VarFormat = "General"
Else
VarFormat = .Cells(8, CustCol).Value ' Determine Variable Format
End If
VarName = .Cells(9, CustCol).Value ' Determine Variable Name
VarValue = .Cells(CustRow, CustCol).Value ' Determine Variable Value
With WordDoc.Content.Find
.Text = VarName
.Replacement.Text = Application.WorksheetFunction.Text(VarValue, VarFormat)
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll ' Find & Replace all instances
End With
Next CustCol
FileName = NEWFLDR & "\" & .Range("G" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
WordDoc.Close False
.Range("Z" & CustRow).Value = TempName ' Template Name
.Range("AA" & CustRow).Value = Now
End If
Else
GoTo S
End If
End If
End If
Else
GoTo R
End If
Next CustRow
WordApp.Quit
End With
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = False
Set WordDoc = WordApp.Documents.Open(Propath & "DUMMY.docx", ReadOnly:=False)
With WordDoc.PageSetup
.TextColumns.SetCount NumColumns:=1
.TextColumns.EvenlySpaced = True
.TextColumns.LineBetween = False
.Orientation = wdOrientPortrait
.TopMargin = WordDoc.CentimetersToPoints(1.5)
.BottomMargin = WordDoc.CentimetersToPoints(1.5)
.LeftMargin = WordDoc.CentimetersToPoints(2.7)
.RightMargin = WordDoc.CentimetersToPoints(2.7)
End With
MyPath = NEWFLDR & "\"
myFile = Dir(MyPath & "*.docx", vbNormal + vbReadOnly + vbHidden)
While myFile <> ""
With WordDoc.Bookmarks("\ENDOfDoc").Range
.InsertFile FileName:=MyPath & myFile, ConfirmConversions:=False, Link:=False, Attachment:=False
.InsertBreak Type:=wdSectionBreakNextPage 'wdSectionBreakNextPage = 2
End With
myFile = Dir()
Wend
WordDoc.Characters(1).Delete
FileName = MyPath & "MERGED " & Format(Now, "DD-MM-YYYY hh.mm.ss") & ".pdf" ' Create full filename & Path with current workbook location, Last Name & First Name
With WordDoc
.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
.SaveAs MyPath & "MERGED " & Format(Now, "DD-MM-YYYY hh.mm.ss") & ".docx"
.Close True
.Quit
End With
Set FileSystem = CreateObject("Scripting.FileSystemObject")
For Each File In FileSystem.GetFolder(MyPath).Files
If LCase(FileSystem.GetExtensionName(File)) = "docx" And Not InStr(1, File.Name, "MERGED", vbTextCompare) > 0 Then
File.Delete
End If
Next File
Set WordDoc = Nothing
Set WordApp = Nothing
Set FileSystem = Nothing
Application.Run "TurnOn"
End Sub