PDA

View Full Version : [SOLVED] Shorten loop using boolean

YasserKhalil
08-25-2017, 12:55 AM
Hello everyone
I have a loop that do same procedure twice but slightly different .. and I need to use boolean to shorten those lines

For i = 0 To PNum - 1 Step 2 Set newPDF = CreateObject("AcroExch.pdDoc")
newPDF.Create
NewName = F & "Txp " & Cells(r, 3).Value & ".pdf"
newPDF.InsertPages -1, PDDoc, i, 1, 0
newPDF.Save 1, NewName
newPDF.Close
Set newPDF = Nothing

Set newPDF = CreateObject("AcroExch.pdDoc")
newPDF.Create
NewName = F & "Vsd " & Cells(r, 3).Value & ".pdf"
newPDF.InsertPages -1, PDDoc, i + 1, 1, 0
newPDF.Save 1, NewName
newPDF.Close
Set newPDF = Nothing

r = r + 1
Next i

I tried to use boolean and use also If i Mod 2 but couldn't figure it out ..

YasserKhalil
08-25-2017, 01:00 AM
Thanks a lot
While i am trying I could figure it out

For i = 0 To PNum - 1
Set newPDF = CreateObject("AcroExch.pdDoc")
newPDF.Create
If i Mod 2 = 0 Then
NewName = F & "Txp " & Cells(r, 3).Value & ".pdf"
Else
NewName = F & "Vsd " & Cells(r, 3).Value & ".pdf"
r = r + 1
End If
newPDF.InsertPages -1, PDDoc, i, 1, 0
newPDF.Save 1, NewName
newPDF.Close
Set newPDF = Nothing
Next i

Thank you very much

snb
08-25-2017, 04:05 AM
Cells(0, 3) doesn't exist
F has no value
Use with ... end With
Variable r is redundant, because i=r

YasserKhalil
08-25-2017, 04:30 AM
Thanks a lot snb for reply
In fact this is part of whole code and r is changeable ..
Regards