VBA Excel to PDF autofill - Makro filling out info in the wrong window

Latskap

New Member
Joined
Aug 18, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am working on a code that autofills info from Excel to PDF, saves it and then starts again.

When i start the makro the information will be filled into whatever window is opened (if this is a bug, and can be corrected, please help). Because of this, I need to make sure that the PDF File always is the one that is opened.

Sometimes, after filling the PDF with information and saving, the makro reverts back to the Excel file and starts to fill a random bracket out. This is my problem. I need the PDF to stay maximized as the makro is running, so that the information is correctly filled into the PDF, and not everywhere else.

If there is any help to be had, I will be most grateful.

Makro

Sub CreatePDFForms()
Dim PDFTemplateFile, NewPDFName, SavePDFFolder, LastName As String
Dim CustRow, LastRow As Long
Dim Navn
With Ark1
If .Range("Q18").Value = Empty Or .Range("Q20").Value = Empty Then
MsgBox "Both PDF Template and Saved PDF Locations are required for macro to run"
Exit Sub
End If
LastRow = .Range("E9999").End(xlUp).Row 'Last Row
PDFTemplateFile = .Range("Q18").Value 'Template File Name
SavePDFFolder = .Range("Q20").Value 'Save PDF Folder
ThisWorkbook.FollowHyperlink PDFTemplateFile
Application.Wait Now + 0.00006

For CustRow = 3 To LastRow
Navn = .Range("B" & CustRow).Value
Application.SendKeys "{Tab}", True
Application.SendKeys Navn, True
Application.Wait Now + 0.00001 'Navn

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("C" & CustRow).Value, True
Application.Wait Now + 0.00001 'Adresse

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("D" & CustRow).Value, True
Application.Wait Now + 0.00001 'Postadresse

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("E" & CustRow).Value, True
Application.Wait Now + 0.00001 'Epost

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("A1" & CustRow).Value, True
Application.Wait Now + 0.00001 'Konkursbo

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("I" & CustRow).Value, True
Application.Wait Now + 0.00001 'Feriepenger

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("J" & CustRow).Value, True
Application.Wait Now + 0.00001 'Feripenger

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("H" & CustRow).Value, True
Application.Wait Now + 0.00001 'Lønn

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("K" & CustRow).Value, True
Application.Wait Now + 0.00001 'Andre ytelser

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("L" & CustRow).Value, True
Application.Wait Now + 0.00001 'Skatt

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("M" & CustRow).Value, True
Application.Wait Now + 0.00001 'Skattetrekk

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("P" & CustRow).Value, True
Application.Wait Now + 0.00001 'Brutto

Application.SendKeys "{Tab}", True
Application.SendKeys .Range("G" & CustRow).Value, True
Application.Wait Now + 0.00006 'Brutto

Application.SendKeys "^(p)", True
Application.Wait Now + 0.00003
Application.SendKeys "{Enter}", True
Application.Wait Now + 0.00003

Application.SendKeys "%(i)", True
Application.Wait Now + 0.00003

Application.SendKeys (SavePDFFolder & "\" & Navn & ".pdf")
Application.SendKeys "{Enter}", True
Application.Wait Now + 0.00009

Next CustRow

Application.SendKeys "^(q)", True
Application.SendKeys "{numlock}%s", True

End With
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,214,591
Messages
6,120,432
Members
448,961
Latest member
nzskater

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