I am trying to save a Word template in .pdf format. I need to print the following range (1-3,18). The problem is that using "ExportAsFixedFormat2" does not allow me to select that range or at least I dont know how:
wdapp.ActiveDocument.ExportAsFixedFormat2 OutputFileName:=savenamepdf, ExportFormat:=wdExportFormatPDF, Range:=wdExportFromTo, From:=1, To:=3
If I use "printout" I can select the range and create the pdf file, but when I try to open it I get an error message like it is broken or corrupted:
wdapp.ActiveDocument.PrintOut OutputFileName:=savenamepdf,_ Range:=wdPrintRangeOfPages, Copies:=1, Pages:="1-3,18"
Anyone knows how to print that range?
here is the whole code block
Select Case cell
Case 100
With wdApp
.ActiveDocument.SaveAs2Filename:=savenameword,ReadOnlyRecommended:=False
.ActiveDocument.ExportAsFixedFormat2OutputFileName:=savenamepdf,ExportFormat:=wdExportFormatPDF,Range:=wdExportFromTo, From:=1, To:=4
.ActiveDocument.Close
End With
Case 189
With wdApp
.ActiveDocument.SaveAs2 savenameword
.ActiveDocument.PrintOut OutputFileName:=savenamepdf,Range:=wdPrintRangeOfPages, Copies:=1, Pages:="1-3,18"
.ActiveDocument.Close
End With
End Select
What you want is not supported. The only way around it would be to bring together the content into a different/new document, then save that to PDF.
Or, a copy of the document can be opened in Word, the material that's no wanted deleted, then save the document as PDF.
The PrintOut method to a file generates a text file that can later be sent to the printer. It's in a special format and the Word content is in a binary format. It's not a converter to other file formats, even if you append a pdf extension to the file name, the content won't be PDF.
For more information about what printing to a file is used for:
https://answers.microsoft.com/en-us/windows/forum/windows_vista-windows_programs/what-does-print-to-file-mean/2e73491c-634f-4067-8b7e-c158f647129d
https://word.tips.net/T000462_Printing_to_a_File.html
http://ask-leo.com/what_is_print_to_file_used_for.html
I have experienced similar issues.
Before calling PrintOut set the wdApp.ActivePrinter to "Microsoft Print to PDF".
wdApp.ActivePrinter = "Microsoft Print to PDF"
If your default/application printer is some other printer (even cutepdf or 7-pdf, etc) the resulting pdf is likely to be corrupted.
I tried the same, and couldn't find a way based on word alone.
What I do now is to save the whole document to a pdf file, then extract the pages I need with pdftk. Of course you mast have pdftk installed.
It can be automated in a VBA macro, thus:
ActiveDocument.ExportAsFixedFormat OutputFileName:="tempfile.pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportFromTo, From:=page1, To:=99999, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=True
With ActiveWindow.View
.MarkupMode = wdInLineRevisions
.RevisionsFilter.Markup = wdRevisionsMarkupAll
.RevisionsFilter.View = wdRevisionsViewFinal
End With
commandstring = "pdftk tempfile.pdf cat " & pagerange & " output outfile.pdf"
StatusBar = commandstring
SynchronousShell (commandstring)
Kill "tempfile.pdf"
the variable "pagerange" contains the desired page range following pdftk conventions.
"Item" can be set to 'wdExportDocumentWithMarkup' if you want. ( I often need to do that )
The code works for me but may not be suitable for production. For one thing, it does not check contention on the temporary file.
Related
I am attempting to speed up insertion of pdf's as embedded files into a Word document.
In MS Word, from the insert object dialog, it is possible to select 'package' as an option. Doing this avoids the automatic association of the inserted file with a particular application, which is required, as not all users of this document have the same PDF reader installed.
After manually inserting a file that does not have associations, I have switched to field codes to view the class type of the inserted package, the field code reads: { EMBED Package }
So I have tried the following:
Application.Selection.InlineShapes.AddOLEObject _
ClassType:="Package", _
FileName:=FiletoInsert, _
LinkToFile:=False, _
DisplayAsIcon:=True, _
Range:=Application.Selection.Range.Next(Unit:=wdCell, Count:=1)
But the embedded file resulting from this is still associated with Foxit or Adobe applications.
On comment suggestion, I've tried the macro recorder to check the parameters input by the application when using that approach, output in Macro:
Selection.InlineShapes.AddOLEObject ClassType:="Package", FileName:="", _
LinkToFile:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\WINDOWS\system32\packager.dll", IconIndex:=0, IconLabel:="Package"
I have tried changes based on this, but still including the FileName of the file to be inserted, but the application association remains.
How do I go about embedding these files without the association with an application, so that any user can open the pdf?
I'd like to be able to programmatically select the double sided button in MS Word 2013 printpreview in backoffice view via vba.
I am unable to get the PCL6 code to work with our sharp MFC. Some documents I want to have it print double sided by default but not on all documents. I can't find the ExecuteMso button for this as the backoffice view doesn't seem to be accessible via vba code.
Perhaps using WinAPI would work or sendkeys but I think that's messy and unreliable.
When you record a macro and print using the duplex options, it does not capture duplex in the dialog.
This is the macro that was generated when I double-sided a test document:
ActivePrinter = "\\MyServer\MyHPPrinter"
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
During the recording of the Macro, the document printed duplex. When I closed/reopened the document and ran the Macro, it printed one-sided. Using Word from Office 365 and printing to HP Laser Jet9050dn. I also tried adding , ManualDuplexPrint:=False after wdPrintAllPages. This did nothing for me.
If this was formatted incorrectly, please forgive me.... this is my first post here.
You can control printing on one side or both sides with an option called: ManualDuplexPrint. Set it to False and it will print both sided.
Let's say that you want to print the whole document both sided. You would write:
Sub test()
ThisDocument.PrintOut Range:=wdPrintAllDocument, ManualDuplexPrint:=False
End Sub
Please don't forget to hit the check mark next to the question if this is the answer! :)
I Found a workaround for duplex Printing. Word doesn't support the option for Automatic duplex printing rather it is printer specific which loads the options if a printer supports it.
Install a copy version of the printer driver on your pc. I found great help for this from here.
Follow the procedure and install the driver. Rename it (Use Duplex for easy understanding).
Go to printing preference and set the duplex setting as default.
Now make that printer active in your VBA code.
ActivePrinter = "Brother DCP Duplex"
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentWithMarkup, Copies:=1, Pages:="s1", PageType:= _
wdPrintAllPages, Collate:=True, Background:=True, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
No need for ManualDuplexPrint:=False.
I tried it myself and works perfectly.
Happy Coding.
I have written a small macro that takes an daily Excel report and prints it to a specific printer (printing to PDF). When I run the macro, I am still missing the final steps. Running it as is, I still need to click the "save" button that pops up, and have to navigate to the correct file path. Is there a way to have it automatically hit the save button for me, and save the file into the correct folder (as seen in the code below)?
Sub printToPDF()
'declare variable for my file path
Dim filePath As String
'declare variable for my file name
Dim fileName As String
fileName = "Operations_Daily_Outage_Report_" & Format(Date, "yyyy-mm-dd")
filePath = "M:\Daily_Outage_Report\Active"
Worksheets("general_report").PageSetup.CenterVertically = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Foxit Reader PDF Printer"
End Sub
I think everyone is trying to give you answers that you can try out. I'm not sure why you can't just test it and tell us if it works for you?
If you have a reasonably new version of Access (within last 10 years), then you should be able to use the built-in Office PDF converter
Change this line:
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Foxit Reader PDF Printer"
To This:
Worksheets("general_report").ExportAsFixedFormat Type:=xlTypePDF _
FileName:=filePath & "\" & fileName Quality:=xlQualityStandard
Come back and tell us if it worked for you.
I am using MS Word to edit text that I convert to structured HTML using VBA.
The text is written out using document.saveas2 with encoding:=msoEncodingUTF8.
Today I found that the Trademark Symbol [Edit: inserted using the Insert Symbol capability; Insert Tab, Symbols group, Symbol button] was appearing in the text files as "(tm)".
Having discovered that encoding:=65001 should also produce UTF8, I tried it - and in one case it seemed to work, but the result was not reproducible.
I also learned that being older than Unicode, Word might use a private code page for certain characters, so I also entered the unicode code directly followed by alt-X; the TM symbol appeared correctly but still failed to be written to the text file.
Whilst I have been able to work around the problem by replacing TM with the HTML "& trade ;" (extra spaces to prevent it getting rendered as the symbol!), I am concerned about the potential for other encoding failures.
Can anyone shed any light on the cause(s) of this issue or offer an effective resolution/mitigation?
System config: Word 2010; Windows 7 64 bit.
I recorded a macro to save some some Chinese text that is clearly unsupported in the default code page on my system, which was Windows-1252. I saved in .txt format and it asked for the encoding, which I selected UTF-8. Here is the result:
ActiveDocument.SaveAs2 FileName:="The.txt", FileFormat:=wdFormatText, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=65001, InsertLineBreaks:=False, AllowSubstitutions:= _
False, LineEnding:=wdCRLF, CompatibilityMode:=0
It did save the file correctly in UTF-8. I edited the macro down to the following minimal code and it still worked.
ActiveDocument.SaveAs2 FileName:="test.txt", FileFormat:=wdFormatText, _
AllowSubstitutions:=False, Encoding:=65001
I wanted to save in filtered html utf8, so I tried:
doc.SaveAs2 FileName:="file1.htm", FileFormat:=wdFormatFilteredHTML, AllowSubstitutions:=False, Encoding = msoEncodingUTF8
I found that although with FileFormat:=wdFormatText the file did save in utf8, doing the same with wdFormatFilteredHTML did not.
What did work was
doc.WebOptions.Encoding = msoEncodingUTF8
doc.SaveAs2 FileName:="file1.htm", FileFormat:=wdFormatFilteredHTML, AllowSubstitutions:=False
I created 300+ page Visio drawing by importing data to create 300+ "org charts".
I say "org charts' bc they are actually drawings of a Crystal Report as 'CEO' and its datasource tables and fields as "employees".
I now have one huge Visio file with 300+ pages, each page is one report and its datasources. But my goal is to get 300+ separate documents each named appropriately. Using vba i can rename each page as desired or obtain desired name from page.
Ideally i want to have pdf files, not visio files but in pinch would do, and last desirable would be as jpg/gif.
I have tried:
exporting/saving each page as jpg/gif/html but get 920 error.. i am totally stumped after exhaustive search on how to modify resolution for each page to avoid 920 error, i mean what is the 'perfect' resolution required? very vague documentation available.
Sub ExportPagesAsFiles()
Dim PagsObj As Visio.Pages
Dim PagObj As Visio.Page
Dim ExportName As String
Dim ExportPath As String
Set PagsObj = ActiveDocument.Pages
'Open "C:\temp\exportLog.txt" For Output Shared As #1
For Each PagObj In PagsObj
ExportPath = "c:\Report_Visio\"
ExportName = ExportPath & PagObj.Name & ".jpg"
' ".gif"
' ".wmf"
' ".html"
'Print #1, ExportName
PagObj.Export ExportName
Next PagObj
'Close #1
End Sub
printing to pdf printer but hit pdf software dialogue (CutePDF). I have no admin rights to modify system registry and I am on Windows 7 machine and sendkeys is outlawed.
vba to create new doc, copy/paste page drawing into it, name new doc, save it with name. But some Visio quirk only copy paste page objects but not the captions and data behind them so they are blank shapes. Copying entire page contents is not clear to me after exhaustive search.
vba to save copy of 300+ doc with name of first page, then delete rest of pages. Open original 2nd time, save as second page, then delete rest of pages, and repeat 300+ times. I quit this after 10 or so pages and 3 hours.
Seems every possible solution path hits pitfalls, swamps, cliffs ..
As a general note, there is either very spartan, or obtusely technical, Visio information available on internet. With any other Office product, there are loads of info, examples, etc in forums etc. But Visio its crickets.
So wonder if any Visio developers can help me choose and navigate these solution paths!
Thanks.
Edit to add: I am using Visio Standard 2003 version
Edit to add Visio export to file code used
You can try to use PDFCreator (its open source).
It's a virtual printer, that prints to PDF, JPG, BMP, PNG (very useful for charts) and many other formats.
It has auto save option, configurable filenames and it can save printed document pages as separate files.
Some screenshots from application that configures virtual printer (this is PDFCreator 0.9.6).
Printing each page to separate file
Auto-save options:
I've decided to take a different approach.
I used VBA and Access to create each Visio page separately, instead of creating a 300+ page Visio doc. Visio's Org Chart import data wizard is powerful but leaves one stuck for options to get individual pages out, as I outlined above.
So, in Access, I looped through 300+ report records, selecting each report's data, one at a time, creating Visio drawing for that report, naming Visio file as report name and then saving and closing it.
Then I ended up with 300+ Visio files, each one showing a report's datasource tables and fields. Good enough.
Using VBA to create chart from data 'orgwiz' http://office.microsoft.com/en-ca/visio-help/make-visio-organization-charts-from-personnel-files-HA001077464.aspx
The code is
Set objVisio = CreateObject("Visio.Application")
Set objAddOn = objVisio.Addons.ItemU("OrgCWiz")
strCommand = "/DATASOURCE=c:\temp\MyDatabase.mdb, " _
& " TABLE=MyVisioDataSource, " _
& " DBQUALIFIER=Microsoft.Jet.OLEDB.4.0 " _
& " /NAME-FIELD=Data_Object_Name " _
& " /UNIQUEID-FIELD=Data_Object_ID " _
& " /MANAGER-FIELD=Data_Object_Parent_ID " _
& " /DISPLAY-FIELDS=" & strDisplayFields _
& " /CUSTOM-PROPERTY-FIELDS=" & strPropertyFields _
& " /SYNC-ACROSS-PAGES " _
& " /HYPERLINK-ACROSS-PAGES " _
& " /SHAPE-FIELD=MASTER_SHAPE " _
& " /PAGES=" & strReportName
objAddOn.Run ("/S-INIT")
Dim cmdArray, i
cmdArray = Split(strCommand, "/")
For i = LBound(cmdArray) To UBound(cmdArray)
objAddOn.Run ("/S-ARGSTR /" + cmdArray(i))
Next
objAddOn.Run ("/S-RUN ")