| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1124 人关注过本帖
标题:[救助]关于vb.net创建、编写、保存Word文档
只看楼主 加入收藏
kila2719641
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-16
收藏
 问题点数:0 回复次数:0 
[救助]关于vb.net创建、编写、保存Word文档
本人最近要做一个基于的试卷生成系统

要求具有导入,导出功能,也要能导出为word文档便于打印为纸质试卷。

于是上网查找了一下资料找到了“(2005)创建、编写、保存Word文档”。但是我用的是Microsoft VisualStudio 2010。
代码为如下:
Imports Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Public Class From1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
        Dim WordApp As New Word.Application
        Dim wordDoc As Word.Document
        wordDoc = WordApp.Documents.Add()
        wordDoc.Activate()
        WordApp.Visible = True              '可以观察

        WordApp.Options.CheckGrammarAsYouType = False       '不检查语法
        WordApp.Options.CheckSpellingAsYouType = False      '不检查拼写
        WordApp.Selection.TypeText(vbCrLf)                  '回车
        WordApp.Selection.Font.Color = Word.WdColor.wdColorBlue     '更改颜色
        WordApp.Selection.TypeText("这是蓝色")      '打字
        WordApp.Selection.TypeParagraph()           '段落回车

        With WordApp.Selection.ParagraphFormat              '行前0.5,行后0.5,段落双倍行距
            .LeftIndent = WordApp.CentimetersToPoints(0)
            .RightIndent = WordApp.CentimetersToPoints(0)
            .SpaceBefore = 2.5
            .SpaceBeforeAuto = False
            .SpaceAfter = 2.5
            .SpaceAfterAuto = False
            .LineSpacingRule = Word.WdLineSpacing.wdLineSpace1pt5
            .Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
            .WidowControl = False
            .KeepWithNext = False
            .KeepTogether = False
            .PageBreakBefore = False
            .NoLineNumber = False
            .Hyphenation = True
            .FirstLineIndent = WordApp.CentimetersToPoints(0)
            .OutlineLevel = Word.WdOutlineLevel.wdOutlineLevelBodyText
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            .CharacterUnitFirstLineIndent = 0
            .LineUnitBefore = 0.5
            .LineUnitAfter = 0.5
            .AutoAdjustRightIndent = True
            .DisableLineHeightGrid = False
            .FarEastLineBreakControl = True
            .WordWrap = True
            .HangingPunctuation = True
            .HalfWidthPunctuationOnTopOfLine = False
            .AddSpaceBetweenFarEastAndAlpha = True
            .AddSpaceBetweenFarEastAndDigit = True
            .BaseLineAlignment = Word.WdBaselineAlignment.wdBaselineAlignAuto
        End With

        WordApp.Selection.Font.Italic = Word.WdConstants.wdToggle   '斜体开始
        WordApp.Selection.TypeText(Text:="这是斜体")
        WordApp.Selection.TypeParagraph()
        WordApp.Selection.Font.Italic = Word.WdConstants.wdToggle   '斜体结束
        WordApp.Selection.Font.Name = "Times New Roman"             '更换字体
        WordApp.Selection.TypeText(Text:="this is times new roman")

        WordApp.Selection.Font.Size = 12
        WordApp.Selection.TypeText(Text:="变换字体")
        WordApp.Selection.TypeParagraph()
        WordApp.Selection.Font.Bold = Word.WdConstants.wdToggle     '粗体开始
        WordApp.Selection.TypeText(Text:="实验设置:")
        WordApp.Selection.TypeParagraph()
        WordApp.Selection.Font.Bold = Word.WdConstants.wdToggle     '粗体结束
        WordApp.Selection.Font.Name = "黑体"                        '黑体
        WordApp.Selection.TypeText(Text:="我是黑体")
        WordApp.Selection.TypeParagraph()
        WordApp.Selection.InlineShapes.AddPicture(FileName:="d:\1.jpg", LinkToFile:=False, SaveWithDocument:=True)      '插入图片
        WordApp.Selection.TypeParagraph()

        WordApp.ActiveDocument.Tables.Add(Range:=WordApp.Selection.Range, NumRows:=2, NumColumns:= _
    5, DefaultTableBehavior:=Word.WdDefaultTableBehavior.wdWord9TableBehavior, AutoFitBehavior:= _
    Word.WdAutoFitBehavior.wdAutoFitFixed) '创建2*5的表格
        With WordApp.Selection.Tables(1)
            .Style = "网格型"
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = True
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = True
        End With

        wordDoc.SaveAs("d:\temp.doc")       '将文件存储   
        wordDoc.Close()                     '关闭
        WordApp.Quit()
        Me.Show()
    End Sub
End Class





但是,我在vs2010上运行的时候会出错。又不会改,请各位大虾指教。
列出warnings和Errors:
Warning    1    Namespace or type specified in the Imports 'Microsoft.Office.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.    c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    1    9    WindowsApplication3

Warning    2    Namespace or type specified in the Imports 'Microsoft.Office.Interop.Word' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.    c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    2    9    WindowsApplication3

Error    3    Type 'Word.Application' is not defined.    c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    6    28    WindowsApplication3

Error    4    Type 'Word.Document' is not defined.    c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    7    24    WindowsApplication3


Error 5~15都是:

Error    5    'Word' is not declared. It may be inaccessible due to its protection level.    c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsApplication3\WindowsApplication3\Form1.vb    15    40    WindowsApplication3


搜索更多相关主题的帖子: word 
2011-03-16 14:13
快速回复:[救助]关于vb.net创建、编写、保存Word文档
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039502 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved