Word Interop (COM) Proofing Options - vb.net

I have documents that do not retain their proofing options.
I have some code that now ensures all styles are set to the correct language and has proofing turned on:
For Each s As Style In oDocument.Styles
Select Case s.Type
Case WdStyleType.wdStyleTypeCharacter, WdStyleType.wdStyleTypeParagraph, WdStyleType.wdStyleTypeTable
s.NoProofing = False
s.LanguageID = WdLanguageID.wdEnglishUK
End Select
Next
If I check the proofing options in the document without selecting any text, the following is displayed:
Proofing Options, No Text Selected
If I check the proofing options in the document with all text selected, the following is displayed: Proofing Options, All Text Selected
I have tried the following code snippet, but it did not work:
For Each range As Range In oDocument.Words
range.LanguageID = WdLanguageID.wdEnglishUK
range.NoProofing = False
range.LanguageDetected = False
Next

The behavior described in the question indicates mixed language formatting is present in the document.
Firstly, language formatting can be applied directly (similar to font formatting) rather than being controlled solely by styles. The language defined in a style will not override direct language formatting.
Additionally, "odd" behavior with language formatting often occurs when a document was created on a machine where the OS default language is different from Word's default language. The problem began with Office 97, where Microsoft changed Word's default behavior and has it check the Windows language and apply it to a new document if Word's default language is not identical. The information is written deeply into the document structure, where the Word UI is not able to change it. It can only be removed by editing the underlying Word Open XML.
Due to massive complaints by professionals who use Word, the behavior was somewhat alleviated in later versions to the point where selecting the entire body of the document (Ctrl+A) then applying the desired language directly could mostly prevent newly added text from being incorrectly formatted with the Windows language.
If manually selecting the entire document and applying the language directly does not change the behavior (object model equivalent Document.Content.LanguageID =), the only recourse would be to delve into the document's "zip package", inspect the XML to track down where the other language is stored, and remove it. This information can be used to create a solution with the Open XML SDK (or using standard Packaging and XML namespaces in the .NET Framework) so that it can be used on "any" document (YMMV)
Note: I have done this manually, but it was some time ago, so I don't remember exactly in which XML file in the zip package the Windows language is written.

Related

Make user defined language in Notepad++ work regardless of theme

I have imported a Less style that a developer had recommended on this website, but there is a slight problem - it is made for specific themes only.
Without editing any XML files, is there any way to get this language file working regardless of theming? Like the built-in languages.
Language file working regardless of the theme – as far as I know the answer is no. Also check here.
If you do not want to edit XML files, you can still change colors by selecting Define your language... item in menu Language and change the colors of syntax categories using color pickers.
Adding your language into list of built-in languages is not easily possible unless you wish to play with N++ sources. You are supposed to use user-defined languages for standard tasks. However you can:
disable all unnecessarly languages in Language menu section in Preferences window and uncheck the Make language menu compact so you will get flat list of languages in menu.
access your language menu item faster by pressing Alt+L, then 2×Up and Enter

How do I style a word document exported from a webpage in VB.Net

I'm trying to export text retrieved from a database into a word document in VB.Net and while I have a working example, I need to figure out how to style some sections of the document appropriately.
I have found a few working examples from MS Online resources (such as this one), which I've found can cover some basics:
para.Range.Text = "Quad Chart"
para.Range.Style = "Heading 1"
para.Range.Bold.Font = True
But it doesn't cover even some of the simplest of formatting such as:
How you align the text (left, right, center)?
How you specify letting?
How do you start a list style?
What I'm trying to find is either a straight answer to these or (even better), a definitive list of the commands that would allow most any formatting.
Also, I would prefer not using Spire, which seems to be a common answer.
Thanks!
The VBA object model describes all the classes, their methods and properties that you can use for the marking up of content.
Your suggestion to use styles is strongly recommended as a way of separating your code from the presentation. Create a document template (.dot or .dotx, depending on Word version) and attach this to your documents. Then, when the document is opened, it will inherit layout and presentation from the template and be correctly rendered.
The list creation is a little intricate as you will need to restart the list if you are using numbering.
If you are interested in a completely different approach, you can look at Applying an XSLT Transform in the Microsoft Office Word 2003 XML Software Development Kit. This describes how to generate XML documents and using XSL transforms to describe the presentation. More general, but definitely more complex to set up.
Your preferred approach will depend on whether you want to generate native documents with a template, or to require your users to install the transform using the tools in the SDK.
So, you have a few examples. Office VBA is a cut down version of VB6, so why not record some macros in Word, open the VB editor and look at what it does. It's also the easiest way to navigate the help on the Word object model.

How to create switchable multi-language pdf form?

