Word 2007 Error - "This method or property is not available..."

G

GordonS

I am getting the COM Exception from Word 2007 when I try to mark a range and
set the font in that range to “hiddenâ€.
Error message is “This method or property is not available because the
current selection is locked for format changes.â€.
The error code in the exception is -2146823683.

I am using Visual Studio 2008 to build a Word 2007 add-in project. Office
2007 is installed on my PC.

It is a new document and I haven’t yet closed it or made it visible to the
user. I appended some content to the end of the document in the form of a
couple of tables within content control groups. For each content control
group I set the LockContentControl propertyto false beforehand. The
LockContents property is not settable from code, but when I examine the
object after the exception using Visual Studio it says that LockContents is
set to true. How is this possible? How do I make the range editable?

I tried doing a search of the web for anything on this error but found
nothing. The error seems to indicate that it thinks Word has locked an area
of my document for format changes – if that is the case, how can I unlock the
entire document using C#.

The exception details are as follows:
System.Runtime.InteropServices.COMException was unhandled by user code
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the current
selection is locked for format changes."
Source="Microsoft Word"
ErrorCode=-2146823683
StackTrace:
at Microsoft.Office.Interop.Word.FontClass.set_Hidden(Int32 prop)
at
Pilgrim.Office.Word.Utility.MSWordUtility.BackupSectionGroups(Application
objWord, Document objDoc, BillDataset objDataset, String& sErrorMessage) in
C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\Utility\MSWordUtility.cs:line 607
at Pilgrim.Office.Word.Utility.MSWordUtility.CreateBill(Application
objWord, String sTemplateName, BillDataset objDataset, String& sErrorMessage)
in C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\Utility\MSWordUtility.cs:line 323
at Pilgrim.Office.Word.PilgrimRibbon.btnNewBill_Click(Object sender,
RibbonControlEventArgs e) in C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\PilgrimRibbon.cs:line 228
at
Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
at
Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponent component, Object[] args)
at
Microsoft.Office.Tools.Ribbon.RibbonManager.Invoke(RibbonComponentCallback
callback, Object[] args)
at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)
at
Microsoft.Office.Tools.Ribbon.RibbonManager.System.Reflection.IReflect.InvokeMember(String
name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
InnerException:

The code sample is as follows:
objCopy.LockContentControl = false;
objCopy.Range.Select();
objWord.Selection.Font.Hidden = 1;
objCopy.LockContentControl = true;

objCopy is a ContentControl object of type group, and objWord is a reference
to Word.Application.
 
J

Jialiang Ge [MSFT]

Hello Gordon,

From your post, my understanding on this issue is: you wonder how to hide a
Group Content Control in Word 2007 programmatically. If I'm off base,
please feel free to let me know.

First of all, please allow me to explain why Word object model does not
allow setting format properties on group content controls, then I will show
some workarounds for your reference:

I reproduced the issue "This method or property is not available because
the current selection is locked for format changes." with the VBA macro:

Sub Macro1()
Dim contentCtrl As ContentControl
Set contentCtrl = Selection.Range.ContentControls.Add(wdContentControlGroup)
contentCtrl.Title = "Group Content Control"
contentCtrl.LockContents = False
contentCtrl.LockContentControl = False
contentCtrl.Range.Font.Hidden = 1
End Sub

The line "contentCtrl.LockContents = False" throws the exception "The
property cannot be changed for content groups".

I consulted with the Word product team with the reason. According to their
response, the properties "DefaultTextStyle", "PlaceholderText",
"LockContents", "Text", "SetPlaceholderText", "Temporary" are useless to
group content controls because GroupContentControls by nature do not have
text that can be edited. The main effect of creating a GroupContentControl
is that only other content controls inside the group can be edited by
users. Any text or other content that is inside the GroupContentControl but
outside the other content controls cannot be edited or formatted by users.
That is why these properties are removed from VSTO Office API
GroupContentControl Properties:
http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.word.groupco
ntentcontrol_properties.aspx

The product team said, the group content controls' LockContents property is
always true by design, and it is disabled to change their text format.
Actually, if we do the following test directly in Word 2007 UI (not
programmatically), we will find all the font/paragraph/style options in the
Word 2007 Home ribbon are disabled.
1. Create a new document in Word 2007, and turn to the Developer ribbon of
Word 2007 UI.
2. Type some word in the document, select the words, and click the button
"group" in the Controls tab of the Developer ribbon.
3. Select the group, and turn to Home ribbon of Word 2007 UI.
4. We will find that all the Font/Paragraph/Style options are disabled.

Therefore, we are not able to set the Font.Hidden = 1 for the group content
control range.
I am sorry if this design of GroupContentControl does not fit your request.
In order to realize the request: "hide a Group Content Control in Word 2007
programmatically", a possible workaround I can think of is to "ungroup"
http://msdn2.microsoft.com/en-us/library/microsoft.office.tools.word.content
control.ungroup.aspx the group content control when our program needs to
hide it, then call objWord.Selection.Font.Hidden = 1 on this ungrouped
range. When our program needs to show the group content control, we call
Font.Hidden = 0 first, then group the controls. Please have a try and let
me know if this workaround fits your request. I am contacting the Word
product team to see if there are better resolutions for you.

Please let me know if you have any other concerns, or need anything else.

Regards,
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

Jialiang Ge [MSFT]

Hello Gordon,

I am Jialiang Ge from Microsoft Managed Newsgroup support team. Since I
haven't seen your reply after I last posted mine, I am writing to check the
status of the issue on your side. Would you mind letting me know the result
of the suggestions? Please feel free to let me know if there is anything
else I can help. Thanks

Regards,
Jialiang Ge
MSDN Managed Newsgroup Support
 
G

GordonS

Essentially the original problem we posted is still causing me problems – we
worked around an issue but didn’t understand the symptoms that need to be
known to implement a proper resolution whenever it reoccurs.

I am trying to edit a table I have just passed into a word document using
Word Automation/VSTO. This results in the following exception:

System.Runtime.InteropServices.COMException was caught
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the current
selection is locked for format changes."
Source="Microsoft Word"
ErrorCode=-2146823683
StackTrace:
at Microsoft.Office.Interop.Word.Range.set_NoProofing(Int32 prop)
at
Pilgrim.Office.Word.Utility.MSWordUtility.RefreshSections(Application
objWord, Document objDoc, BillDataset objDataset, RefreshFieldsSplash
dlgRefreshSplash, String& sErrorMessage) in C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\Utility\MSWordUtility.cs:line 838
InnerException:

In the statement above it falls over on the statement:
objNewTableRange.Tables[1].Range.NoProofing = 0;

If I comment out the above statement it will fall over with the next
statement that tries to modify the table’s contents e.g. paste into it.

In order to resolve the issue I need to understand what the error message
“This method or property is not available because the current selection is
locked for format changes.†Is telling me.

I think the message is misleading, because I have changed the selection in
the document to range (0 – 0) and still get the same error.

What is it that Word thinks it has locked, and how do I unlock it so that I
can make changes via Automation/VSTO?

--
Gordon Smith


GordonS said:
I am getting the COM Exception from Word 2007 when I try to mark a range and
set the font in that range to “hiddenâ€.
Error message is “This method or property is not available because the
current selection is locked for format changes.â€.
The error code in the exception is -2146823683.

I am using Visual Studio 2008 to build a Word 2007 add-in project. Office
2007 is installed on my PC.

It is a new document and I haven’t yet closed it or made it visible to the
user. I appended some content to the end of the document in the form of a
couple of tables within content control groups. For each content control
group I set the LockContentControl propertyto false beforehand. The
LockContents property is not settable from code, but when I examine the
object after the exception using Visual Studio it says that LockContents is
set to true. How is this possible? How do I make the range editable?

I tried doing a search of the web for anything on this error but found
nothing. The error seems to indicate that it thinks Word has locked an area
of my document for format changes – if that is the case, how can I unlock the
entire document using C#.

The exception details are as follows:
System.Runtime.InteropServices.COMException was unhandled by user code
HelpLink="C:\\Program Files\\Microsoft
Office\\Office12\\1033\\WDMAIN11.CHM#37373"
Message="This method or property is not available because the current
selection is locked for format changes."
Source="Microsoft Word"
ErrorCode=-2146823683
StackTrace:
at Microsoft.Office.Interop.Word.FontClass.set_Hidden(Int32 prop)
at
Pilgrim.Office.Word.Utility.MSWordUtility.BackupSectionGroups(Application
objWord, Document objDoc, BillDataset objDataset, String& sErrorMessage) in
C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\Utility\MSWordUtility.cs:line 607
at Pilgrim.Office.Word.Utility.MSWordUtility.CreateBill(Application
objWord, String sTemplateName, BillDataset objDataset, String& sErrorMessage)
in C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\Utility\MSWordUtility.cs:line 323
at Pilgrim.Office.Word.PilgrimRibbon.btnNewBill_Click(Object sender,
RibbonControlEventArgs e) in C:\25
Top\Client\Addin\Pilgrim.Office.Word\Pilgrim.Office.Word\PilgrimRibbon.cs:line 228
at
Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ControlActionRaise(IRibbonControl control)
at
Microsoft.Office.Tools.Ribbon.RibbonPropertyStorage.ButtonClickCallback(RibbonComponent component, Object[] args)
at
Microsoft.Office.Tools.Ribbon.RibbonManager.Invoke(RibbonComponentCallback
callback, Object[] args)
at Microsoft.Office.Tools.Ribbon.RibbonMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture)
at
Microsoft.Office.Tools.Ribbon.RibbonManager.System.Reflection.IReflect.InvokeMember(String
name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args,
ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)
InnerException:

