Reputation: 61
I am trying to loop through all shapes in a document and check their "Alternate Text" which has had the source filename for that image recorded as it's alternate text. I need to read specific source images and convert them to a different image format.
I am able to get to the point of reading the AlternateText of the shape but it throws an exception: '((Microsoft.Office.Interop.Word.Shape)(s)).AlternativeText' threw an exception of type 'System.Runtime.InteropServices.COMException'
When I set a breakpoint and view the "s" object, the majority of properties are throwing this exception, however some are not, for example I can read the LinkFormat property and a few others without issue, but the majority of properties throw an error.
Here is the code I am using:
Word.Application WordApp = new Word.Application();
d = WordApp.Documents.Open(@strFilename, ReadOnly: true, Visible: false);
int iReplacements = 0;
int iReplacementNoLink = 0;
foreach (Word.Shape s in d.Shapes)
{
Application.DoEvents();
try
{
if (s.LinkFormat.SourceName.ToString().Contains(".eps") || s.LinkFormat.SourceName.ToString().Contains(".png"))
{
iReplacements++;
}
if (s.AlternativeText != "")
{
iReplacementNoLink++;
}
}
catch (Exception fff)
{
Console.Write(fff);
}
}
The if statement checking the s.AlternateText always ends up in the catch.
I am using Visual Studio 2013 and I have Office 2007, I am not sure if that is relevant or not.
Can anyone tell me what I need to do to be able to read the Alternate Text of the shapes? If I am going about it in the wrong way or need to include a library or if I need to upgrade VS or Office? It seems like it should be really straight forward.
Thank you for any assistance you can provide.
Upvotes: 2
Views: 1325
Reputation: 31
Only selecting a Field has the side-effect of the page scrolling. If you don't want this, then you need to navigate back to the initial starting point. To navigate back, use Range.Select(), but if you are in a (modern) comment, use Comment.Edit().
I've created a solution for this, it can be found here: https://stackoverflow.com/a/77273213/77273213
Upvotes: 0
Reputation: 61
I am unsure why this worked, but I was able to resolve this issue by using the "Select" method of the shape. Once the shape is selected the majority of the properties that previously were throwing errors are populated. There are still approximately 20 properties that thow the error, but I am now able to access things like "AlternativeText","Name","Callout" which were previously throwing the error.
Word.Application WordApp = new Word.Application();
d = WordApp.Documents.Open(@strFilename, ReadOnly: true, Visible: false);
int iReplacements = 0;
int iReplacementNoLink = 0;
foreach (Word.Shape s in d.Shapes)
{
Application.DoEvents();
try
{
//if (s.Type == Microsoft.Office.Core.MsoShapeType.msoLinkedPicture)
if (s.LinkFormat.SourceName.ToString().Contains(".eps") || s.LinkFormat.SourceName.ToString().Contains(".png"))
{
iReplacements++;
}
s.Select();
if (s.AlternativeText != "" && s.AlternativeText != null)
{
iReplacementNoLink++;
}
}
catch (Exception fff)
{
Console.Write(fff);
}
}
Upvotes: 2