Click here to Skip to main content
15,869,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
0 down vote favorite


i am creating a add-ins for Microsoft Word. i have a tree-view control where i am generating Folder, files and File's bookmarks in hierarchical structure.

For this i am using these code :
C#
void GetTree(string strSearchPath)
    {
        treeFiles.Nodes.Clear();
        SetNode(treeFiles, strSearchPath);
        treeFiles.TopNode.Expand();
    }

    void SetNode(TreeView treeName, string path)
    {
        DirectoryInfo dirInfo = new DirectoryInfo(path);
        TreeNode node = new TreeNode(dirInfo.Name);
        node.Tag = dirInfo;
        GetFolders(dirInfo, node);
        GetFiles(dirInfo, node);
        treeName.Nodes.Add(node);
    }

    void GetFolders(DirectoryInfo d, TreeNode node)
    {

        try
        {
            DirectoryInfo[] dInfo = d.GetDirectories();

            if (dInfo.Length > 0)
            {
                TreeNode treeNode = new TreeNode();
                foreach (DirectoryInfo driSub in dInfo)
                {
                    treeNode = node.Nodes.Add(driSub.Name, driSub.Name, 0, 0);
                    GetFiles(driSub, treeNode);
                    GetFolders(driSub, treeNode);
                }
            }
        }
        catch { }

    }

    void GetFiles(DirectoryInfo d, TreeNode node)
    {
        var files = d.GetFiles("*.doc*");
        FileInfo[] subfileInfo = files.ToArray<FileInfo>();

        if (subfileInfo.Length > 0)
        {
            for (int j = 0; j < subfileInfo.Length; j++)
            {
                bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) & FileAttributes.Hidden) == FileAttributes.Hidden);
                if (!isHidden)
                {
                    string strExtention = Path.GetExtension(subfileInfo[j].FullName);
                    if (strExtention.Contains("doc"))
                    {
                        TreeNode treeNode = new TreeNode();
                        treeNode = node.Nodes.Add(subfileInfo[j].FullName, subfileInfo[j].Name, 1, 1);
                       AddBookMarkFile(subfileInfo[j].FullName, treeNode);
                    }
                }
            }
        }
    }

    private void AddBookMarkFile(string strParentFile, TreeNode node)
    {
        object oDocPath = strParentFile;
        object oMissing = System.Reflection.Missing.Value;
        object oVisible = false;
        object oReadOnly = true;
        object saveChanges = Word.WdSaveOptions.wdSaveChanges;
        object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
        object routeDocument = true;

         Word.Document ReadDoc=null;

        try
        {
           // ReadDoc = Globals.ThisAddIn.Application.Documents.Open(ref oDocPath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oVisible);
            ReadDoc = Globals.ThisAddIn.Application.Documents.Open(ref oDocPath, ref oMissing, ref oReadOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oVisible, ref oMissing, ref oMissing, ref oMissing);

            // Loop through all hyperlink
            Word.Bookmarks bms = ReadDoc.Bookmarks;
            for (int i = 1; i <= bms.Count; i++)
            {

                object index = (object)i;
                Word.Bookmark bm = bms.get_Item(ref index);
                if (!string.IsNullOrWhiteSpace(bm.Name.ToString()))
                {
                    node.Nodes.Add(strParentFile + "~" + bm.Name.ToString(), bm.Name.ToString(), 1, 1);
                }
            }
        }
        finally
        {
           // ReadDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);

        }
    }


on the node double click i am using this code:

C#
private void treeFiles_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) 
    {          
        string[] fullPath = e.Node.Name.Split('~');
        string checkExt = Path.GetExtension(fullPath[0]);
        if (checkExt.Contains("doc"))
        {
            object fileName = fullPath[0];
            object readOnly = false;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;

            Word.Document aDoc = Globals.ThisAddIn.Application.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);

            //if (!string.IsNullOrWhiteSpace(fullPath[1]))
            //{
            //    //Select the bookmark
            //    object bookmarkName = fullPath[1];
            //    if (aDoc.Bookmarks.Exists(bookmarkName.ToString()))
            //    {
            //        Word.Bookmark bookmark = aDoc.Bookmarks.get_Item(ref bookmarkName);
            //        bookmark.Select();
            //    }
            //}
            aDoc.Activate();
        }


Problem are as follows: 1). Files does not open on the click method of the file in the tree-view. if i do not use

AddBookMarkFile(string strParentFile, TreeNode node)

then its working file. but i want to display the bookmark file as child file of the parent file. and on the clicking of the bookmark or the file, it should open.

2) while add the file in tree-view. if i add the bookmark file, then files come in the document recovery task-pane. how i can get rid of it.

3). if i do not use

AddBookMarkFile(string strParentFile, TreeNode node)

method then it work's fine.

please help me out.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900