Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Dear All,

I want to evaluate smartart shape, which is already inserted in the existing word document. Where the document contain n number of smartart shapes.

I alreay searched on google regarding this, but I did not find suitable one.

Please review my code....

What I have tried:

C#
public static bool EvaluateSmartArtShape(IQuestion question, string filename, SmartArtShapes _smartArtShape)
       {
           WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);

           try
           {
               //Avoid screen flickering or unwanted alerts while initializing
               wordApplication.ScreenUpdating = false;
               WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
               wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;

               WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);
               //WORD
               WordInterop.Shapes ws = wordDocument.Shapes;
               foreach(WordInterop.Shape s in ws)
               {
                   if (s.HasSmartArt == MsoTriState.msoTrue)
                   {
                       if (s == wordApplication.SmartArtLayouts[(int)_smartArtShape])//line 697
                       {
                           return true;
                       }
                   }
               }
               wordDocument.Close();

           }
           catch (Exception ee)
           {
               string strError = ee.ToString();
               Cleanup(question.ObjectStore, true);
           }
           return false;
       }


Error:
C#
System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
   at Microsoft.Office.Core.SmartArtLayouts.get_Item(Object Index)
   at TeTec.Action.Office2013.Word.WordHelper.EvaluateSmartArtShape(IQuestion question, String filename, SmartArtShapes _smartArtShape) in C:\TETEC\Development\TeTec.Action\Office\Office2013\Word\WordHelper.cs:line 697


Can any one please help me.


Thanks
Posted
Updated 17-May-17 22:27pm
v6
Comments
Maciej Los 30-Apr-17 9:05am    
Seems _smartArtShape is a collection of SmartArtShapes... If i'm right, then you cannot convert collection into integer value.
F-ES Sitecore 30-Apr-17 10:02am    
Word automation isn't supported under asp.net. This might appear to work on your local machine but it won't work when deployed.
abdul subhan mohammed 30-Apr-17 10:23am    
Its an winfroms app
OriginalGriff 1-May-17 1:48am    
Don't "bump" your question: it's rude, arrogant, unnecessary, and doesn't help you get a faster answer. By all means add info, but just editing it to get it back to the top of the "unanswered" list is just saying "I'm more important than anyone else, so deal with *MY* question and ignore the rest".
And so is everybody else, and everybody else's questions. If everyone was this thoughtless and rude, you'd never get an answer to anything, because all we'd be able to see would be 100 pages of idiots bumping all the real questions to page 100+
All you do is annoy people, and get them to deliberately not even look at your question, much less answer it.
abdul subhan mohammed 2-May-17 10:01am    
I apologize, what you are saying is right but dear, what if my question goes to the last where only few persons will go through it.

1 solution

public static bool EvaluateSmartArtShape(IQuestion question, string filename, SmartArtShapes _smartArtShape)
        {
            WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);

            try
            {
                //Avoid screen flickering or unwanted alerts while initializing
                wordApplication.ScreenUpdating = false;
                WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
                wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;

                WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);

                bool result = false;
                //WORD
                WordInterop.Shape tempSmartShape = null;
                WordInterop.Shapes ws = wordDocument.Shapes;
                foreach (WordInterop.Shape s in ws)
                {
                    if (s.HasSmartArt == MsoTriState.msoTrue)
                    {
                        tempSmartShape = s;
                        SmartArt _smartArt = tempSmartShape.SmartArt;
                        string _smartArtName = _smartArt.Layout.Name;

                        if (_smartArtName.Replace(" ", "").Trim().ToLower() == _smartArtShape.ToString().Trim().ToLower())
                            result = true;

                        break;
                    }
                }                

                wordDocument.Close();
                return result;

            }
            catch (Exception)
            {
                Cleanup(question.ObjectStore, true);
            }
            return false;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900