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 ?