1.首先在方案中添加引用,选取com组件,选择Microsoft Word 10.0 object library.(我安装的是office xp)。
2.在程序的开头引用部分,添加如下语句:
using Word=Microsoft.Office.Interop.Word;
using System.Reflection;
3.定义如下变量,并启动word程序:
Word._Application oWapp;
Word._Document oDoc;
object oPath=文件路径;
object misOpt = System.Reflection.Missing.Value;oWapp=new Word.Application();
oWapp.Visible=true;oDoc=(Word._Document)(oWapp.Documents.Open(ref oPath,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt));
注:这段代码是C#的实现,各个参数的含义参看Office xp中的VBA帮助;oPath代表了文件路径,通过C#的装箱操作,可以直接将代表路径的字符串赋给它;到微软的网站上下载 Office XP Primary Interop Assemblies,它使得Office xp 的组件成为受托管的代码,且其使用更为方便。
这也是转贴别人的,说明一下

解决方案 »

  1.   

    http://www.lihuasoft.net/article/show.php?id=2815
    还可以参考这篇文章,:)
      

  2.   

    补充一下,我做的是WindowsApplication,不是web。
      

  3.   

    <asp:HyperLink id="HyperLink1" style="Z-INDEX: 104; LEFT: 88px; POSITION: absolute; TOP: 304px" runat="server" NavigateUrl="/我的文件夹A/我的文件夹B/我的文件A.doc">我的文件A</asp:HyperLink>
    楼主用这个就可以了,还可以指定这个路径。
    HyperLink1.NavigateUrl="/我的文件夹A/我的文件夹B/我的文件B.doc";
      

  4.   


    楼主 是不是 打开运行 就可以了我能 提供一个方法 .. 可以打开 任何一个文件...代码非常简单
     
    首先打开一个文件 对话框,随便选择一个文件 也包括word
    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
           textBox1.Text = openFileDialog1.FileName;
    }
     运行该文件 
    try
    {
    if(textBox1.Text.Length != 0)
    {
    myProcess = Process.Start(textBox1.Text);
    myProcess.WaitForExit();
    }

    catch (Exception ee) 

            richTextBox1.Text += "异常:"+ee.ToString()+"\n";

      

  5.   

    忘了 告诉你  加上命名空间using System.Diagnostics;
      

  6.   

    TO:QH_HQ(非正派)
    谢谢你,不过我要的效果不是用word打开doc文档,而是要把doc文档的内容在窗体中显示就可以。
    你还有什么办法吗?
    上面几种方法我也试过了,但是都是用word打开。
    如果用流的方式读取文档内容,我就是不知道,读出来的内容应该写在什么容器里才能显示呢?
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ControlWord
    {
        
        public class CCWordApp
        {
            private Word.ApplicationClass oWordApplic;// a reference to Word application
            private Word.Document oDoc;// a reference to the document        //用于读取打开Word的字符数
            public int Count = 0;        public CCWordApp()
            {            oWordApplic = new Word.ApplicationClass();
            }       
            public void Open(string strFileName)
            {
                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);
                
                Count = oDoc.Characters.Count;
                oDoc.Activate();
            }
       
            // 功能:WORD退出   
            public void Quit()
            {
                object missing = System.Reflection.Missing.Value;
                oWordApplic.Application.Quit(ref missing, ref missing, ref missing);
            }        public void Save()
            {
                oDoc.Save();
            }        public void SaveAs(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;            oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            }
            public void SaveAsHtml(string strFileName)
            {
                object missing = System.Reflection.Missing.Value;
                object fileName = strFileName;
                object Format = (int)Word.WdSaveFormat.wdFormatHTML;
                oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                oDoc.Close(ref missing, ref missing, ref missing);        }
            public void CopyAll()
            {
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Copy();        }        public void PasetAll()
            {            oWordApplic.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
            }
            public void Clear()
            {            object Unit = (int)Word.WdUnits.wdCharacter;
                object Count = 1;
                oWordApplic.Selection.WholeStory();
                oWordApplic.Selection.Delete(ref Unit, ref Count);
            }
            public void InsertText(string strText)
            {
                oWordApplic.Selection.TypeText(strText);
            }        public void InsertLineBreak()
            {
                oWordApplic.Selection.TypeParagraph();
            }
            public void InsertLineBreak(int nline)
            {
                for (int i = 0; i < nline; i++)
                    oWordApplic.Selection.TypeParagraph();
            }
            public void SetAlignment(string strType)
            {
                switch (strType)
                {
                    case "Center":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        break;
                    case "Left":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                        break;
                    case "Right":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                        break;
                    case "Justify":
                        oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        break;
                }        }        public void SetFont(string strType)
            {
                switch (strType)
                {
                    case "Bold":
                        oWordApplic.Selection.Font.Bold = 1;
                        break;
                    case "Italic":
                        oWordApplic.Selection.Font.Italic = 1;
                        break;
                    case "Underlined":
                        oWordApplic.Selection.Font.Subscript = 0;
                        break;
                }        }
      

  8.   

    接 上去 :
    =================    public void SetFont()
            {
                oWordApplic.Selection.Font.Bold = 0;
                oWordApplic.Selection.Font.Italic = 0;
                oWordApplic.Selection.Font.Subscript = 0;        }        public void SetFontName(string strType)
            {
                oWordApplic.Selection.Font.Name = strType;        }        public void SetFontSize(int nSize)
            {
                oWordApplic.Selection.Font.Size = nSize;        }        public void InsertPagebreak()
            {            object pBreak = (int)Word.WdBreakType.wdPageBreak;
                oWordApplic.Selection.InsertBreak(ref pBreak);
            }        public void GotoBookMark(string strBookMarkName)
            {            object missing = System.Reflection.Missing.Value;            object Book = (int)Word.WdGoToItem.wdGoToBook;
                object NameBookMark = strBookMarkName;
                oWordApplic.Selection.GoTo(ref Book, ref missing, ref missing, ref NameBookMark);
            }        public void GoToTheEnd()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.EndKey(ref unit, ref missing);        }
            public void GoToTheBeginning()
            {            object missing = System.Reflection.Missing.Value;
                object unit;
                unit = Word.WdUnits.wdStory;
                oWordApplic.Selection.HomeKey(ref unit, ref missing);        }        public void GoToTheTable(int ntable)
            {            object missing = System.Reflection.Missing.Value;
                object what;
                what = Word.WdUnits.wdTable;
                object which;
                which = Word.WdGoToDirection.wdGoToFirst;
                object count;
                count = 1;
                oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
                oWordApplic.Selection.Find.ClearFormatting();            oWordApplic.Selection.Text = "";
            }        public void GoToRightCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
            }        public void GoToLeftCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdCell;
                oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
            }        public void GoToDownCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
            }        public void GoToUpCell()
            {            object missing = System.Reflection.Missing.Value;
                object direction;
                direction = Word.WdUnits.wdLine;
                oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
            }
            public void InsertPageNumber(string strType, bool bHeader)
            {
                object missing = System.Reflection.Missing.Value;
                object alignment;
                object bFirstPage = false;
                object bF = true;
                switch (strType)
                {
                    case "Center":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
                        break;
                    case "Right":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
                        break;
                    case "Left":
                        alignment = Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
                        oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
                        break;
                }        }    }
    }
      

  9.   

    ==============
    接上去
    =========
    调用方法:
     CCWordApp test;
                        test = new CCWordApp();
                        test.Open(fi.FullName);
                        //这里为什么是2不是1呢?没整明白
                        if (test.Count < 2)
                        {
                            test.GoToTheEnd();
                            test.InsertText("略");
                            test.Save();
                            Content.Text += "处理了下面的文件:" + fi.Name + "\r\n";
                        }
                        else
                        {
                            Content.Text += "略过了下面的文件:" + fi.Name + "\r\n";
                        }                    test.Quit();
      

  10.   

    我在 网上也 没有看到  什么好的办法都是先把 WORD 转换为其他形式的...
      

  11.   

    通过COM将doc转成rtf, 然后在RichTextbox加载
      

  12.   

    TO:zxkid
    那么word文档中的图片怎么办?
      

  13.   

    能实现,WEB WIN 中都可以
    菜单和Word时间都可以自定义
    不过你的分太少了......
      

  14.   

    ref oPath,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt,ref misOpt
    我想一下``這是什么意思```為什么要寫這么多個重復的``??