如何在指定的文件夹新建Word文档?
怎样实现类似于这样的操作:在任意一个文件夹单击鼠标右键——新建Microsoft Word文档 ?
怎样把这些功能放在一个函数里面?通过类似于CreateDoc(WinWordPath,Directory.GetCurrentDirectory()+"\NewDoc\1.doc")的调用新建一个1.doc文档?
问题点数:100、回复次数:12Top
1 楼xiongchen(二氧化鬼)回复于 2005-02-17 14:20:39 得分 40
用这个不就好了?
object fileName = Environment.CurrentDirectory+"\\example.doc";
doc.SaveAs ( ref fileName,ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional,
ref optional);
Top
2 楼alexzhang00(三角猫)回复于 2005-02-17 14:26:06 得分 0
学习Top
3 楼AllenTing(今天你GC了吗???)回复于 2005-02-17 14:28:28 得分 0
upTop
4 楼nga96(因为我笨,所以努力。陈勇华)回复于 2005-02-17 14:31:37 得分 10
一楼的,代码要是全点就好啦,没理解Top
5 楼hoking3210(风)回复于 2005-02-17 15:09:54 得分 0
To xiongchen(二氧化鬼)
能说明白一点吗?
另外,这个和File.Create(filePath)有什么不同?我在另一个函数调用
File.Create(filePath);
OpenDoc(wordPath,filePath);
private void OpenDoc(string extPath,string filePath)
{
Process p = new Process();
p.StartInfo.FileName=extPath;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments=filePath;
p.Start();
}
打开word的时候,总是弹出错误对话框,提示“文档的名称或路径无效……”Top
6 楼readersm68(地主)回复于 2005-02-17 15:26:53 得分 0
学习Top
7 楼foolnet(foolnet)回复于 2005-02-17 15:42:31 得分 40
首先要导入Word.dll
namespace sample2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
public static int Main (string[] argv)
{
Application app = new Application();
app.Visible=true;
// Set up to create a plain, empty text document.
// Setting these variables to Missing.Value is comparable.
// to not providing a value for an optional parameter in VB.
// This gets the default behavior.
object template=Missing.Value; //No template.
object newTemplate=Missing.Value; //Not creating a template.
object documentType=Missing.Value; //Plain old text document.
object visible=true; //Show the doc while we work.
_Document doc = app.Documents.Add( ref template,
ref newTemplate,
ref documentType,
ref visible);
Thread.Sleep (5000); //Display the empty document for 5 seconds.
doc.Words.First.InsertBefore ("This document is no longer empty!");
Thread.Sleep (5000); //Wait for 5 more seconds
//Save the file, use default values except for filename.
object fileName = Environment.CurrentDirectory+"\\example2_new";
object optional = Missing.Value; //Take default values.
#if OFFICEXP
doc.SaveAs2000( ref fileName,
#else
doc.SaveAs ( ref fileName,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
#endif
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional,
ref optional);
// Now use the Quit method to cleanup.
object saveChanges = true;
app.Quit(ref saveChanges, ref optional, ref optional);
Console.Read();
return 0;
}
}
}
Top
8 楼hoking3210(风)回复于 2005-02-17 16:40:54 得分 0
To foolnet(foolnet)
直接调word,不使用word控件可以吗?Top
9 楼sutalon(神州无敌)回复于 2005-02-17 16:56:26 得分 0
upTop
10 楼readersm68(地主)回复于 2005-02-18 11:56:23 得分 10
Word.dll 没有这个DLLTop
11 楼hoking3210(风)回复于 2005-02-18 12:47:37 得分 0
To readersm68(Up)
大哥,你就当 foolnet(foolnet) 添加了Microsoft Word 9.0 Object Library的COM引用好了。
另外,有解决办法吗?Top
12 楼yizhixiaozhu(天啦,手都起茧了)回复于 2005-02-18 13:00:03 得分 0
upTop




