locked
SharePoint New Document :: Word Add-In RRS feed

  • Question

  •  

    Hi All,

     

    I am posting this question out of total desperation really.

     

    I have create a simple Word COM ADD-IN and I am attempting to determine if a Word document has been opened via SharePoint, on the face of it this would seem a simple tasks, however, I cannot find any property in the object model which indicates the path to the SharePoint server.

     

    Essencially,

     

    1. In SharePoint I click new document.

    2. Word is opened, I think some form of ActiveX component must do this passing in the template and URL as params?

    3. My add-in then intercepts the On new document event of word.

     

    It seems impossible to retrieve the origionating path at this stage, ActiveDocument.Path is null etc etc.

     

    For example, in a simple test.

     

    string documentPath = System.String.Empty;

    Word.Document thisWordDocument = WordApp.ActiveDocument;

    documentPath = WordApp.ActiveDocument.GetType().InvokeMember("FullName",BindingFlags.GetProperty,null,thisWordDocument,null).ToString();

    MessageBox.Show("Full Name : " + documentPath);

     

    Gives name of document - No Path Url!

     

    Or

     

    string documentPath = System.String.Empty;

    Word.Document thisWordDocument = WordApp.ActiveDocument;

    documentPath = WordApp.ActiveDocument.GetType().InvokeMember("Name",BindingFlags.GetProperty,null,thisWordDocument,null).ToString();

    MessageBox.Show("Full Name : " + documentPath);

     

    Gives name of document - No path Url!

     

    What is really annoying is that if you do a saveas the document is obviously saved back (So word must know this!), hence, I have also attached to the saveas control and used InvokeMember to get its details but it only gives me the name of the document.

     

    Is there any way on earth to get the site URL from which the document has been created or is this simply impossible to achieve. I

     

     need to derive the site and document library the document comes from and then contact a custom web service to perform some actions, but cannot do this as Microsoft seem unable to expose the origionating URL.

     

    Any help would be really appreciated as I am to a total loss now.

     

    Many Regards

    David

    Saturday, October 20, 2007 4:08 PM

Answers

  • Hi David,

    Can you show your code for actually getting the ActiveDocument and the Path property?

    For instance if I just connect to an open instance of Word (that has an open document from SharePoint)

    The following gives me the correct path to the document library it's saved.

    Microsoft.Office.Interop.Word.ApplicationClass ac = (Microsoft.Office.Interop.Word.ApplicationClass)Marshal.GetActiveObject("Word.Application");

    Console.WriteLine(ac.ActiveDocument.Path);


    Also, why do you need to use reflection to access the properties?

    Thanks!
    Michael


    Posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, August 20, 2008 2:52 PM

All replies

  • Hi David

    Did you found a solution for this problem?, I´m sitting with the same problem.
    Wednesday, August 20, 2008 7:17 AM
  • Hi David,

    Can you show your code for actually getting the ActiveDocument and the Path property?

    For instance if I just connect to an open instance of Word (that has an open document from SharePoint)

    The following gives me the correct path to the document library it's saved.

    Microsoft.Office.Interop.Word.ApplicationClass ac = (Microsoft.Office.Interop.Word.ApplicationClass)Marshal.GetActiveObject("Word.Application");

    Console.WriteLine(ac.ActiveDocument.Path);


    Also, why do you need to use reflection to access the properties?

    Thanks!
    Michael


    Posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, August 20, 2008 2:52 PM
  • Hi Michael!

    The problem is not solved with the following code:


    Microsoft.Office.Interop.Word.ApplicationClass ac = (Microsoft.Office.Interop.Word.ApplicationClass)Marshal.GetActiveObject("Word.Application");

    Console.WriteLine(ac.ActiveDocument.Path);


    Did you create new document in SharePoint or opened an existing one? When creating a new document in SharePoint a Word Add-In VSTO 2005 has NO Path filled in for the active document and Name and FullName is "Document1" depending on language. Furthermore the server options in Word are disabled. You must save the document before everything is working. There must be a path or reference in Word from where the document came from? DocumentActionPanel perhaps hold that info?

    Regards
    Bjorn

    Thursday, November 20, 2008 8:07 AM
  • Hi Bjorn

    Did you ever find the path to the document library?, Im also looking for this value and I agree the ActiveDocument.Path is empty when a new document is created.
    But the value is there somewhere. if you press save as Word does know the correct location within the document library to save back to sharepoint. So where is the value?

    Regards Carsten
    Carsten I Nielsen
    Thursday, September 3, 2009 7:35 PM
  • I am running into the same issues (also see my post here: http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/3ba7f984-7d4b-48df-9ea2-2360fd3cc0bd). i believe Office uses RPC for the communication with SharePoint (also see http://msdn.microsoft.com/en-us/library/ms478653.aspx) but there doesn't seem to be a lot of information about this.
    Wednesday, February 10, 2010 12:06 AM
  • Hi all,

    I have successfully managed to extract the document library path from System.Environment.CommandLine property. Using the following method, I've managed to extract the url name of the document library:

    public static string GetDocLibPath()
        {
          string[] tempArray = Environment.CommandLine.Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
          if (tempArray.Count() >= 3)
          {
            string[] temp2Array = tempArray[2].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
            if (temp2Array.Count() == 3)
            {
              return temp2Array[2];
            }
            else return "";
          }
          else return "";
        }
    

    I'm using SharePoint 2007, Word 2007 and Visual Studio 2008 with Tools for Office.

    Hope this helps.

    Frank

    Monday, August 16, 2010 5:02 PM