.netexcelvb.netprintingexport-to-excel

Excel can't find anything to print


I am working on an application, who needs to export data. I export this data to JSON, DB and after it I create a certificate in excel.

The export to excel and the excel file creation works perfectly, BUT I am not able to print the excel. I tried it from my App (VB.NET) and also with VBA (to check if was a problem with my app), but both fails. If I open the file with excel, I am not able to print it and can't see the preview although PrintRange is set.

I always get

"Excel couldn't find sth to print"

I tried to print the workbook, the sheet and a range, but always the same. I also tried setting worksheet.PageSetup.PrintArea and the same. Trying ExportAsFixedFormat doesn't work as well. I also tried to set all my installed printers as default and nothing, it fails with every printer.

That happens only with this file, any other files print without problems.

If I open the file--go to print--also see an "Nothing to print" instead of the preview, although print range is set.

Now, if I click in a cell, write sth. and delete it or make any interaction, then I am able to see the preview and print the file as well.

If I save now the working file and try in a simple App to open the file and print it, the bad message comes. If I reopen the file in excel, I don't see the preview again and have to interact with the file to be able to print.

I have tried all what I could and also searched like crazy but anything has worked.

I hope someone have had the same issue and can help me, or just knows what I am doing wrong or what is wrong with my excel file, since this is the first time in my life (and work a lot with excel) that I've seen this message.

The export code is a bit long, so I haven't posted it here, but it's sth like this:

    Public Shared Function ExportCalibrationAsync(pressureCalibration As PressureCalibration, filePath As String, fileName As String, excelTemplate As Byte()) As Task 'TODO:Refactor...Not use 2.dots
        Return Task.Run(Sub()
                            CheckFileName(fileName)
                            Dim fullPath = Path.Combine(filePath, fileName)
                            Dim tempFileName = Path.GetTempFileName()
                            Dim tempFullPath = Path.Combine(filePath, tempFileName)
                            Dim excel = New Excel.Application()
                            Dim row = 1
                            'Write the passed excel template to a tempFile
                            My.Computer.FileSystem.WriteAllBytes(tempFullPath, excelTemplate, False)
                            Dim excelWorkbooks = excel.Workbooks
                            'Add template workbook to actual workbook
                            Dim excelWorkbook As Excel.Workbook = excelWorkbooks.Open(tempFullPath)

                            '////////////
                            '////Code to Export data to excel
                            '////////////

                            '//ExportAsFixedFormat or Print doesn't work and
                            '//MsgBox pops up with "Excel could not find anything to print"
                            excelWorkSheet.PageSetup.PrintArea="$A$1:$G$40"
                            excelWorkSheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,filePath,XlFixedFormatQuality.xlQualityStandard)
                            excelWorkbook.SaveAs(fullPath)
                            excelWorkbook.Close()
                            excel.Application.Quit()
                            excel.Quit()
                            File.Delete(tempFullPath)
                            GC.Collect()
                            GC.WaitForPendingFinalizers()
                            GC.Collect()
                            GC.WaitForPendingFinalizers()
                            Marshal.ReleaseComObject(calibrationChartObject)

I have a chart in this sheet, but I don't think the chart is the problem, since I can print other files with charts using the same code.


Solution

  • I finally (after 2 weeks) find the solution, at least it works for me.

    I have saved all the time the file with the pagelayout view activated. The one in the middle in the photo. Preview

    Today I was testing again, and I decide to save activate the normal layout and voilá it works!!!

    I've checked and that's definitely the fix...If I activate the pagelayout in any file, save it and try to export it...."Excel couldn't find anything to print".

    It's really weird (at least for me) this error. If anyone knows if that has a reason tell me...I think is maybe a small bug.