Here I'm looking for an applicable approach...
How can send the running VBA procedures to background for prevent workbook hangs until the VBA statements running are finished.
Hangs when running a VBA procedure...
Point that the Document.PrintOut built-in procedure has Background argument which have the macro continue while Microsoft Word prints the document.
The Excel Hangs while running .Send procedure of below snip code
Dim iMsg As Object
Dim iConf As Object
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
With iMsg
Set .Configuration = iConf
.To = strTo
.ReplyTo = "Email Address"
.From = """Sender Name (Email Address)"" <EmailAddress>"
.Subject = strSubject
.TextBody = strBody
.AddAttachment ThisWorkbook.Path & "\Temp\" & ThisWorkbook.Name
.Send
End With
Excel Hangs while For Next loop in below code:
Sub PrintIt()
Dim objWord As Word.Application
Dim objDocTotal As Word.Document
Dim objDoc As Word.Document
Dim i As Integer
Dim strOutfile As String
Dim rg As Word.Range
ActiveSheet.OLEObjects("SalaryPaycheck").Activate
Set objWord = GetObject(, "Word.Application")
objWord.Visible = False
Set objDoc = objWord.ActiveDocument
Set objDocTotal = Documents.Add
objWord.Application.DisplayAlerts = wdAlertsNone
objWord.Application.ScreenUpdating = False
For i = worksheetfunction.Min(range("Table1[Column1]") To _
worksheetfunction.Max(range("Table1[Column1]")
Range("Key").Value = i
With objDoc
.Fields.Update
.Content.Copy
End With
Set rg = objDocTotal.Content
With rg
.Collapse Direction:=wdCollapseEnd
If i > 1 Then .InsertBreak wdPageBreak
.PasteAndFormat wdFormatOriginalFormatting
End With
Next i
strOutfile = "<Path>\Salary.pdf"
objDocTotal.ExportAsFixedFormat outputfileName:= _
strOutfile, exportformat:=wdExportFormatPDF, _
openafterexport:=False, optimizefor:=wdExportOptimizeForPrint, Range:= _
wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent
objDocTotal.Close False
objWord.Quit
Set objDoc = Nothing
Set objWord = Nothing
End Sub
As in above cases the average of CPU Utilization is approximately lower than 40%!
And we know from Excel 2007 the multi-threaded is supported (support.microsoft.)
Why Excel hangs when running some codes (as exampled above) while the codes running, up to they have terminated or end?
How can prevent Workbooks hanging in above same as the
Background's argument ability forPrintOutprocedure in MS-Word which said at top of here?
Regards.
Backgroundargument ofPrintOutprocedure is enabled and running other procedures. Here we not focused on Multi threaded approach. Please read the question with more precision. Regards. - TuberoseiMsg.Send) is finished processing, because VBA is not multi-threaded, and Excel itself can't do any processing while the VBA code is running. I think you are confusing Excel being multi-threaded with VBA being multi-threaded. - YowE3KDoEventsstatements to your code) you run the risk of changes being made to the Excel workbook that your code is not expecting. It is a bad idea. But your VBA code cannot yield control within a single statement, only between statements, because VBA is not multi-thread enabled. - YowE3K