VBA doesn't export every cell from excel to PDF file - vba

I'm a bit confused. The export from an excel sheet to a pdf file actual works fine. but after I added a new sentence in bottom of the sheet, it doesn't copy this sentence in the pdf file. My excel sheet contains a form of 180 cells. In 181 is the new sentence but it won't take this in pdf.
Could anyone tell me what the problem is presumably?
If you need more information, I would try to give you some more details.
Edit:
The export is defined as follows:
Worksheets(NameSheet).ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=File, _
Quality:=xlQualityStandard, _
IncludedocProperties:=True, _
IgnorePrintAreas:=False
The sentence is added directly in excel sheet without vba code. I searched in the code for .Cells(181,1) but nothing was defined. That means for me, in the pdf the sentence should be generated.

I figured out. That was a setting in excel.
the solution is described on this page: Set a print area
Description:
On the worksheet, select the cells that you want to define as the print area. (I selected all cells up to 181)
On the Page Layout tab, in the Page Setup group, click Print Area, and then click Set Print Area.

You can also manually set the print area if you want.
With ActiveSheet.PageSetup
.Orientation = xlPortrait
.PrintArea = "$A$1:$AA$181"
.Zoom = False
.FitToPagesTall = False
.FitToPagesWide = 1
End With

Related

Converting multiple sheet Excel file to PDF using VBA error

I am currently trying to convert a multiple sheet excel file to a PDF using VBA using the following code:
Private Sub CommandButton1_Click()
Dim mySheets As Variant, sh
mySheets = Array("Sheet1", "Sheet2", "Sheet3")
For Each sh In mySheets
Sheets(sh).PageSetup.Orientation = xlLandscape
Next
Sheets(mySheets).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="S:\GasInc\Services\B&ITS\OpsEng\EngServ\_Station Design\Projects\Station Co-ops\Angela Lian" & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
End Sub
It converts my file fine, however for what I have on sheet 2 it splits it up into multiple pages in the PDF because I guess it does not scale it to fit the page. I was wondering how I could modify the code to make it scale this sheet so it will fit on one page of the PDF.
Thanks!
I have a similar sub in one of the tools I use. Do you have your worksheet's width / height scale to fit properties set to 1 page?
For Each sh In mySheets
With Sheets(sh).PageSetup
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Next sh
One of the weird things I've run into is scaling issues when converting to PDF with some chart / image objects. For instance a logo looks fine on screen but prints stretched on the pdf. I never fixed the problem, but through research it seemed due to the way the printer sees it compared to the screen resolution. Have you ever run into that problem?

VBA ExportAsFixedFormat only saving last active chart

I'm trying to save a single worksheet from a workbook as a pdf using the ExportAsFixedFormat method:
Sheets("Overview").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Mid(saveFile, 1, InStr(saveFile, ".")) & "pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
This worked for a while until i started doing some chart manipulation beforehand. The manipulation I am talking about looks like this:
ActiveSheet.ChartObjects("Diagramm 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Measurements").Range( _
"C4:C29,G4:G29")
Now its not exporting the whole sheet as a pdf but rather only the chart called "Diagramm 4".
I more or less understand why it's doing this but I can't find a way to fix this.
You could try selecting any cell on that sheet like:
Range("A1").Select
before you export the page. Likely this is happening because you're making the chart active without making it inactive again. Look what happens when you normally select a chart and then try to print a worksheet - it'll just try to print the chart.

Excel VBA macro Print to PDF - output file not supported?

I have a simple macro that selects a sheet, enters basic data then prints to a PDF file. The macro works and the file is created, however, when trying to open the resulting file, an error comes up on Adobe Acrobat saying 'Acrobat could not open 'the document' because it is either not a supported file type or because the file has been damaged.
Here's my code (names and text changed to protect the innocent)
Sheets("Sheet1").Select
ActivePrinter = "CutePDF Writer on CPW2:": _
Range("Ab2").Select
ActiveCell.FormulaR1C1 = "text 001"
Range("Ab3").Select
ActiveCell.FormulaR1C1 = "text 002"
Range("Ab4").Select
ActiveCell.FormulaR1C1 = "text 003"
ActiveWindow.SelectedSheets.PrintOut _
printtofile:=True, Collate:=True, prtofilename:="filename.pdf"
If I comment out the ActivePrinter line and change the filename to a .ps file, it will create a postscript file that opens just fine in Acrobat. I know I can then convert the .ps file to .PDF, but I'm trying to avoid that since the macro runs thru 150+ variations creating 150+ files.
Has anyone had similar experiences, and if so, what did you do to overcome them?
Any assistance would be greatly appreciated.
This is the code (from macro recorder) that will save a file as PDF:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Temp\Sample.pdf", Quality:= xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Save Excel Worksheets as Seperate PDFs with a Save Location Dialog Box