I want to create a pdf form for two language (Chinese/English) UI, and there's a button(s) or somethings on the form for language switch, is there anyway can make it? and how to do?
thanks!
Thanks for all reply!
Actually I got a sample like this,
PDF Sample
there're two checkbox on the top-left of the form, one is for English UI, the other is Chinese, I just want to know how to make PDF like that sample? (and I don't see any layers on the sample...)
thx
mkl's comment (which he should turn into a full answer, really) already hinted at the option to use different page templates residing in the same file.
Another option you could explore is this:
put the two language versions into 2 different layers (or 'optional content groups' in PDF parlance)
make the visibility of the two layers toggeable
let the user activate that layer which he/she needs.
Layer activation can be handled through normal Acrobat Reader user interface elements.
The layer switching can be made accessible via a "button" on the PDF page too -- but that requires additional JavaScript to be embedded in the PDF (something many people are not particularly keen about).
As Kurt proposed, I make my comment on Frank's answer an answer in its own right:
Actually there is a pdf feature seldomly used nowerdays, page
templates. Thus, those two forms can reside in the same file in
different page templates, and based on some initially present buttons
("English version", ...) the desired form is spawned.
Unfortunately I don't know how to create page templates using some easy-to-use tool, I only came a cross them in the context of integrated PDF signatures (depending on the signature type, page template instantiation is a document change not breaking the signature) and tested them with low-level tools.
Essentially page templates are PDF objects just like page dictionaries of the normal pages, they are not XFA stuff. They merely are not referenced in the pages tree but instead in the name tree.
There is a JavaScript command which creates a visible page based on such a template --- I don't know which anymore; I may be able to find out when I'm back in office next week. This command would have to be bound to the inital language selection button in the file.
The problem will be in switching the static text - PDF does not allow this.
If I were you, I would split the document into two identical forms in the respective languages. You can use bookmarks and links on the first page to navigate to the right part of the document.
Note that it is possible to assign the same field names to the Enlgish/Chinese versions of your fields. This will make it easier to process the submitted form data because the process path would be independent of the chosen language. It will also simplify any JavaScript (validation, summing, etc.) you plan to add.

Detecting tables and images in word document using office interop

I am iterating through paragraphs in a word document using word interop API. So far i did not have a problem detecting different headings by using the style object. However now i have a situation that contents inside a table have the same style as those outside it. I need to figure out a way to understand when the paragraph in question is actually a table.
I have similar need to figure out when paragraph is actually an embedded image.
When i physically select a table or image in the word document i can see that tools section above format changes. When an image is selected it is "Picture tools" when a table is selected it is 'Table tools" and when a normal paragraph is selected the tools section does not show.
How can i detect this behavior using word interop API?
Thanks
Sameer
Though this post seems to be old, I came across this while searching for a similar problem while working on Office automation, hope this post will help to investigate and expand further.
While looping through paragraphs of a word document,
Paragraph.Range.Tables.Count provides a value indicating if the paragraph is inside a table or not.
Paragraph Outside table : Paragraph.Range.Tables.Count = 0
Paragraph Inside table : Paragraph.Range.Tables.Count = 1 ( or above not checked )
To get end of table (last paragraph inside table)
: Paragraph.Next().Range.Tables.Count == 0
(The above logic applies while using NetOffice assembly, which in-turn uses interop assemblies,hope this is applicable directly to word interop assembly also)

How to Pretty print VBA code?

How do you copy VBA code into a Word document and retain the VBA editor color scheme?
You can use Notepad++ to accomplish this in three ways. Just so you know, Notepad++ is a more advanced version of Notepad, which supports syntax highlighting of different code files "out of the box" - Visual Basic included!
Download & install it, fire it up, and load up your VBA code. You should automatically see it beautifully coloured (if not, because the file extension is something other than .vb, go to Language -> VB or Language -> V -> VB).
If you need to change any of the colours, you can easily do so - just go to Settings -> Styler Configurator. From that menu, you can change the various highlighting and font options, to suit your needs - although the default usually suffices for most.
Then, go to Plugins -> NppExport. From there, you have three options you can consider:
Directly print from Notepad++
Copy all formats to clipboard
Export to RTF
Export to HTML
The first is self explanatory. The second one - "Copy all formats to clipboard" - will copy the entire file with the highlighted syntax to the clipboard. Once you click it, then open Microsoft Word or your other favourite document editor, and just hit paste! You should see the beautifully syntax-highlighted code. If something goes wrong, then you can try one of the other options (export to RTF/HTML), although I've never had a problem with the clipboard method.
There are two programs on cnet downloads, which are free to try. In case you did not try them, here are the links:
VB-VBA Code Formatter & Printer 2.2
VBAcodePrint 6.13.110
Smart Indenter could be what you're looking for? You'd load the result into Notepad++, set language to VB and save as .rtf/.doc (or print to file, can't remember off the top of my head).
For a more modern approach, Sublime Text users can install ExportHTML from Package Control. This has the added benefit of being able to include the line numbers, and changing the code coloration.
HTML files can be opened directly within Word.
Highlight supports a wide range of Operating Systems and 150+ languages including Visual Basic, although I'm not sure about VBA.
I would recommend this one: planetB
It works well with Internet Explorer (didn't work wit Firefox for me, not sure abt Chrome).
Plus it's online, so no need to install anything (the case of Company/University PC's)
Regards
Wheeliam
The following works with Visual Studio Code:
Start VS Code.
New file.
Copy & Paste the VBA code into the VS Code window.
In the lower right-hand corner, click on "Plain Text" and select "Visual Basic" instead. Note that the code is now syntax-highlighted.
Copy & Paste the formatted code into Word.