Running my Macro changes Page Breaks

Benny236

New Member
Joined
Feb 29, 2016
Messages
33
Hi all.

I'm running a macro that prints an invoice to PDF within excel. Problem is, when I run the macro, the page breaks all change and it turns it into a badly formatted 18 page document. I'm running OSX.

Any ideas? Many thanks in advance.

Macro code below:



Sub GetDataGenerateInvoices()

Dim DealerCounter As Integer
Dim DealerCodePos As Integer
Dim strFile As String
Dim strPathFile As String
Dim strSheet As String
Dim strInvoiceTemplate As String

Dim dataWorkBook As Workbook
Dim masterBook As Workbook
Dim tempFileName As Variant
Dim tempFilePath As Variant
Dim rngDataBook As Range
Dim rngMasterBook As Range

Set masterBook = ActiveWorkbook

tempFileName = MacScript("Choose file as string")
tempFileName = Replace(tempFileName, ":", "/")
tempFilePath = Right(tempFileName, Len(tempFileName) - InStr(tempFileName, "/") + 1)

Set dataWorkBook = Workbooks.Open(tempFilePath)

'Get sales data and copy
Set rngDataBook = Application.Range("Current!A6:F2000")
masterBook.Activate
Set rngMasterBook = Application.Range("Current!A6")
rngDataBook.Copy rngMasterBook
rngMasterBook.CurrentRegion.EntireColumn.AutoFit

'Get invoice numbers and copy
dataWorkBook.Activate
Set rngDataBook = Application.Range("Current!AB4:AB50")
masterBook.Activate
Set rngMasterBook = Application.Range("Current!AB4")
rngDataBook.Copy rngMasterBook
rngMasterBook.CurrentRegion.EntireColumn.AutoFit

'Get addresses and copy
dataWorkBook.Activate
Set rngDataBook = Application.Range("Addresses!A5:B100")
masterBook.Activate
Set rngMasterBook = Application.Range("Addresses!A5")
rngDataBook.Copy rngMasterBook
rngMasterBook.CurrentRegion.EntireColumn.AutoFit

dataWorkBook.Close False

'create and save invoice for all dealer codes
For DealerCounter = 1 To Worksheets("Invoice 1").Range("K18").Value

'used to cycle through the unique dealer codes
DealerCodePos = DealerCounter + 3

If Not Worksheets("Current").Range("AA" & DealerCodePos).Value = "RM" Then

'change dealer code in worksheet
Worksheets("Current").Range("P6").Value = Worksheets("Current").Range("AA" & DealerCodePos).Value

'wait 2 seconds to allow update of cells
Application.Wait (Now + TimeValue("0:00:00"))

'create PDF file name
strFile = Worksheets("Invoice 1").Range("F10").Value & ".pdf"
strPathFile = masterBook.Path & "/" & strFile

'choose which invoice template to use
strSheet = Worksheets("Invoice 1").Range("L18").Value
strInvoiceTemplate = "Invoice " & strSheet

'export to PDF
Worksheets(strInvoiceTemplate).ExportAsFixedFormat _
Type:=xlTypePDF, _
fileName:=strPathFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

End If
Next
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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