Hello.
What do you want to do with the PDF once you have it?
1. To manually see it and then decide whether the checkpoint passes or fails? [One Manual Step]
2. To just attach it somehow to the Report Results for archival and bug reporting? [Auto]
3. To automatically look for a specific string in the pdf to ascertain the success of the checkpoint? [Auto]
I wrote a test script with just a single action to test my theory and it works. It assumes the first option above. Here is the test script:
Code:
Dim returnedStatusCode 'This is the variable that will hold the status of pdf conversion
outputDirectoryForPrintedPdf = Reporter.ReportPath & "\PdfDocs" 'This is where the pdf would be generated; You can change this to suit your needs.
temporaryCacheFolder = Reporter.ReportPath & "\PdfCache" 'This cache will be deleted at the end of conversion but can also be kept through the switch -ClearCacheFinally False. You can change this to suit your needs.
nameOfPdfFile = Environment("ActionName") 'This will be the name of the pdf file generated under the folder:outputPathForPrintedPdf. You can change this to suit your needs.
pathToConversionUtil = "D:\OneNote2PDF\OneNote2PDF\bin\Debug\OneNote2PDF.exe" 'You have to specify the path where you have placed the executable in your machine..
Set oShell = CreateObject("Wscript.Shell") 'Let's get the shell object to run OneNote2PDF command-line utility. O' Shell, come to my rescue!
lastPrintedTime = CStr(Now) ' Capture the current time before proceeding to print
'Since we need the lasprintedtime to be our lower limit for pages, give the print command to the application here AFTER we have captured the current time.
msgbox "Here, printed through application and waited for printing to complete"
' Actually run the command-line utility in the background and wait for the utility to return a code.
' ClearCacheFinally switch deletes the cache files generated during conversion at the end.
' UseUnfiled switch tells the program to use the "Unfiled Notes.one" file
' Output switch determines where the final merged pdf would be placed
' CacheFolder is just a temporary location to hold various pages in the Unfiled Notes section
' PagesAfterThisTime tells the program to look for only those pages which were printed after the specified time
' ShowTOC makes sure that the pdf is not cluttered with a Table Of Contents if you specify it as false
returnedStatusCode = oShell.Run(pathToConversionUtil & " -NameOfPdfFile " & """" & nameOfPdfFile &"""" & " -ClearCacheFinally True -UseUnfiled -Output " & """" & outputDirectoryForPrintedPdf & """" & " -CacheFolder " & """" & temporaryCacheFolder &"""" & " -PagesAfterThisTime " & """" & lastPrintedTime &"""" & " -ShowTOC False -ExportNotebook True -RefreshCache True", 0, True)
' I have hardcoded 0 to mean Success
If returnedStatusCode = 0 Then
fullPathToPdf = outputDirectoryForPrintedPdf & "\" & nameOfPdfFile & ".pdf"
' Okay, now, we have a pdf at the location contained in the location contained in fullPathToPdf variable
' Let's open the pdf.
' Now, if you have Adobe Acrobat Standard or Professional on your machine, then you can use the ActiveX approach, which is more robust.
' I just have Adobe Reader X so I've used another way. It will just show the pdf on screen. Feel free to modify this according to your needs.
pdfViewResult = oShell.Run("acrord32.exe " & fullPathToPdf, 1, True)
' This one manual step is actually hurting me! More information needed to improve this...
testerChoice = MsgBox("Was the PDFOkay?", vbYesNo, "Pdf Verification")
If testerChoice = vbYes And pdfViewResult = 1 Then
Reporter.ReportEvent micPass, "CheckingPrintedPages", "The pages printed are fine and a pdf is available @ " & fullPathToPdf
Else
Reporter.ReportEvent micFail, "CheckingPrintedPages", "The application's Print functionality has a problem. Pdf available @ " & fullPathToPdf
End If
End If
' I have modified the command-line utility to return various codes in case of error
' -1 means Could not obtain reference to OneNote
' -2 means The file Unfiled Notes.one does not exist, which in our case will mean that no prints were received by the OneNote Printer Driver. This is so because even if the file does not exist, OneNote regenerates it every time it receives a print request.
'-3 means that the OneNote couldn't return the OneNote Xml for the notebook we're interested in. Basically, this means that there was some problem with OneNote.
' -9 means that an unknown error occurred.
' -99 means that there were no pages printed after the date time we specified.
If returnedStatusCode = -2 Or returnedStatusCode = -99 Then
Reporter.ReportEvent micFail, "CheckingPrintedPages", "No pages were printed by the application"
Else If returnedStatusCode = -1 Or returnedStatusCode = -3 Or returnedStatusCode = -9 Then
Reporter.ReportEvent micFail, "CheckingPrintedPages", "There was a problem accessing the OneNote Application"
End If
End If
This script works fine on QTP 10.0 with OneNote 2007. I don't see anything that may prevent it from running successfully with QTP 11.0 and OneNote 2010.
How to use
There are two things that this script depends on:
1. The OneNote2PDF.exe that I created after modifying its sourcecode.
OneNote2PDF.zip (Size: 17.83 KB / Downloads: 157)
2. Its required dependency iTextSharp.dll. Now, I couldn't attach this dll in this reply since it exceeded the limit. You need to get this dll from here:
http://onenote2pdf.codeplex.com/releases/view/10273 Just use the Recommended Donwload link. It will get you this dll.
Keep both these files together in some location and update the value of the variable: pathToConversionUtil so that it reflects the full path for the exe.
If you feel that you are confused, please don't hesitate to write back with your doubts.
Some points to note:
1. The approach assumes that after beginning the action and before the conversion is attempted, there is no other program that's printing to OneNote. This is crucial.
2. You will have to call your action / script to ask the application to print pages after you have captured the current date and time (See Ln. 16). Ask me if you have doubts.
3. You can modify the values of various variables I have declared at the beginning of the script according to what suits your environment.
4. All of this depends on a utility called "OneNote2PDF", whose source code I have modified to suit our requirements. The original utility can be found at
http://onenote2pdf.codeplex.com/
I also considered some alternatives to getting this done.
Alternative Approaches
1. Export & Purge
In this case, I thought that we'd start with a blank Unfiled Notes.one file. Print pages, call the script and view the pdf. However, the utility will delete the Unfiled Notes.One file immediately afterward. This approach is kinky because it doesn't allow OneNote to be used for anything else or by any other application then.
2. WMI
Using WMI, listen to the InstanceCreationEvent for Win32_PrintJob and as soon as that fires for the printer called "Send To OneNote 2007", run the script and get the pdf. This would have been GREAT if only it worked. But, since it depends on polling mechanism, it misses updates. Event doesn't always fire so I dropped the idea.
3. Pick N' Choose
There is a third avenue which we can still explore. With this approach, we go to OneNote, get all pages. Start checking the footers of the pages beginning from the latest page until we again reach Page 1, which signals an earlier print job. And then, all these pages we selected, we export to pdf. We can also combine this with the current approach.
4. Image File Format
If you answered yes to the second question I asked first, then we may need to consider exporting the OneNote file to JPEGs instead and then attach it to the Test Results
5. Deeper Inspection of the pdf
If you answered yes to the third question, then we need to replace Ln. 37 with an ActiveX call to Adobe Acrobat. This way we can deeply inspect what pdf contains but this also requires that the machine has either Adobe Acrobat Standard or Professional.
You just check the script, test it and let me know how it goes. If it suffices, then great, otherwise, we'll find a way to sort out the problems you report.
Cheers,
Light.