Custom content control

J

Jan Kucera

Hello,
is there any way how to provide custom content control? Like for example
selecting the item from treeview or provide any custom way of choosing data.
VSTO way acceptable.

Thanks, Jan
 
J

Jialiang Ge [MSFT]

Hello Jan,

From your post, my understanding on this issue is: you want to design a
custom control which provides customized ways to choose data. Would you let
me know whether it is a Office add-in? For instance, a Office add-in shows
a button in its command bar. When the button is clicked, a dialog is shown,
within which there are treeviews, etc and allow users to choose an item in
it. What is the source of the 'data'? Would you give a more detailed
description of your request? I am looking forward to your reply.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jan Kucera

Hello,
thank you for your reply. Yes, you understand well. I thought that there
could be a way to register your content control, which renders as normal
content control with arrow like Date Picker or Combo Box. When the users
clicks the arrow, my UI, be it either Windows Forms, WPF or at worst VBA
form, would be showed in the drop-down.
I need this, to answer your next question, particularly to be able to
query a database or remote server to get the possible data values. I can't
provide extra button (or whatever) which would insert text directly into the
document, since I need 1) to store the content data in separate data xml
file packed in the docx file 2) the user to be able later change the
selected value in otherwise locked document. Beside the UI, also mapping
between values and the xml file would have to be defined with the custom
content control.
Well if I would need to create an add-in for this functionality, good to
know, if this could be packed within a document template, that would be much
more useful for me.

I hope this gives some more insight to what I'm looking for. Feel free to
ask any details.

Jan
 
J

Jialiang Ge [MSFT]

Hello Jan,

Thanks for your reply. According to the description, you want to design a
custom content control, which shows self-designed forms, instead of the
built-in calendar control, picture control or combo control, when user
clicks on it. If I am off base, please feel free to let me know.

According to the MSDN article
http://msdn2.microsoft.com/en-us/library/bb243344.aspx, there are just
seven different types of content controls that you can add to a document:
Calendar, Building Block, Drop-Down List, Combo Box, Picture, Rich Text,
Plain Text. Therefore, I need to confirm with development team to check if
it supports custom forms. It might take some time for their confirmation.
Thanks in advance for your patience.

