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
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 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.
I have a template that contains several macros, this template will be distributed on several devices.
I have the following macros to insert a text using quick parts of ms word.
Application.Templates( _
"C:\Users\user_name\AppData\Roaming\Microsoft\Word\STARTUP\template_name.dotm" _
).BuildingBlockEntries("alphabet").Insert Where:=Selection.Range, _
RichText:=True
ActiveDocument.TrackRevisions = Not ActiveDocument.TrackRevisions
The issue is, the code contains the absolute path to the template, which won't be the same on different machines.
I have tried using %Appdata% instead, but there macro did nothing, with no error messages.
is there any way around this ?
Thank you
You can use Environmental Variables in your VBA code to get the 'UserName' i.e.
"C:\Users\" & LCase(Environ("UserName")) & "\AppData\Roaming\.."
Here is one link to show other variables (or just search for 'VBA using environmental variables' (without quotes: https://www.wiseowl.co.uk/blog/s387/environment-variable-vba.htm
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.
Has anyone had problems inserting a PNG file into a word document, using a VBA Macro?
I have an MS Word document that contains a very large directory listing of image files, inside a table. I've been asked to update the document by inserting the corresponding image in front of the name.
Now, if I enter the image manually (using Insert|Image|From File), I'm able to successfully place the PNG image ... so I decided to write a quick VBA Macro to insert the image for me. The following is a sample of the code:
Dim myFile As String
Selection.SelectCell
Selection.Copy
myFile = _
Chr(34) & "C:\Documents and Settings\...\Project\Images\" _
& Left(Selection.Text, Len(Selection.Text) - 2) & Chr(34)
Selection.InlineShapes.AddPicture _
FileName:=myFile, LinkToFile:=False, SaveWithDocument:=True
Outcomes:
Whenever I execute the macro, I get the "Unable to Convert" error dialog, and no image is inserted.
I even changed the code to invoke the wdDialogInsertPicture Dialog instead, and it worked just fine.
This is very confusing ... using a manual process, the insert works, but going with an automated solution, the insert doesn't work!
Any ideas or suggestions?
I've tried the macro several times and it works ... it seems that I'm no longer able to re-create the error again. So, I'm going to mark this under the "mysteries of Office VBA" column and leave it as is ... this is not a high-priority project, so there is no need for me to continue investigating.
Thanks to Alain and Joel Spolsky for their help.