Disable Controls in Word Application - c#

I have downloaded a component (Edraw Viewer Component) for word documents viewing, customization, etc...
I could not post the image since I need 10 reputation.
Please find the question on MSDN forum on the following link.
(In case the text written on image is not visible, Please find it below)
TEXT:
After selecting a word document, it seems that the EDRAW viewer control is changing the parent window of the word application and set it to an MDI contained form that opens within the main menu (check image; you could see that the MS-Word app is embedded within the windows form.)
What is really confusing, is how could they access the Copy, cut and paste buttons and disable them.
Also note that the latter buttons were disabled at the level of the document and not the entire application. (This means whenever I open another instance of MS-Word outside the menu, Copy-Cut and paste are enabled).
The software is not using CustomUI to customize the ribbon, nor VBA scripts, plus, the Word Object Model does not expose any property or method related to these buttons.
Please note that I have also tried using windows automation, accessed the word application and all of its controls (Tab, Buttons...), but the Control IDs were never accessible.
Please Find a code snippet:
Private _clientAppRootInstance As AutomationElement = Nothing
Me._clientAppRootInstance = AutomationElement.RootElement.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "Document1 - Microsoft Word"))
For Each inst as AutomationElement In Me._clientAppRootInstance.FindAll(TreeScope.Descendants, _
New OrCondition( _
New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button), _
New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Custom) _
))
If CType(inst.Current, AutomationElement.AutomationElementInformation).Name = "Copy" Then
If inst.Current.AutomationId <> "" Then
PstBtnID = inst.Current.AutomationId
End If
End If
Next
End If
At this level, please note that all inst.Current.AutomationID (which represent the ControlID, that I shall allow me accessing control handle and using windows API functions to manipulate its behavior) is always empty.
I tried this same example on windows calculator (calc.exe) and it worked like dream ( I could access each button through its controlID)
Your Help is much appreciated.
Regards.

Related

How can I disable To address bar for new created mail item?

I am working with Outlook addin which is perform task for creating new mail item and sent to users.
Now what I want is, I don't want to allow user to add email in To field, in short I need To field to remain disable for user, but I am not getting any way to disable it.
MailApp.Application oApp = new MailApp.Application();
oMailItem =(MailApp.MailItem)oApp.CreateItem(MailApp.OlItemType.olMailItem);
oMailItem.To = "abc#gg.com"; // need to disable this for end user.
oMailItem.Subject = "Xyz";
oMailItem.HTMLBody = "Xyz";
oMailItem.Display(false);
oMailItem.Actions[1].Enabled = false; // it allow index from 1 to 4 which disable reply , reply all, reply to and forward option on current mail but not To field.
Please help to achieve this in my addin. How can I disable To field for end user?
The only way to disable the To field address control is to customize the form design, remove the controls and publish that form definition to that user's Personal Forms library. For more information, see https://learn.microsoft.com/en-ca/office/vba/outlook/concepts/forms/customizing-form-pages-and-form-regions.
In addition to Eric's suggestion, you can try to hook the MailIem.PropertyChange event, and when (and if!) the "To" property is modified, reset it back.
There is no need to customize the built-in Outlook form and then re-publish it. It is a very old technology which is not used any longer by add-in developers.
You can use Outlook form regions and place the form to the Replace-all layout which replaces the whole Outlook form with the form region. Read more about that in the Create Outlook form regions article on MSDN.
Also, you may consider using Add-in Express. They provide the CompleteReplacement layout which also completely replace all pages of any Inspector window.
Note, in both cases, you can use any .net controls for creating a custom UI in Outlook

How to disable print and save options or hide tool bar of PDF viewer in WPF Windows Application

