blog.easyciel.net author="Patrick Rabian" about="c#, sharepoint, biztalk, team system resources" more="news, samples, tips for .NET world's developers !"

How to modify Microsoft Office document properties (Excel, Word, Powerpoint) without Office installed ?

Friday, 23 December 2005 18:10 by prabian

From your Visual Studio’s projet (.NET 2003 or 2005), add a reference to the pre-registered dll DSOFILE.DLL downloaded from : Support Microsoft

You can use this sample which do the following :

  • Open a office document in read-only mode
  • Compare the custom property named ‘connectionStringCustomProperty’ with a new value
  • If the value is already set : return
  • Else re-open the document in read-write mode and set the new value

private void UpdateConnectionString(string filePath,string connectionStringCustomProperty, string newConnectionString)
{

OleDocumentPropertiesClass document = new OleDocumentPropertiesClass();
bool mustUpdate = false;

try        
{
//Verifions si la modification est nécessaire en mode read-only

document.Open(filePath, true, dsoFileOpenOptions.dsoOptionDefault);
foreach (CustomProperty property in document.CustomProperties)
{

if (property.Name == connectionStringCustomProperty)
{

object value = property.get_Value();

if (!value.ToString().Equals(newConnectionString))
{
mustUpdate =
true;
}
}
}
}

finally        
{
document.Close(
false);
}

if (!mustUpdate) return;

//Modification de la propriété        

try        
{
document.Open(filePath,
false, dsoFileOpenOptions.dsoOptionDefault);
foreach (CustomProperty property in document.CustomProperties)
{

if (property.Name == connectionStringCustomProperty)
{

object value = (object)newConnectionString;
property.set_Value(
ref value);
}
}

if (document.IsDirty)
document.Save();
}

finally        
{
document.Close(
false);
}
}

French title : Comment modifier les propriétés personnalisées d’un fichier Microsoft Office Excel, Word ou PowerPoint ?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   Office
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 20. 2008 23:51