Answered by:
Deleting a Range Object in Word gives Error HRESULT E_FAIL has been returned from a call to a COM component

Question
-
object endcollapsed = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
Hyperlink hyper;
Range hlinkRange = hyper.Range.Duplicate;
hyper.Delete();
hlinkRange .Collapse(ref endcollapsed);
When control comes "hyper.Delete()" method, getting Error HRESULT E_FAIL has been returned from a call to a COM component. This is happening for a particular range of hyperlink text in Field Code which inserted to the word from an external application.StackTrace : at Microsoft.Office.Interop.Word.Hyperlink.Delete()
Error Code : -2147467259
http://www.selvaonline.com - Live a Happy & Balanced Life
- Edited by SelvaOnline Thursday, January 9, 2014 5:06 PM
Thursday, January 9, 2014 4:33 PM
Answers
-
Hi,
Are you sure the code you provide is a whole code snippet and it can be built successfully.
From your code, I find that you are using an unassigned local variable 'hyper', so I don’t think it will be built successfully.
I try to repro your issue with the code below, and I find the hyperlink can be deleted successfully.
private void HyperlinkTest() { object endcollapsed = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; Hyperlink hyper = Globals.ThisAddIn.Application.ActiveDocument.Hyperlinks[1]; Range hlinkRange = hyper.Range.Duplicate; hyper.Delete(); hlinkRange.Collapse(ref endcollapsed); }
To get more effective responses, would you mind sharing your whole code snippet and the sample document to help us to reproduce your issue?
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by Luna Zhang - MSFT Friday, January 17, 2014 12:51 PM
Friday, January 10, 2014 10:31 AM
All replies
-
object endcollapsed = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd;
Hyperlink hyper;
Range hlinkRange = hyper.Range.Duplicate;
hyper.Delete();
hlinkRange .Collapse(ref endcollapsed);
When control comes "hyper.Delete()" method, getting Error HRESULT E_FAIL has been returned from a call to a COM component. This is happening for a particular range of hyperlink text in Field Code which inserted to the word from an external application.StackTrace : at Microsoft.Office.Interop.Word.Hyperlink.Delete()
Error Code : -2147467259
http://www.selvaonline.com - Live a Happy & Balanced Life
- Edited by SelvaOnline Thursday, January 9, 2014 5:05 PM
- Merged by Luna Zhang - MSFT Friday, January 10, 2014 1:35 AM same thread
Thursday, January 9, 2014 4:32 PM -
Hi SelvaOnline,
The Duplicate property of the Range class returns a read-only Range object that represents all the properties of the specified range. So, it is not allowed to call the delete method there.
Finally, I would recommend breaking the following line of code into single ones:
Range hlinkRange = hyper.Range.Duplicate;
and release underlying COM objects instantly.- Proposed as answer by Eugene Astafiev Friday, January 17, 2014 1:22 PM
Thursday, January 9, 2014 4:54 PM -
Hi Eugene Astafiev,
Thank you for helping me. I have splitted like this and no luck. Please correct me if i am wrong with my splitting.
Range hlinkRange;
hlinkRange= hyper.Range.Duplicate;
The hyper link which i am trying to delete is FieldCode in word. I am not having any issue with normal hyperlinks. When hyper link in fieldcode, i am getting this exception.
http://www.selvaonline.com - Live a Happy & Balanced Life
Thursday, January 9, 2014 5:03 PM -
Hi Eugene Astafiev,
Any thought on my updated query. Thanks a lot for your time and help in advance.
Regards,
Selva
http://www.selvaonline.com - Live a Happy & Balanced Life
Thursday, January 9, 2014 8:09 PM -
Hi,
Are you sure the code you provide is a whole code snippet and it can be built successfully.
From your code, I find that you are using an unassigned local variable 'hyper', so I don’t think it will be built successfully.
I try to repro your issue with the code below, and I find the hyperlink can be deleted successfully.
private void HyperlinkTest() { object endcollapsed = Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd; Hyperlink hyper = Globals.ThisAddIn.Application.ActiveDocument.Hyperlinks[1]; Range hlinkRange = hyper.Range.Duplicate; hyper.Delete(); hlinkRange.Collapse(ref endcollapsed); }
To get more effective responses, would you mind sharing your whole code snippet and the sample document to help us to reproduce your issue?
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by Luna Zhang - MSFT Friday, January 17, 2014 12:51 PM
Friday, January 10, 2014 10:31 AM