I have used WebBrowser control in xaml like below.
<WebBrowser x:Name="pdfviewer"/>
Also refer below for back end code
this.pdfviewer.Source = new Uri(#"C:\Users\VMH8COB\Desktop\UpdateRequestFormV2.pdf");
To hide the toobar I have used below code.
this.pdfviewer.Source = new Uri(#"C:\Users\VMH8COB\Desktop\UpdateRequestFormV2.pdf#toolbar=0");
It is working but if the escape key is pressed then adobe default side tool par will display. I want to disable print and save button else hide tool bar in all case.
I have searched for solution more than one week, but still I didn't get it.
Kindly help me on this.
Thanks.

Opening a browser and filling in some data

i want to know how i can open a browser to a specific web page and then fill out some of the content of the boxes on that page.
My idea is for someone to be able to order a particular item from our internal ordering system. The barcodes for these items are what will populate the fields on the page i want to open.
I no i can open a new instance of ie using Process.Start("IEXPLORE.EXE", url); howver how do i get a handle on that exact ie instance window so i can begin to add the required data to the fields?
Is this even possible?
Thanks very much
WatiN should help with this. I've generally used it for acceptance testing of web apps, but the principle is the same. Open a browser instance, reference stuff in the DOM, manipulate form elements, etc.
In addition to WatiN (as was suggested in another answer), you might consider a load testing package like Web Performance Load Tester. They have a free version that lets you run up to 10 virtual users at a time, which will perform scripted actions.
Another option would be to use a standard WebBrowser object to load your website. The WebBrowser object allows you to access and alter certain web parts. Below is sample code that automatically searches Bing:
private void LoadPage()
{
WebBrowser webBrowser1 = new WebBrowser();
webBrowser1.Navigate("http://www.bing.com");
//Wait for document to load...
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
//Set the text of the search input
HtmlElement txtTextField = webBrowser1.Document.GetElementById("sb_form_q");
txtTextField.InnerText = "My test text";
//Perform a click on the search button
HtmlElement btnSubmit = webBrowser1.Document.GetElementById("sb_form_go");
btnSubmit.InvokeMember("click");
}

Navigating to a specific fragment in a flow document from code behind

I have a WPF page used as an input form which contains a number of controls on one side, and a flow document reader on the other.
I want to set the content of this document reader to a specific part of a flow document which is loaded when the form is loaded, (via a loaded event).
I have found an article explaining how to do this, using fragments, but the examples shown are only expressed in XAML.
In my case I need to update the document property of the flow document reader when the user gives focus to one the controls (I have wired up the events already) therefore I need to do this in the code behind rather than XAML.
I have tried setting the document property to:
Document#Control_Sport
where Document is the name of XAML flow document and Control_Sport is the name of the fragment I need to navigate to.
However this gives an error, it doesn't like the hash sign being there.
I tried looking on MSDN but its XAML only. Is there a way I can do this through code?
Any help would be appreciated.
Felix,
Link to MSDN article: http://msdn.microsoft.com/en-us/library/ms750478.aspx#FragmentNavigation
You can navigate to any Block within a FlowDocument by calling Block.BringIntoView.
First, create a Frame object inside your Page or Window object. Setting the JournalOwnership property to "OwnsJournal" will give the document its own navigation bar (forward and back arrows plus a history). You will probably need to add additional parameters to size and locate the frame within your document as well, but I didn't include them in my example since I don't know what your app requires:
<Frame Name="MyFrame" JournalOwnership="OwnsJournal" />
Then, create a pack URI for your document fragment. This document is assumed to be in the same directory as the application's executable; you will need to add more to the path to navigate to the directory where the document resides in your project:
Uri MyUri = new Uri("pack://application:,,,/MyXamlDocument.xaml#MyFragment");
Then, navigate to it from inside your button's Click handler or whatever other means you like to initiate the navigation:
MyFrame.Navigate(MyUri);

Broken tables in RichTextBox control (word wrap) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t the richtextbox displaying this table properly?
We are having problems with the Windows.Forms.RichTextBox control in Visual Studio 2008.
We are trying to display text supplied as an RTF file by a 3rd party in a windows forms application (.NET 3.5). In this RTF text file there are tables, which contain text that spans multiple lines. The RTF file displays correctly when opened with either WordPad or Word 2003.
However, when we load the RTF file into the RichTextBox control, or copy & paste the whole text (including the table) into the control, the table does not display correctly - the cells are only single line, without wrapping.
Here are links to images showing the exact problem:
Correctly displayed in WordPad
Incorrectly displayed in RichTextBox control
I have googled for solutions and 3rd party .net RTF controls without success. I have found this exact problem asked on another forum without an answer (in fact that's where the link to the images come from) so I'm hoping stack overflow does better ;-)
My preferred solution would be to use code or a 3rd party control that can correctly render the RTF. However, I suspect the problem is that the RichTextBox control only supports a subset of the full RTF spec, so another option would be to modify the RTF directly to remove the unsupported control codes or otherwise fix the RTF file itself (in which case any information as to what control codes need to be removed or modified would be a huge help).
The Rich Text box from .NET is extremely buggy.
In RTF, the way a table is defined is actually quite different from what you could expect if you are used to HTML.
HTML:
<table>
<tr>
<td>Mycell</td>
</tr>
</table>
In RTF, a table is simply a series of paragraphs with control words defining rows, cells, borders. There is no group tag for the start/end of a table.
RTF:
\trowd\trgraph \cellx1000 Mycell \cell\row\pard\par
If you want to add a paragraph inside a cell, you use \par and the control \intbl (in table) to indicate the paragraph is inside the table.
.NET RTB can handle only a very small subset of RTF control words and doesn't support the vast majority of available commands. By the looks of things, \intbl is part of the long long list of control words it doesn't support, and if it actually parses \par at that point, the display is trashed.
Unfortunately, I don't have a solution for that but I hope the small explanation above helps you make some sense of the problem.
Don't put too much faith on my RTF sample. It works, but it's absolutely bare-bones. You can download the RTF specifications from Microsoft's website:
Word 2007 RTF specs.
Can you use the old COM control instead of the new .NET control, or do you require a "pure" .NET solution?
In other words, go into the Visual Studio toolbox, right click, choose "Choose Items", look in the COM Components tab and check Microsoft Rich Textbox Control 6.0.
Answering my own question here, but only due to the help from Joel and sylverdrag...
The short answer is that both the .Net and underlying COM RichTextBox do not support word wrap in tables. I ended up knocking up a test application and using both the COM and .Net RichTextBox controls and they both exhibited the same (broken) behaviour.
I also downloaded the RTF spec from the link supplied by sylverdrag and after tinkering with hand-made RTF documents in MS Word and RichTextEdit controls, I can confirm that TichTextBox does not correctly support the \intbl control word - which is required for word wrap in tables.
There appear to be three possible solutions:
Use TX Text Control. I have confirmed this works using a trial version but it is expensive - prices start at US$549 per developer.
Use an embedded MS Word instance as discussed on Code Project. Note that the code example provided on Code Project didn't work out of the box but I did get it working with Office 2003 & VS 2008. After much mucking around we hit an unexpected show stopper - we want the document to be read-only so we Protect() the document. While this works, when a user tries to edit the document the MS Word "Protect Document" side bar pops out from the right hand side of the control. We can't live with this and I was not able to turn it off (and from googling it looks like I'm not alone).
Give up on RTF and use HTML instead and then render the document in a WebBrowser control instead of a RichTextEdit control. That is the option we are taking as it turns out the source document is available in either format.
Step 1, Use the old COM Microsoft Rich Textbox Control 6.0;
Step 2, Make a copy of Windows\System32\MsftEdit.dll and then rename it to riched20.dll;
Step 3, Copy riched20.dll to your app folder such as bin\bebug.
This works fine, table displays correctly.
Wordpad is generally a very thin wrapper over the rich edit control, so if it appears properly there then Windows should be able to handle it.
Perhaps you're instantiating the wrong version of the rich edit control? There have been many, and Windows continues to supply the older ones for backwards compatibility. http://msdn.microsoft.com/en-us/library/bb787873(VS.85).aspx
Just create a new Control. It works fine for me.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class RichTextBox5 : RichTextBox {
private static IntPtr moduleHandle;
protected override CreateParams CreateParams {
get {
if (moduleHandle == IntPtr.Zero) {
moduleHandle = LoadLibrary("msftedit.dll");
if ((long)moduleHandle < 0x20) throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");
}
CreateParams createParams = base.CreateParams;
createParams.ClassName = "RichEdit50W";
if (this.Multiline) {
if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) && !base.WordWrap) {
createParams.Style |= 0x100000;
if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
createParams.Style |= 0x2000;
}
}
if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None) {
createParams.Style |= 0x200000;
if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
createParams.Style |= 0x2000;
}
}
}
if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0)) {
createParams.Style &= -8388609;
createParams.ExStyle |= 0x200;
}
return createParams;
}
}
// P/Invoke declarations
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string path);
}
This is not a issue of RitchText Control provided in .net . some Ritchtext rules (Ritchtext Synatax) has been changed in new version of Ms-office (2007). however the component used in .net cannot update to cater the new rules so the issue occours.
Anand

Categories