Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

setting borders & formatting after save as PDF

0

hello

I  need  setting the  borders & formatting   where showing data in listbox on userform after save PDF . so  when show  the  data in listbox  on userform  or  select item in combobox1 and  populate data in listbox when press commandbutton1  should  show  the  same formatting  and borders as  in sheet1  when save  the  file as pdf 

this is  the  code  for  saving data  as pdf 

Private Sub CommandButton1_Click()
  ListBoxToPDF ListBox1, ThisWorkbook.Path & "\ListBoxToPDF.pdf"
  Unload Me
End Sub

Sub ListBoxToPDF(listbox As Object, pdf As String)
  With Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    .Range("A1").Resize(listbox.ListCount, listbox.ColumnCount) = listbox.List

    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdf _
      , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
      :=False, OpenAfterPublish:=False

    .Parent.Close False
  End With
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hi Ali and welcome to the Forum

if you only want to see all the data and to have a simple border around the cells (in the pdf), you can just add the two lines in bold below (but I added two comments too for your guidance):

Private Sub CommandButton1_Click()
  ListBoxToPDF ListBox1, ThisWorkbook.Path & "\ListBoxToPDF.pdf"
  Unload Me
End Sub

Sub ListBoxToPDF(listbox As Object, pdf As String)
  With Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    .Range("A1").Resize(listbox.ListCount, listbox.ColumnCount) = listbox.List
    ' autofit the columns
    .UsedRange.Columns.AutoFit
    ' draw simple borders around cells
    .UsedRange.Borders.LineStyle = xlContinuous

    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdf _
      , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
      :=False, OpenAfterPublish:=False

    .Parent.Close False
  End With
End Sub

The attached revised file has those changes.

Note that the whole ListBox is exported to pdf if  you don't pick a "code" in the Userform.

Hope this fixes things for you. Kindly mark this Answer as Selected if so.

Discuss

Discussion

great ! many  thanks 
Ali M (rep: 28) May 18, '22 at 11:37 am
Glad it worked for you. Thanks for selecting my Answer, Ali.
John_Ru (rep: 6142) May 18, '22 at 11:53 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login