If a user prints a report, and they happen to be using the Microsoft XPS printer, i would like the default the filename to something meaningful.
i would have thought that the XPS printer would take the name of the print job, and use that as the default filename - but it doesn't.
Is there some other, programatic, way to default the name of the generated XPS file when i print to that printer? i was thinking there might be something like:
a registry key
global shared memory
API call like SetDefaultXPSFilename()
extended attributes about a print job
Example
Automate Excel to create a spreadsheet:
Excel xl = new ExcelApplication();
Workbook wb = xl.Workbooks.Add();
GenerateReport(wb);
wb.PrintOut();
Now if the user's default printer is the Microsoft XPS Document Writer, then the user will get:
i would like a way for that File name to be defaulted to something useful, such as:
20110729 - Chip Bank Settlement Sheet.xps
The user will accept the default filename, and files will organized automatically, rather than the user typing:
asdfadf.xps
References
eggheadcafe: XPS Default File Name
MSDN: XPS Name when Sent to Printer
Bump: 20110729 (12 months later)
Well,
here is a simple way (at least in my case):
(myPrintPage inherits from System.Drawing.Printing.PrintDocument)
With myPrintPage
With .PrinterSettings
If .PrinterName = "Microsoft XPS Document Writer" Then
.PrintToFile = True
.PrintFileName = "c:\test.pdf"
End If
End With
.Print()
End With
I haven't found a way, yet, to determine whether or not the printer I have chosen is going to print into a file, hence the test on the printer's name.
In addition to above, here is a piece of code I found useful:
Let's say that my default printer is NOT the XPS Document Writer. My code needs to archive automatically some data, print a report in XPS, then offer the user to print the report on the default printer. In the second step, I need to change the PrinterSettings of myPrintPage.
Here is how:
'save xps results
'is the XPS printer installed?
Dim myXPSfound As Boolean = False
For Each s As String In System.Drawing.Printing.PrinterSettings.InstalledPrinters
If s.Contains("XPS") Then
myXPSfound = True
Exit For
End If
Next
If myXPSfound Then
'Manual settings of the XPS printerSettings
Dim myXPSPrinterSettings As New Drawing.Printing.PrinterSettings
myXPSPrinterSettings.Collate = False
myXPSPrinterSettings.Copies = 1
myXPSPrinterSettings.Duplex = Drawing.Printing.Duplex.Simplex
myXPSPrinterSettings.FromPage = 0
myXPSPrinterSettings.MaximumPage = 9999
myXPSPrinterSettings.MinimumPage = 0
myXPSPrinterSettings.PrinterName = "Microsoft XPS Document Writer"
myXPSPrinterSettings.PrintRange = Drawing.Printing.PrintRange.AllPages
myXPSPrinterSettings.PrintToFile = True
myXPSPrinterSettings.ToPage = 1
myPrintPage.PrinterSettings = myXPSPrinterSettings
myPrintPage.PrinterSettings.PrintToFile = True
myPrintPage.PrinterSettings.PrintFileName = mytargetFileName & ".xps"
Try
myPrintPage.Print()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Error Printing the XPS File")
End Try
Else
MsgBox("The Microsoft XPS Writer was no found on this computer", MsgBoxStyle.Information, "Error Printing the XPS File")
End If
It can be handy sometimes.
The Microsoft XPS Document Writer (MXDW) will generate an output file path without prompting the user if the application that prints sets lpszOutput in DOCINFO.
If you don't have access to the code of the application then another option is to build an XPS driver that generates a file path even when lpszOutput hasn't been set. The Windows Driver Kit (WDK) is the place to start.
For more details and links see this post.
Win2PDF 7 can save as XPS, and does default to the name of the print job. If you don't want to use the print job as the name displayed in the File Save dialog, you can change the default file name by setting a registry value named "PDFTitle".
You can also set the output file without prompting either using the lpszOutput field of DOCINFO, or by setting a registry setting named "PDFFileName" as described in the Win2PDF documentation. The file will be created in the XPS format if the file name contains an .xps extension.
Related
I'm trying to print by XPS PRINTER without the save dialog.
I'm using the Printer Object on VB6.
Any Idea?
It's not possible to control the XPS Document Writer file name from VB6, but it is possible using a third party product named Win2PDF. To set the XPS file name in Win2PDF, you use the SaveSetting method to save the file name to the registry as in:
SaveSetting "Dane Prairie Systems", "Win2PDF", "PDFFileName", "c:\test\test.xps"
After calling SaveSetting with the key set to "PDFFileName", you print to the Win2PDF printer using the VB6 printer object to create the XPS file.
I'm working on a script for FoldingText which will convert a FoldingText outline (basically a Markdown Text file) into a Remark presentation (an HTML script which makes slideshows from Markdown files). The script works, but I'd like to make the following improvement:
Instead of asking for the name and location to save the HTML file, I'd like to grab the name of the current document and save it as an HTML file (with the same name) in the current folder. The script should fail nicely if there is already a document with that name (offering to either write-over the document or save as a new document with a different name).
The script I'm using for writing to the file was from these forums. The first part is:
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as «class utf8»
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
And the second part is:
set theFile to choose file name with prompt "Set file name and location:"
my write_to_file(finalText, theFile, true)
Thanks.
FoldingText should have some way of retrieveng the path of the document. I've not found any free dowonload fo the application, so I've not benn able to check by myself, but you should be able to find it if you view the dictionary of the application.
My guess is that there's a property like 'path of', or 'file of' for the FoldingText document:
You will probably end up with something like this:
set theDocPath to file of front document of application "FoldingText"
tell application "Finder"
set theContainer to container of theFile
end tell
set newPath to (theContainer & "export.html) as string
repeat while (file newPath exists)
set newPath to choose file name with prompt "Set file name and location:"
end repeat
my write_to_file(finalText, newPath, true)
I have looked everywhere for this solution. The code below allows me to print to the printer, Adobe PDF, but what I want to do is automate the file name save as screen with a generic name and in a specific folder. For example, the file would be saved to C:\temp\tmpResize.pdf and I am having problems there.
var params = this.getPrintParams();
params.interactive=params.constants.interactionLevel.silent;
params.pageHandling=params.constants.handling.none;
params.fileName = "/c/temp/tmpResize.pdf";
params.printerName="Adobe PDF"
this.print(params);
Thanks for your help.
I've created a MigraDoc/PdfSharp document and now need to send it to a specific printer without any user interaction.
What do I need to use as a Renderer and how do I set the printer path/name to the MigraDocPrintDocument?
MigraDocPrintDocument is the correct class.
// Creates a PrintDocument that simplyfies printing of MigraDoc documents
MigraDocPrintDocument printDocument = new MigraDocPrintDocument();
// Attach the current printer settings
printDocument.PrinterSettings = printerSettings;
We use System.Windows.Forms.PrintDialog() to let the user select the printer (this dialog fills the printerSettings structure).
Use
internal PrinterSettings printerSettings = new PrinterSettings();
for the default printer. Change this structure to print with different settings or on a different printer.
Please note that with PDFsharp 1.31, printing will work with the GDI+ build only (the WPF build will not print the document correctly).
I read an Excel 2003 file with a text editor to see some markup language.
When I open the file in Excel it displays incorrect characters. On inspection of the file I see that the encoding is Windows 1252 or some such. If I manually replace this with UTF-8, my file opens fine. Ok, so far so good, I can correct the thing manually.
Now the trick is that this file is generated automatically, that I need to process it automatically (no human interaction) with limited tools on my desktop (no perl or other scripting language).
Is there any simple way to open this XL file in VBA with the correct encoding (and ignore the encoding specified in the file)?
Note, Workbook.ReloadAs does not function for me, it bails out on error (and requires manual action as the file is already open).
Or is the only way to correct the file to go through some hoops? Either: text in, check line for encoding string, replace if required, write each line to new file...; or export to csv, then import from csv again with specific encoding, save as xls?
Any hints appreciated.
EDIT:
ADODB did not work for me (XL says user defined type, not defined).
I solved my problem with a workaround:
name2 = Replace(name, ".xls", ".txt")
Set wb = Workbooks.Open(name, True, True) ' open read-only
Set ws = wb.Worksheets(1)
ws.SaveAs FileName:=name2, FileFormat:=xlCSV
wb.Close False ' close workbook without saving changes
Set wb = Nothing ' free memory
Workbooks.OpenText FileName:=name2, _
Origin:=65001, _
DataType:=xlDelimited, _
Comma:=True
Well I think you can do it from another workbook. Add a reference to AcitiveX Data Objects, then add this sub:
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
Dim stream As ADODB.stream
Set stream = New ADODB.stream
With stream
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, adSaveCreateOverWrite
.Close
End With
Set stream = Nothing
Workbooks.Open sPath
End Sub
Then call this sub with the path to file with the off encoding.