1
Answer

How to get smartart index from existing word document C#

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:

 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:
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

Answers (1)

0
Photo of Bryian Tan
NA 9.4k 887.4k 7y
You can use regular expression to get the file name. Here is an example: https://dotnetfiddle.net/DdzA3c
 
  1. using System;  
  2. using System.Text.RegularExpressions;  
  3.                       
  4. public class Program  
  5. {  
  6.     public static void Main()  
  7.     {  
  8.         string file = @"C:\Users\xyz\Desktop\FYP\Animal\abc.jpg";  
  9.         string pattern  = @"[ \w-]+?(?=\.)";  
  10.           
  11.         Regex regex = new Regex(pattern);  
  12.           
  13.         Match match = regex.Match(file);  
  14.           
  15.         Console.WriteLine(match.Value);  
  16.     }  


0
Photo of Nilesh Patil
NA 3.4k 7.1k 7y
Hi,
 
Refer This Code
 
private void timer1_Tick(object sender, EventArgs e)
{
counter++;
try // Get the tif file from C:\image\ folder
{
string path = @"C:\image\";
String filename = Directory.EnumerateFiles(path, "*.png").FirstOrDefault();
if (null != filename) {
// Load picture
pictureBox1.Load(filename);
// Show the file name
lblFile.Text = filename;
}
else {
//TODO: No *.png files are found
}
}
catch(IOException ex)
{
MessageBox.Show("No files or " + ex.Message);
}
}
 
0
Photo of Bryian Tan
NA 9.4k 887.4k 7y
and your question is?