title: VBA Issues with Inserting Multiple PDF Objects Within a Loop
body: <p>My set-up is that I have a bunch of blank templates in a folder. Inside each blank template is a fund code (it is the only thing in the template)</p>
  
  <p>The below macro I created (in an external workbook) goes through the folder with the templates, opens each template, and "fills it out" via a loop.</p>
  
  <p>Basically my macro opens each template, assigns the fund code to a variable and then uses that variable in combination with some text strings to pull in other worksheets/PDF objects related to that specific fund code.</p>
  
  <p>My issue is that in a more meaty version of the below code, I added maybe four or five more PDF objects to insert. It'll go through some of the templates and then randomly stop on a random fund code at a random pdf object insert line saying either "object cannot be found" or "object cannot be inserted"</p>
  
  <p>If I press debug and then press F8 to run that line again, it is able to insert the object no problem. So perhaps my code is running too fast for adobe to handle? I am unsure. Perhaps my code isn't doing things as efficiently as possible. This would save sooo much time for my team, I just can't be having it work half the time.</p>
  
  <p>(also the file names have definitely been correct, so that is not an issue)</p>
  
  <pre><code>Public Sub test()
  Set currentbook = ActiveWorkbook
  
  Application.AskToUpdateLinks = False
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Dim wbk As Workbook
  
  Dim filename1 As String
  Dim Path As String
  Dim a As Long
  Path = "C:\Users\Bob\Desktop\Workbooks\"
  filename1 = Dir(Path &amp; "*.xlsm")
  '--------------------------------------------
  'OPEN EXCEL FILES
   Do While Len(filename1) &gt; 0  'IF NEXT FILE EXISTS THEN
   Set wbk = Workbooks.Open(Path &amp; filename1)
   wbk.Activate
  
  'Gets Fund Code
  Sheets("Initialize").Select
  Dim FdCode As String
  FdCode = Worksheets("Initialize").Range("D8")
  
  
  
  '--------------------------- PDF ADDS
  
  'Add PDF TB----------------------------------------------------
  
  Worksheets("F.a - Working TB").OLEObjects.Add filename:="C:\Users\Bob\Desktop\Raw Reports\R122 04.30.16 - 04.30.17\" &amp; FdCode &amp; " 04.30.16 TB.PDF", Link:=False, DisplayAsIcon:=False, Left:=40, Top:=40, Width:=150, Height:=10
  On Error GoTo 0
  
  
  'Add PDF Closed Options----------------------------------------------------'
  
  Worksheets("T300.1 - Options (Closed)").OLEObjects.Add filename:="C:\Users\Bob\Raw Reports\Other Reports 04.30.16-04.30.17\Breakout\" &amp; FdCode &amp; " other 04.30.17_                                                   CLOSED OPTIONS POSITION REPORT.PDF", Link:=False, DisplayAsIcon:=False, Left:=40, Top:=40, Width:=150, Height:=10
  On Error GoTo 0
  
  
  
  
  
  
  
  ActiveWorkbook.Save
  
  
  
      wbk.Close False
  
  
  filename1 = Dir
  Loop
  
   Application.ScreenUpdating = True
  End Sub
  </code></pre>