A possible workaround I can think of now is to utilize the
ContentControlOnEnter
(http://msdn2.microsoft.com/en-us/library/bb256768.aspx) event of Content
Control. Please check the following suggestion to see if it applies to your
scenario.

I think we could set a content control as Rich Text or Plain Text, and add
ContentControlOnEnter and ContentControlOnExit event to it:
wordDoc.ContentControlOnEnter+=new
Microsoft.Office.Interop.Word.DocumentEvents2_ContentControlOnEnterEventHand
ler(ContentControl_Enter);

Then in the event handler ContentControl_Enter, we show our form:
Form1 form = new Form1();
form.Show()
In this way, when user clicks on the content control, your self-designed
form shows and allow users to select source data.

When the user clicks select the data source and clicks OK, we set the
content control's Text and close the form.
application.ActiveDocument.ContentControls(1).Range.Text = "AAAAAAAAA"

The link http://msdn2.microsoft.com/en-us/library/bb404692.aspx shows a
video demo of how to use event in ContentControl.

Hope it helps.

If you have any other concern or need anything else, please feel free to
let me know.
Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jan Kucera

Hello.
Thanks for your reply. According to the description, you want to design a
custom content control, which shows self-designed forms, instead of the
built-in calendar control, picture control or combo control, when user
clicks on it. If I am off base, please feel free to let me know.

This is exactly what I'm trying to do.
According to the MSDN article
http://msdn2.microsoft.com/en-us/library/bb243344.aspx, there are just
seven different types of content controls that you can add to a document:
Calendar, Building Block, Drop-Down List, Combo Box, Picture, Rich Text,
Plain Text. Therefore, I need to confirm with development team to check if
it supports custom forms. It might take some time for their confirmation.
Thanks in advance for your patience.

That would be great, although I'm afraid their answer will be negative.
A possible workaround I can think of now is to utilize the
ContentControlOnEnter
(http://msdn2.microsoft.com/en-us/library/bb256768.aspx) event of Content
Control. Please check the following suggestion to see if it applies to
your
scenario.

I've watched the video and it really is very near to what I'm looking for. I
could perhaps even make do with the built-in UI in the easier cases, if I
could display one text in the document and store different string in the
data xml. Is there a way to provide custom mapping between the text and xml
data? (as I can specify stroring the date in text/xsd:date/xsd:dateTime for
example)

Thanks for your help!
Jan
 
J

Jialiang Ge [MSFT]

Hello Jan,

Below is my understanding of your mapping between the Text in the document
and the string stored in xml. If there is any misunderstanding, please feel
free to point it out. Thanks.

Suppose that the text "AAA" maps to string "111", "BBB" maps to "222". It
seems that you want to display the two items "AAA" and "BBB" in the content
control's drop-down. If users select "AAA", it will store "111" into the
specified xml file. Otherwise, store "222" when "BBB" is selected. Is it
the scenario you want to implement?

If that, I think we could capture the ContentControlOnExit event of
ContentControl, as the demo video does. In its event handler, we do the
custom mapping and store the string into xml file. For instance,
String text = application.ActiveDocument.ContentControls(1).Range.Text;
String str;
if (text == "AAA")
str = "111";
else if (text == "BBB")
str = "222";
// store str into xml
¡­ ¡­

If this is not your real request, I feel sorry, and please give me some
more detailed description of the mapping. An example is appreciated. I am
looking forward to your reply.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jan Kucera

Hello,

Jialiang Ge said:
Hello Jan,

Below is my understanding of your mapping between the Text in the document
and the string stored in xml. If there is any misunderstanding, please
feel
free to point it out. Thanks.
String text = application.ActiveDocument.ContentControls(1).Range.Text;
String str;
if (text == "AAA")
str = "111";
else if (text == "BBB")
str = "222";
// store str into xml
¡­ ¡­

Well the "//store str into xml" is what I'm trying to figure out and I
expected that the ContentControl can handle this automatically. I found the
following way of working with the thing:
ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath

Now, when you insert a Date Picker and view its properties, you see the
"Store XML contents in the following format when mapped". This is what I'm
looking for to customize. I want to convert the value in XML into value
visible to the user and vice versa, but I don't see any way to supply such
value converter with the mapping.


By the way, as we were talking about the content control custom UI, is there
a way to define my own properties to set? (like the "Display the date like
this:" one)

Jan
 
J

Jialiang Ge [MSFT]

Hello Jan,

As you said, ActiveDocument.ContentControls(1).XMLMapping.SetMapping
strXPath is able to automatically map the Text of ContentControl to the
value of a custom XML node:
Text of ContentControl ? Value of XML node.

But it seems that your request is to add a value converter between the map:
Text of ContentControl ? Value Converter ? Value of XML node.
For instance, if Value of Xml node is "111", you expect Text of
ContentControl could be "AAA" because the Value Converter converts "111" to
"AAA", and vice versa. Am I correct?

I do not think the current version of Office is able to do it with
XMLMapping automatically. But I believe we could do it manually. As
illustrated in my last reply, we could capture the ContentControlOnEnter
event to load and convert data from xml, and capture the
ContentControlOnExit event to convert and save text into xml.

Now, for your question about how to load and save the custom xml packed in
the document file (e.g. docx), here are two approaches for your reference.

Approach 1. Use CustomXMLPart object
After the custom xml is added into the ActiveDocument.CustomXMLParts
collection (http://msdn2.microsoft.com/en-us/library/aa433523.aspx), we
could get its GUID (http://msdn2.microsoft.com/en-us/library/aa433640) and
retrieve the CustomXMLPart by using CustomXMLParts.SelectByID method
(http://msdn2.microsoft.com/en-us/library/aa433526.aspx) in the next open.
CustomXMLPart object contains methods "SelectNodes"
(http://msdn2.microsoft.com/en-us/library/aa433524.aspx),
"SelectSingleNode" (http://msdn2.microsoft.com/en-us/library/aa433525.aspx)
which allows you to access the XML nodes and then get/set their values.

Approach 2. If you do not want to add the XML to CustomXMLParts collection,
but just pack the xml into docx, we could use ZipFile class of ICSharpCode
(http://www.icsharpcode.net/OpenSource/SharpZipLib) and find the entry of
the xml file. As long as we get the steam of the xml, we could use
XmlDocument class to operate on it. Please refer to the function
ReadDocumentXml() in the codeproject article:
http://www.codeproject.com/office/ExtractTextFromDOCXs.asp

As for your second question,
By the way, as we were talking about the content control custom UI, is there
a way to define my own properties to set? (like the "Display the date like
this:" one)
Although Office provide a way to customize some property of document and
application in docProps\doc.xml and docProps\app.xml if you rename the docx
as zip and extract it, the customization does not include that Property UI
base on my research.

If you have any other concerns or need anything else, please feel free to
let me know.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jan Kucera

Hello,
yes you are correct and I thank you very much for help with my task. If
you get any news about the custom forms or comments on my request, please
let me know.

I really appreciate your help. Have a nice day!
Jan Kucera
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top