The code sample is as follows:
objCopy.LockContentControl = false;
objCopy.Range.Select();
objWord.Selection.Font.Hidden = 1;
objCopy.LockContentControl = true;

objCopy is a ContentControl object of type group, and objWord is a reference
to Word.Application.
 
J

Jialiang Ge [MSFT]

Hello Gordon,

Nice to see you again.

Would you let me know if the error thrown from the line:
objNewTableRange.Tables[1].Range.NoProofing = 0;
is still related to GroupContentControl?

Does the range of Tables[1] contain any GroupContentControl? I reproduced
the error when it has a GroupContentControl in the table.

As is mentioned in my first reply, almost ALL the formattings are disabled
to GroupContentControls. So the error message "This method or property is
not available because the current selection is
locked for format changes." means that we are not allowed to change any
format if the current selection contains a GroupContentControl.

The workaround is to ungroup the GroupContentControl temporarily, and group
it again when we finish the format change. I understand it is very
inconvenient, it is my pleasure to help send a wish to our product
designers via internal channel. You are also welcome to add your
supplements to make Microsoft products easier and more powerful to use by
submitting a ticket in
https://connect.microsoft.com/VisualStudio
As we strive to capture any and all product feedback so as to ensure that
we are continuously developing Microsoft products to meet customer needs,
feedback such as yours is always taken very seriously. It is appreciated
that you can paste the suggestion's link here after you submit the ticket
in https://connect.microsoft.com/VisualStudio, so that other community
members can benefit from it.

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

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
J

Jialiang Ge [MSFT]

Hello Gordon,

Would you mind letting me know if the suggestions work for your question?
Please feel free to let me know if there is anything else I can help. Thanks

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

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

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