I have an Excel file with quite a few worksheets in it (with more to be added eventually). At the moment, I've been going into each sheet and saving them as a PDF individually.
I was wondering if there is a vba macro to save every sheet in the file as a seperate PDF.
I would like the user to have the option of saving the files to a directory of his or her choosing, and I think this would be easiest with a browsing dialog box. The PDFs would be named after the Sheet Name suffixed with " - Some Text".
If this is possible, I would assign the macro to a button in a sort of 'splash' page where I already have a few macro buttons existing.
Also, would there be any way of excluding the 'splash' page from saving as a PDF?
Many Thanks.
Mantas
If the splash page is sheet number 1 then this will work:
Sub SaveOutput()
folderChoice = setupFolders()
ChDir (folderChoice)
For i = 2 To Sheets.Count 'if the splash screen is Sheet 1 it won't be saved.
Worksheets(i).Select
Call exportSheet(Sheets(i).Name & " - Some Text")
Next i
End Sub
Function setupFolders()
MsgBox ("Navigate to output directory...")
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
If .Show = 0 Then
End
End If
FolderName = .SelectedItems(1)
End With
setupFolders = FolderName
End Function
Sub exportSheet(outputName)
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=outputName & ".pdf"
End Sub

Excel vba emf pasted to worksheet print/pdf wrong size

I'm taking a enhanced meta file generated from another application (DPlot Jr) copying to the clipboard, and pasting it to an Excel(2007) worksheet. From there i use vba to convert that file to a pdf. This is the code i have to do that:
' copy the graph eml file to the clip board
ret = DPlot_Command(doc, "[CopyPicture()]")
' copy the clip board contents to a new temp worksheet (under the covers)
'Hide the application
Application.ScreenUpdating = False
'Create a new temp worksheet
Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
ws.Name = "Temp_Graph_DPJR"
ws.Activate
'Paste to the temp worksheet
ActiveSheet.Paste
'Save as pdf from excel
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=pdf_filename, _
OpenAfterPublish:=False
I discovered that, the image that gets created in the PDF is slightly larger than the actual size of the graphic. For example, the width of the graphic should be 2.65". The size of the graph in the PDF is 2.816, so about .166" or 1 Pica, appears to be added.
While i could, and may have to, just decrease the size of the image initially by .166" that seems kind of hacky and I'd just like to have the image's original size to come over.
I discovered that if i paste the image to a Chartsheet, the size IS maintained, but the image becomes a bitmap on teh Chartsheet page.
When i create the pdf, i have all the correct settings. I have no margins, actual size, etc.
Has anyone else seen this? Can anyone help? I need to have the image as a pdf.
Thanks for any help!
Russ
Well, This is strange, and shouldn't make a difference, but it does, so far appear to work.
In the above workflow, after pasting the EMF to a worksheet, if i select that image THEN copy that to a new Chartsheet, the original size is maintained, as well as the vector nature of the graphic. Here is the code:
ret = DPlot_Command(doc, "[CopyPicture()]")
' copy the clip board contents to a new temp worksheet (under the covers)
'Hide the application
Application.ScreenUpdating = False
'Create a new temp worksheet
Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
ws.Name = "Temp_Graph_DPJR"
ws.Activate
'Paste to the temp worksheet then select/copy that one
ActiveSheet.Paste
Selection.Copy
'Create a new temp chart
Set temp_chart = ActiveWorkbook.Charts.Add
'temp_chart.Name = "Temp_Chart"
'Set the linesytle of the border to none
temp_chart.ChartArea.Border.LineStyle = xlNone
'Paste the dplotjr graph to the chart sheet
'We had to do this as this maintained the size of the graph
'Dumb MS Excel worksheet
temp_chart.Paste
'Paste to the temp worksheet
'ActiveSheet.Paste
'Save as pdf from excel
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=pdf_filename, _
OpenAfterPublish:=False
It is almost as though Excel determines that the paste is from another app and pastes a bitmap to the chartsheet, but if it determines that it is from itself, it pastes as a vector image. This maybe a loophole that might go away in the future, but it seems to work so far.
fwiw!