BAD CODE HIGH CPU USAGE? VBA

core355

New Member
Joined
May 2, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello! I'm stuck with this for several weeks and that's why I'm asking for your help.

This code interacts with a third party application, sending values and printing documents from it.

It works fantastic, only for the small detail that with the passing of the iterations the cpu fan starts to make noise. Surely I am applying a bad practice in some line of the code.

Helppppp!

p/d: Sorry for my english

For Each vCelda In ThisWorkbook.Sheets("OP_Print").Range("op_print[Nº DOC.]").SpecialCells(xlCellTypeVisible)
'ASIGNACION
vSoc = Cells(vCelda.Row, Range("op_print[SOC]").Column)
vOPNUM = vCelda
vAÑO = Format(vCelda.Offset(0, 1), "yyyy")
vTre = "-" & UCase(Cells(vCelda.Row, Range("op_print[TRE]").Column))
vFilename = UCase(vFullPath & "\" & vSoc & "-" & vOPNUM & "-" & vAÑO & vTre)

'DESCARGA
If (FSO.FileExists(vFullPath & "\" & vSoc & "-" & vOPNUM & "-" & vAÑO & vTre & ".pdf") = False) Or (FSO.FileExists(vFullPath & "\" & vSoc & "-" & vOPNUM & "-" & vAÑO & vTre & "-RET.pdf") = False) Then
'Espera a que esté activa la ZFI0050
If EsperaW(wHandle) = False Then Stop
'Limpia ZFI0050
SK "^/"
SK "/nZFI0050"
SK "{ENTER}"
DoEvents
Sleep 1000
'Rellena ZFI0050
SK vSoc & "{DOWN}"
SK vOPNUM & "{DOWN}"
SK vAÑO
'retenciones (vret)
If vRet = "SOLO OP" Then
SK "^({TAB 2}) "
ElseIf vRet = "SOLO RET" Then
SK "^({TAB 2}){TAB} "
ElseIf vRet = "AMBAS" Then
SK "^({TAB 2}) {TAB} "
End If
'Ejecuta ZFI0050
SK "{f8}"
DoEvents
'Solo ret
If vRet = "SOLO RET" Then
EsperaW sCaption:="Vista de impresión para Local", vClass:="SAP_FRONTEND_SESSION", iSleep:=250
SK "^(p)"
End If
'Ventana interna impresión de SAP
If vRet = "SOLO OP" Or vRet = "AMBAS" Then
vSubWinButPrint = sButSapAceptar
SendMessage vSubWinButPrint, BM_CLICK, 0, 0
End If
'Cuadro impresión de Windows
'Espera
vTimer = Timer
Do
If FindWindow(vbNullString, "Imprimir") > 0 Then
v1 = FindWindow(vbNullString, "Imprimir")
If EsperaW(wHandle:=v1) Then Exit Do
End If
If Timer > vTimer + 10 Then Stop
Loop
'Acepta
Sleep 250
SK "{enter}"
DoEvents
'Cuadro Guardar Impresión Como
'Espera
If EsperaW(sCaption:="Guardar impresión como", vClass:="#32770", iSleep:=250) = False Then Stop
'Envía valores nombre archivo op
If vRet = "SOLO OP" Or vRet = "AMBAS" Then
SK vFilename & "{ENTER}"
ElseIf vRet = "SOLO RET" Then
SK vFilename & "-RET" & "{ENTER}"
End If
'Retenciones (sólo para AMBAS)
If vRet = "AMBAS" Then
EsperaW wHandle:=wHandle, iSleep:=250
SK "^(p)"
DoEvents
Sleep 10
'Cuadro impresión de Windows
'Espera
vTimer = Timer
Do
If FindWindow(vbNullString, "Imprimir") > 0 Then
v1 = FindWindow(vbNullString, "Imprimir")
If v1 = GetForegroundWindow() Then Exit Do
End If
If Timer > vTimer + 10 Then Stop
Loop
'Acepta
Sleep 500
SK "{enter}"
'Cuadro Guardar Impresión Como
'Espera
If EsperaW(sCaption:="Guardar impresión como", vClass:="#32770") = False Then Stop
Sleep 10
'Envía valores nombre archivo
WSCRIPT.SendKeys vFilename & "-RET" & "{ENTER}", True
End If
'Pinta la celda
vCelda.Interior.Color = vbGreen
End If
Next vCelda

Function EsperaW(Optional ByVal wHandle As Long, Optional ByVal sCaption As String, Optional ByVal vClass As String, Optional vEspera As Double, Optional ByVal iSleep As Long) As Boolean
Dim vTimer As Double


EsperaW = False
If vEspera = 0 Then vEspera = 15
'wHandle
If wHandle = 0 Then
vTimer = Timer
Do
wHandle = GetHandleFromPartialCaption_long(0, sCaption, vClass)
If wHandle > 0 Then Exit Do
If Timer > vTimer + vEspera Then Exit Function
Loop
End If

'Espera ventana activa
vTimer = Timer
Do
If GetForegroundWindow = wHandle Then
EsperaW = True
Exit Do
End If
If Timer > vTimer + vEspera Then Exit Function
Loop

If iSleep > 0 Then
Sleep iSleep
Else
Sleep 250
End If
End Function
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top