How to print from excel to pdf without typing in a filename - excel

Is there a way to get around having to type in a filename when sending a document to Adobe PDF printers? My code is:
Application.ActivePrinter = "Adobe PDF on Ne01:"
MyWorkbook.PrintOut From:=1, To:=3, Copies:=2, Collate:=True
This works except that it prompts me for a filename. Would rather find a way to fix this without installing any extra libraries so it would be portable.
Thanks

Does this help at all?
"How To: Changing the Adobe PDF
Printer/Distiller Printer Settings"
"Issue
You wish to automate printing from a
third party application to either
Adobe PDF or Distiller print drivers
so that no UI elements are invoked for
example 'Do not send fonts to
Distiller', 'View results in Acrobat',
'Prompt for PDF Filename', 'Delete log
files for successful jobs', and the
'Ask to Replace existing PDF file'
preferences when you print.
Solution..."

I had to solve a similar problem where I had to create PDFs with automatically-generated file names.
In my solution, I installed PDF Creator, which acts like a printer on your computer, except that it saves PDF files when you print to it. One of its settings allows you to automatically save a PDF to a directory when you print to PDF Creator.
I set up my Excel application to print repeatedly to PDF Creator. I can't remember which it was (perhaps the name of the worksheet?), but one of the sheet's properties controlled the title of the document and thus the name of the saved file.
This isn't an ideal situation because it requires you to manually toggle a setting in PDF Creator, but it worked very well for a simple application I ran on my own machine.

Related

Convert .xls to .pdf using LibreOffice via Command Line

