日期:2014-05-19  浏览次数:20836 次

关于用C#写入Word文件的问题,请大家帮帮忙!
我做了一个能导出内容到Word文件的软件,但是会导出很多次,想把导出的内容都写到一个文件里,就是每次导出时自动从文件的尾部开始写,虽然我能得到当前文档的对象Word.Application   oWord   =   (Word.Application)Marshal.GetActiveObject( "Word.Application ");   但是之后用oWord.Documents.Add语句总会新建一个文件,我想问问如何定位到现在正在编辑的Documents,并且续写,谢谢,谢谢!

------解决方案--------------------
先saveas()
再add()
------解决方案--------------------
http://community.csdn.net/Expert/topic/5396/5396424.xml?temp=.9965326
Microsoft.Office.Interop.Word.ApplicationClass oWordApplic;
Microsoft.Office.Interop.Word.Document oDoc

oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;

oDoc = oWordApplic.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, ref missing, ref missing, ref missing,ref missing);
oDoc.Activate();
object missing = System.Reflection.Missing.Value;
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);
oWordApplic.Selection.TypeText( "newcontent ");
------解决方案--------------------
这里有个读取的 不知到有用没

public static string WordText(string strPath)
{//读取word信息

string strWordText = " ";
if (!File.Exists(strPath))
//该文件不存在
return " ";
KillProcess( "WINWORD ");
ApplicationClass wordApp = new ApplicationClass();
object file = strPath;
object nullobj = System.Reflection.Missing.Value;

Document doc = null;
try
{
doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj, ref nullobj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
strWordText = data.GetData(DataFormats.Text).ToString();
}
catch
{
return " ";
}
finally
{
object vk_SaveChanges = WdSaveOptions.wdDoNotSaveChanges;
object vk_OriginalFormat = WdOriginalFormat.wdWordDocument;
object vk_RouteDocument = false;
doc.Close(ref vk_SaveChanges, ref vk_OriginalFormat, ref vk_RouteDocument);
wordApp.Quit(ref vk_SaveChanges, ref vk_OriginalFormat, ref vk_RouteDocument);
}
System.GC.Collect();
return strWordText;
}
------解决方案--------------------
你可以试试用application.open 方法