我正在为 Microsoft Word 创建一个加载项。我有一个树视图控件,我在其中以分层结构生成文件夹、文件和文件的书签。
为此,我正在使用这些代码:
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);
}
}
在节点上双击我正在使用此代码:
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();
}
问题如下: 1)。文件不会在树视图中的文件的单击方法上打开。如果我不使用
AddBookMarkFile(string strParentFile, TreeNode node)
然后是它的工作文件。但我想将书签文件显示为父文件的子文件。单击书签或文件时,它应该打开。
2)同时在树视图中添加文件。如果我添加书签文件,则文件会出现在文档恢复任务窗格中。我怎么能摆脱它。
3)。如果我不使用
AddBookMarkFile(string strParentFile, TreeNode node)
方法然后它工作正常。
请帮帮我。