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
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