How do I set the format of a ContentControl to just use the formatting of the para it is in?

J

Ji Zhou

Hello Dave,

Based on my understanding, we are setting ContentControl.DefaultTextStyle to
change the format of the content control, right?

In my local test, if I set this property DefaultTextStyle, it affects the
content control text, but not the placeholdertext.

For example, I call the following codes in my side,
-----------------------------
ActiveDocument.ContentControls(1).DefaultTextStyle =
ActiveDocument.Styles("Heading 2")
-----------------------------

After the codes executed, the whole text in content control uses the text
format in "Heading 2" style. But the placeholdertext remain its format.

If we want to set the content control to use the inner para formatting. We
first need to get the format of the inner content. We can retrieve it from
the ContentControl.Range.Font.

After that, we create a new style, and set the new created style font to the
range.Font. Then we can set the DefaultTextStyle to the new created style.


VBA codes like,
--------------------------
Sub Test()
Dim myStyle As Style
Set myStyle = ActiveDocument.Styles.Add(Name:="MyStyle")
myStyle.Font = ActiveDocument.ContentControls(1).Range.Font
ActiveDocument.ContentControls(1).DefaultTextStyle = myStyle
End Sub
 
D

David Thielen

Hi;

I think what you are saying is do the following:
Range rng = // place to insert control
ContentControl ctrl =
rng.ContentControls.Add(WdContentControlType.wdContentControlText, ref
obj);
ctrl.SetPlaceholderText(null, null,
tag.Tag.toDisplay(OptionsServer.IntDisplaySelect));
object style = rng.CharacterStyle;
ctrl.Range.set_Style(ref style);

That does set the character style to the character style of the text
before it. But it does NOT match the formatting if the text before has
formatting that overrides the style.

For example, it is set to "Default Character Style" - which is black.
So the placeholder text is black. But if I set the text in the
paragraph to be red, which is an override, the placeholder text is
still black.

What I want it to do is completely match the character formatting of
the text preceeding it. Is there a way to do that? Hopefully other
than property by property setting every character formatting property.

thanks - dave


Hello Dave,

Based on my understanding, we are setting ContentControl.DefaultTextStyle to
change the format of the content control, right?

In my local test, if I set this property DefaultTextStyle, it affects the
content control text, but not the placeholdertext.

For example, I call the following codes in my side,
-----------------------------
ActiveDocument.ContentControls(1).DefaultTextStyle =
ActiveDocument.Styles("Heading 2")
-----------------------------

After the codes executed, the whole text in content control uses the text
format in "Heading 2" style. But the placeholdertext remain its format.

If we want to set the content control to use the inner para formatting. We
first need to get the format of the inner content. We can retrieve it from
the ContentControl.Range.Font.

After that, we create a new style, and set the new created style font to the
range.Font. Then we can set the DefaultTextStyle to the new created style.


VBA codes like,
--------------------------
Sub Test()
Dim myStyle As Style
Set myStyle = ActiveDocument.Styles.Add(Name:="MyStyle")
myStyle.Font = ActiveDocument.ContentControls(1).Range.Font
ActiveDocument.ContentControls(1).DefaultTextStyle = myStyle
End Sub
--------------------------

Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

Hi again;

And if I want to get the formatting of the character just before the
ContentControl, do I create a Range where Start = End = the
ContentControl.Start? Or do I then have to subtract 1?

thanks - dave


Hi;

I think what you are saying is do the following:
Range rng = // place to insert control
ContentControl ctrl =
rng.ContentControls.Add(WdContentControlType.wdContentControlText, ref
obj);
ctrl.SetPlaceholderText(null, null,
tag.Tag.toDisplay(OptionsServer.IntDisplaySelect));
object style = rng.CharacterStyle;
ctrl.Range.set_Style(ref style);

That does set the character style to the character style of the text
before it. But it does NOT match the formatting if the text before has
formatting that overrides the style.

For example, it is set to "Default Character Style" - which is black.
So the placeholder text is black. But if I set the text in the
paragraph to be red, which is an override, the placeholder text is
still black.

What I want it to do is completely match the character formatting of
the text preceeding it. Is there a way to do that? Hopefully other
than property by property setting every character formatting property.

thanks - dave





david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
C

Colbert Zhou [MSFT]

Hi Dave,

We need to substract 1 from the ContentControl.Range.Start.

Codes look like,
 
D

David Thielen

Thank you. Also, is there an easy way to copy all the character
formatting across to the content control?

thanks - dave


Hi Dave,

We need to substract 1 from the ContentControl.Range.Start.

Codes look like,
--------------------------------------------------
object index =1;
int ccpos = doc.ContentControls[1].Start;
Debug.Print(doc.Range(ccpos - 1, ccpos - 1).Font.Name);
--------------------------------------------------

Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
J

Ji Zhou

Hello Dave,

After some further researches, I find a pair of Methods which should be very
useful for this requirment. They are,

Selection.CopyFormat
http://msdn.microsoft.com/en-us/library/bb221853.aspx

Selection.PasteFormat
http://msdn.microsoft.com/en-us/library/bb221948.aspx

So to achieve the objective, I think we can call codes like,

---------------------------------
int ccpos = doc.ContentControls[1].Start;
doc.Range(ccpos - 1, ccpos - 1).Select();
Application.Selection.CopyFormat();
doc.ContentControls[1].Range.Select();
Application.Selection.PasteFormat();
---------------------------------

These two methods are mapped to the user actions of Ctrl+Shift+C and
Ctrl+Shift+V.

Hope this helps!


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support



David Thielen said:
Thank you. Also, is there an easy way to copy all the character
formatting across to the content control?

thanks - dave


Hi Dave,

We need to substract 1 from the ContentControl.Range.Start.

Codes look like,
--------------------------------------------------
object index =1;
int ccpos = doc.ContentControls[1].Start;
Debug.Print(doc.Range(ccpos - 1, ccpos - 1).Font.Name);
--------------------------------------------------

Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

David Thielen

That did it - much thanks


Hello Dave,

After some further researches, I find a pair of Methods which should be very
useful for this requirment. They are,

Selection.CopyFormat
http://msdn.microsoft.com/en-us/library/bb221853.aspx

Selection.PasteFormat
http://msdn.microsoft.com/en-us/library/bb221948.aspx

So to achieve the objective, I think we can call codes like,

---------------------------------
int ccpos = doc.ContentControls[1].Start;
doc.Range(ccpos - 1, ccpos - 1).Select();
Application.Selection.CopyFormat();
doc.ContentControls[1].Range.Select();
Application.Selection.PasteFormat();
---------------------------------

These two methods are mapped to the user actions of Ctrl+Shift+C and
Ctrl+Shift+V.

Hope this helps!


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support



David Thielen said:
Thank you. Also, is there an easy way to copy all the character
formatting across to the content control?

thanks - dave


Hi Dave,

We need to substract 1 from the ContentControl.Range.Start.

Codes look like,
--------------------------------------------------
object index =1;
int ccpos = doc.ContentControls[1].Start;
Debug.Print(doc.Range(ccpos - 1, ccpos - 1).Font.Name);
--------------------------------------------------

Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 

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