I'm trying to convert a .xls file to .pdf using LibreOffice via command line on Ubuntu. I have a kind of report on the .xls file with some colors in the background of the cells and etc.
The problem is when I convert the .xls file, the .pdf loses the original format. Each page is broken almost in the half and the content of one page is displayed in two different pages.
Does anybody know how to convert the .xls file to .pdf via command line with keeping the original format?
Or some trick to set the size of the .pdf page to not break pages? (Also via command line)
The code I used to make the conversion was:
soffice --headless --convert-to pdf:"impress_pdf_Export" filename.xls
If you use LibreOffice to convert Microsoft Excel (XLS) files to PDF documents, this is a two-step process (even if your command does look like it is a one-step process):
Import the XLS into LibreOffice (even if started with --headless).
Export the PDF from LibreOffice.
If the result does not look like you expect (not similar enough to Excel's native PDF export), then start with debugging the first step from above:
Open the XLS file with LibreOffice in a GUI. Does it look like you expect it to look? Or are some formatting options looking weird?
Export the PDF from there (with the GUI). Are the page dimensions as you expect? Did you set them up how you prefer? The margins like you want them? etc.pp. ...
If you are working on Windows, you may also want to consider OfficeToPDF.exe. It is hosted on CodePlex, licensed with the Apache 2.0 License and available in binary and in source code.
It requires a working Office 2013, Office 2010 or Office 2007 installation. But then it can commandline- and batch-convert to PDF various MS Office-based file formats, including XLS(X), PPT(X), DOC(X), VSD(X) and PUB as well as Libre/OpenOffice-based ODT, ODS and ODC files.
Although this is a little bit off from the initial question (you don't _really need Office Libre if you have the Office suite and on a Windows machine)
I do appreciate the follow-up provided by Kurt. It prompted me to post the following Gist offering some clear instructions on how to go about using the .exe in a for loop.
https://gist.github.com/einsty/2189cae4175f619cff0f
Try copying appropriate font file (for me it's
a simsun.ttc file) to your libreoffice installing directory like '/opt/libreoffice4.2/share/fonts/truetype'.But if the width of a single excel sheet is too much for a print page(sth like 'A4'),it'll still collapse.

Copy embedded text file from an Excel sheet with VBA

I have an Excel sheet with an embedded txt file which I would like to copy into a certain target folder.
I used the following code which works fine if I use it from my computer:
'copy oleobject
ActiveSheet.OLEObjects(1).Copy
'paste to activeworkbook's path
CreateObject("Shell.Application") _
.Namespace(ActiveWorkbook.path) _
.Self.InvokeVerb "Paste"
However, some of my colleagues did receive the text file which was embedded in the Excel sheet with an additional line beforehand which included the file save path in the temporary folder.
I could just delete this line with VBA but I would like to understand why this happens with other computers and not with mine.
Please help!
Thanks
OK so here is the issue:
txt file is a template and VBA is supposed to read it in again in a string file and change it. Until now it was just saved in a different folder and that worked fine but for mobility reasons (to be used by many other users) it would be a great advantage if it could be handled with just one file.
It is possible to do this with txt file embedded in Excel, but embedded files are not really well-supported by VBA automation. Most files only have a few methods available, and if I remember correctly with TXT file type, the available method is to open the file.
ActiveSheet.OLEObjects(1).OLEFormat.DoVerb 1
I mentioned a similar problem that I had (in PPT, instead of Excel, but the issue is the same). The route we chose initially was given here:
Extracting an OLEObject (XML Document) from PowerPoint VBA
Invoke the .OLEFormat.DoVerb 1 to open the file in Notepad
Use WinAPI functions to read the contents of Notepad into a string
variable
Use WinAPI functions to close Notepad
Use FileSystemObject to write/modify a new text file
Embed the new, modified text file
The functions to find and read Notepad contents are documented here and require some use of WinAPI functions (all noted in the link):
http://www.excelforum.com/excel-programming-vba-macros/729730-access-another-unsaved-excel-instance-and-unsaved-notepad-text.html
This is a LOT of work for not much benefit, instead you could simply open a file dialog prompting user to choose the text file (located on a shared network drive, etc.). This would be much more reliable.
Alternatively, depending on the size of the text file, you can store the contents inside a Shape (and you can put the shape on a hidden worksheet, etc.), either in the .TextFrame or another property that allows text. Ultimately we abandoned the solution in the links above in favor of storing the contents inside a Shape's .AlternativeText property. This worked very well for us, in storing XML contents of about 500,000 to 1 million characters per file.
The reason we chose not to go the Notepad route is that there was a lag while the file is being read and user could accidentally interrupt the procedure, corrupting the file(s), etc. and that Notepad doesn't fully support automation, etc.
I also thought of using a text field but if the oleoobject would work I thought I could also embed other file types like pictures.
Pictures are easy to work with because they have a built-in .Export method.

Importing .xls to Access .mdb: External Table is not in the expected format

I have a macro that imports a spreadsheet as follows: (this spreadsheet is an export from a web-based application, and during the initial export the chosen format is 97-2003)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "d2s_safety_tbl", _
"\\company.com\dfsroot$\Share\office_public\D2S\D2S\D2S_Scorecard\Source Data\D2S\D2S Safety.xls", True
When importing to Access, I get:
Run-time Error '3274': External table is not in the expected format.
When I open this Excel file, I get a dialog
"The file you are trying to open is in a different format than specified by the file extension..."
So the file name is .xls, my computer tells me its the 97-2003 Format, but once I open the file and click save, it defaults to save it as a Web Page format with the option to save as .xls. What gives?
UPDATE: If I open the file, then Save As .xls format (seemingly redundant, but apparently not), it asks me if I want to overwrite the existing file, so I do. Once I go through this, the VBA import is successful. I can't have the clerk go through this process every week--any way to avoidd this? Possibly the initial export from the web-based application?
DoCmd.TransferSpreadsheet is refusing to import your .xls file because it is not really an .xls file, it is an HTML file that has been given an .xls file extension. Providing a "fake" file extension is a trick that I've seen other "developers" use, and it really is a Bad Idea (for the reasons we've seen here).
If the keepers of the upstream system balk at doing The Right Thing and fixing their code to produce a real .xls file then try renaming the ".xls" file to .htm and importing it using
DoCmd.TransferText acImportHTML, ...
I also got a 3274 error when importing a spreadsheet into Access. I have been using this macro for a while now.
The solution was to compact and repair the Access database.
I had the same problem along with another problem (office 2016 x64): Pasting from excel to access raised 'Data on the Clipboard is damaged,...' error. I found a workaround by clicking on the lower right button in paste section of home tab of access, opening the clipboard pane.
After opening Clipborad Pane as stated above, my DoCmd.TransferSpreadsheet surprisingly worked fine.
I don't know why but it may help those having the same problem and those trying to find a real solution.

VB code to print a file from a third-party application to a network drive without dialogue box

I have a specific requirement in our organization.
We are using a third-party tool to convert excel to pdf. Our excel files contain financial data and charts and the tool converts them to pdf files. Once converted, they are sent for printing.
Now I need to write a vb code which tampers with the printer configuration. After conversion to pdf, when I click on print in the tool, it should not go to default printer to print the physical file, nor should it display the dialog box asking for printer to choose, rather it should pop-up a custom dialog box indicating file being saved in the network drive. The drive and file name should be pre-configured.
I know there are quite a few solutions which caters to this problem of saving a file without displaying the default dialog box but my problem is, I am not printing files from the same application. Files are prepared by a third-party application and print option is pressed from here itself. When the file leaves for printing, I want my code to take effect. In a way, if this application is configured in a computer, no matter which tool you are using to print, the files should get saved to a network drive without popping up the default print dialogue box.
Thanks in advance. Any guidance is much appreciated.

SWT OleClientSite: How to load XML file in Excel?

I have an Excel file in OfficeML format, MyData.xls. Since I upgraded to Office 2007 from Office 2003 I get a warning message saying that the file content does not match the file extension. It seems that OfficeML now must have the extension 'xml'.
In my application I use OleClientSite to display the file in an OleFrame object. If I change the file extension to 'xml' then Excel is not started. If I leave the extension as 'xsl' then I get the above warning message.
How can I force the file with the 'xml' extension to be opened in the OleFrame using Excel?
The easiest solution is to switch back to the 2003 format, which should not require any changes to your application. To do this, open your file with the extension set to *.xls. When prompted with the warning ("... do you want to open the file now?"), proceed to open (this is a warning to make sure you don't unintentionally open a macro-enabled file). Once in Excel and the file is open, simply save it as *.xls. This can be done by going to "Office Button / Save As / Excel 97-2003 Workbook".
Now, the harder solution will be upgrading your application to deal with the new OfficeML format. I don't know about the component you're using, but it will likely still work for some of the binary parts in the new standard (most notably VBA projects), but you're going to have to unpack and start reading XML files.
If you haven't already done this, create a new Excel workbook, save it as *.xlsx (the 2007 format) and in Explorer, change its extension to *.zip. Open it up and take a look around. For more in-depth on the files, I would start digesting this MSDN article.
Maybe I'm missing something, but shouldn't you just use .xslx as your extension? I'm assuming that by OfficeML, you're refering to Office Open XML.
The <?mso-application progid="Excel.Sheet"?> should be present in the XSL template used.
The below link explains clearly how to include the processing instruction. I had to do something similar and it worked for me.
http://www.shareyourwork.org/roller/ralphsjavablog/entry/generating_excel_sheets_with_xslt

Resources