none
CustomProperties from Shared AddIn RRS feed

  • Question

  • Hi,

      iam trying to add Custom Properties to Document from SharedAddin using OpenXML and DSOfile

    iam getting an error when i call below line (error is document is Accessed by another process) can any one help me on this.

     using (var document = Open(docName, out customPropertiesPart, true))
                    {
                   }


    suresh


    • Edited by sureshcse29 Friday, December 14, 2012 1:28 PM
    Friday, December 14, 2012 1:27 PM

Answers

  • Hi Suresh

    I think it's better to stick to one topic / discussion. If the problem with the Ribbon is unrelated to updating the document properties, please start a new discussion.

    Continuing the discussion about the document properties:

    <<The reason iam using beforesave Event is i need to force the user to fill the textbox before he saves the doc.>>

    You can continue to use BeforeSave for other things, that's not a problem. Anything that would require the document to be closed and the file lock released should be done outside that event, however.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by sureshcse29 Tuesday, December 18, 2012 11:23 AM
    Monday, December 17, 2012 1:27 PM
    Moderator

All replies

  • Could you please describe what you mean by "from SharedAddin using OpenXML and DSOfile"?

    If you have a SharedAddin running in Word and that has opened a document in Word, then you will not be able to access that same document via Open XML or DSOfile. Having the document open in Word (or Excel or PowerPoint or whatever kind of document it is) will put a file lock on it so that no other program can open it.

    You should be able to work with Document Properties from within the SharedAddin...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, December 14, 2012 2:55 PM
    Moderator
  • Hi Cindy,

    Thanks for ur reply,

    Iam closing the document and then Applying Custom Props,it Works well With Word but not with Excel,ppt.When iam trying save and close it is not saving and closing below is the code iam trying any suggestions

    Word:

                       

      void wordApp_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {           
                    Doc.Save();
                    Cancel = true;
                    string Filename = Doc.FullName;
                    try
                    {
                        Doc.Close(Type.Missing, Type.Missing, Type.Missing);
                        SaveCustomPropeties(Filename);

                    }
                    catch (Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }
                
                //// throw new NotImplementedException();
            }

    Excel:

     void excelApp_WorkbookBeforeSave(Microsoft.Office.Interop.Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
            {

            Wb.Save();
                    Cancel = true;
                    string FileName = Wb.FullName;
                    try
                    {
                        Wb.Close(Type.Missing, Type.Missing, Type.Missing);
                        SaveCustomPropeties(FileName);
                    }
                    catch
                    {
                    }

    }

    PPt:

     void pptApp_PresentationBeforeSave(Microsoft.Office.Interop.PowerPoint.Presentation Pres, ref bool Cancel)
            {

     Pres.Save();
                    Cancel = true;
                    string FileName = Pres.FullName;
                    try
                    {
                        Pres.Close();
                        SaveCustomPropeties(FileName);
                    }
                    catch
                    {
                    }

    }


    suresh

    Monday, December 17, 2012 4:40 AM
  • Hi Suresh

    Thank you for the sample code. It's interesting that Word works, as I wouldn't necessarily expect it to. The event name - BEFOREsave - does imply that the Office document may continue to be processed in the application interface.

    Normally, I'd suggest re-purposing the Ribbon command, rather than use the event. But unless you'd be sure your add-in should function only in Office 2010, I know that this approach wouldn't work going forward as Office 2013 has put the Save functionality in the Backstage where it can't be re-purposed anymore. This means that in 2013 you'd have to create your own Backstage tab for Save. If that seems OK to you, then re-purposing the Ribbon FileSave command in 2010 would be the way to go.

    Otherwise, if you want to continue to use the BeforeSave event, I think you'll have to have it start a Timer method that triggers after the event and does the close and SaveCustomProperties actions.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, December 17, 2012 9:03 AM
    Moderator
  • Hi Cindy,

    Thank u for reply,

    The reason iam using beforesave Event is i need to force the user to fill the textbox before he saves the doc.

    i have changed the code litle bit with ur prevous suggestion using custom prop in sharedAddin,but one issue i face ,when i open the word app i retrive a value to Ribbon textbox,but when i open new document the last opened value is retrived to ribbon textbox where as it should be empty.

    sample code 

    <?xml version="1.0" encoding="utf-8" ?> <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" xmlns:x="OfficeAddIn.Connect" onLoad="RibbonLoaded"> <ribbon startFromScratch="false"> <tabs> <tab id="scTab" label="Rbbon Tab" visible="true" keytip="SC"> <group id="scGroup" label="Smart Connect"> <button id="b1" label="Settings" imageMso="TableSharePointListsModifyColumnsAndSettings" keytip="T" onAction="Settings" /> <button id="b2" label="Help" imageMso="CLViewDialogHelpID" keytip="H" onAction="Help" /> <button id="b3" label="About" imageMso="Help" keytip="A" onAction="About" /> <labelControl id="label21" label=" " /> <labelControl id="label22" label=" " /> <labelControl id="label23" label=" "/> <labelControl id="label24" label=" "/> <box id="box1" boxStyle="horizontal"> <labelControl id="label31" label="Library Name"/> <editBox id="scTextbox" sizeString="123456789" onChange="OnChange" getText="GetText"/> </box> </group> </tab> </tabs> </ribbon> </customUI>

    Sample C#;

    public void OnChange(IRibbonControl control, string Text) { TextValue = string.Empty; TextValue = Text; } public string GetText(IRibbonControl control) { TextValue = string.Empty; if (whichHost == HostApplications.Word) { Microsoft.Office.Core.DocumentProperties prps =( Microsoft.Office.Core.DocumentProperties)wordApp.ActiveWindow.Document.CustomDocumentProperties; var props = prps.GetEnumerator(); foreach (DocumentProperty Dp in prps) { if (Dp.Name == CustomProperties.LibraryName) { TextValue = Dp.Value.ToString(); return TextValue; } } } return TextValue; }

    Sample Changed Code C#

      void wordApp_DocumentBeforeSave(Microsoft.Office.Interop.Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
            {
                if (string.IsNullOrEmpty(TextValue))
                {
                    System.Windows.Forms.MessageBox.Show("Library Name Cannot be Empty");
                    Cancel = true;
                }
                else if (!string.IsNullOrEmpty(TextValue))
                {                             
                    Microsoft.Office.Core.DocumentProperties prps;
                    prps = (Microsoft.Office.Core.DocumentProperties)Doc.CustomDocumentProperties;
                    if (ReadDocProperty(CustomProperties.LibraryName, prps) != null)
                    {
                        prps[CustomProperties.LibraryName].Delete();
                    }               
                    prps.Add(CustomProperties.LibraryName, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString, TextValue, Type.Missing);                
                                        
                    
                }
                //// throw new NotImplementedException();
            }




    suresh



    • Edited by sureshcse29 Monday, December 17, 2012 12:59 PM
    Monday, December 17, 2012 12:56 PM
  • Hi Suresh

    I think it's better to stick to one topic / discussion. If the problem with the Ribbon is unrelated to updating the document properties, please start a new discussion.

    Continuing the discussion about the document properties:

    <<The reason iam using beforesave Event is i need to force the user to fill the textbox before he saves the doc.>>

    You can continue to use BeforeSave for other things, that's not a problem. Anything that would require the document to be closed and the file lock released should be done outside that event, however.


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by sureshcse29 Tuesday, December 18, 2012 11:23 AM
    Monday, December 17, 2012 1:27 PM
    Moderator