田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 4 48
 123456
78910111213
14151617181920
21222324252627
282930


站点统计

最新评论



(正则表达式)VB.NET Word Count Function vb.net在word中使用域创建公式
未知 vb.net在word文档中插入页眉   [ 日期:2012-01-02 ]   [ 来自:本站原创 ]  HTML
   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim Word_APP As Microsoft.Office.Interop.Word.Application
        On Error Resume Next
        Word_APP = GetObject(, "Word.Application")
        If Err.Number Then
            Err.Clear()
            Word_APP = CreateObject("Word.Application")
            If Err.Number Then
                MsgBox("不能运行Microsoft.Word,请检查是否安装了Microsoft.Word")
                Exit Sub
            End If
        End If
        Word_APP.Visible = True '界面可视
        Word_APP.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateNormal
        AppActivate(Word_APP.Caption) '显示Word界面
        Word_APP.Options.CheckGrammarAsYouType = False '不拼写检查

        Dim Word_Doc As Microsoft.Office.Interop.Word.Document
        Dim Word_Range As Microsoft.Office.Interop.Word.Range
        Dim Word_Table As Microsoft.Office.Interop.Word.Table
        Word_Doc = Word_APP.Documents.Add()


        '插入页眉
        With Word_APP.ActiveWindow
            .View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader
            .ActivePane.Selection.Tables.Add(Range:=.Selection.Range, NumRows:=1, NumColumns:=3)
            .ActivePane.Selection.TypeText("页眉左侧文字")
            .Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft '左对齐
            .ActivePane.Selection.MoveRight()
            .ActivePane.Selection.TypeText("页眉中间文字")
            .Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter '居中对齐
            .ActivePane.Selection.MoveRight()
            .ActivePane.Selection.TypeText("第")
            .ActivePane.Selection.Fields.Add(Range:=.Selection.Range, Type:=Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage)       '插入当前页码 
            .ActivePane.Selection.TypeText("页")
            .ActivePane.Selection.TypeText("  共")
            .ActivePane.Selection.Fields.Add(Range:=.Selection.Range, Type:=Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages)   '插入总页数 
            .ActivePane.Selection.TypeText("页")
            .Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight '右对齐
            .View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument '跳出页眉设置
        End With
    End Sub



程序代码:


    #Region "给word文档添加页眉页脚"
    ''' <summary>
    ''' 给word文档添加页眉
    ''' </summary>
    ''' <param name="filePath">文件名</param>
    ''' <returns></returns>
    Public Shared Function AddPageHeaderFooter(filePath As String) As Boolean
        Try
            Dim oMissing As [Object] = System.Reflection.Missing.Value
            Dim WordApp As Microsoft.Office.Interop.Word._Application = New Application()
            WordApp.Visible = True
            Dim filename As Object = filePath
            Dim WordDoc As Microsoft.Office.Interop.Word._Document = WordApp.Documents.Open(filename, oMissing, oMissing, oMissing, oMissing, oMissing, _
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
                oMissing, oMissing, oMissing, oMissing)

            '''/添加页眉方法一:
            'WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            'WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
            'WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容

            '''/添加页眉方法二:
            If WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdNormalView OrElse WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdOutlineView Then
                WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView
            End If
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader
            WordApp.Selection.HeaderFooter.LinkToPrevious = False
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
            WordApp.Selection.HeaderFooter.Range.Text = "页眉内容"

            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter
            WordApp.Selection.HeaderFooter.LinkToPrevious = False
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
            WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容")

            '跳出页眉页脚设置
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument

            '保存
            WordDoc.Save()
            WordDoc.Close(oMissing, oMissing, oMissing)
            WordApp.Quit(oMissing, oMissing, oMissing)
            Return True
        Catch e As Exception
            Console.WriteLine(e.Message)
            Console.WriteLine(e.StackTrace)
            Return False
        End Try
    End Function


程序代码:

    #Region "新建Word文档"
    ''' <summary>
    ''' 动态生成Word文档并填充内容 
    ''' </summary>
    ''' <param name="dir">文档目录</param>
    ''' <param name="fileName">文档名</param>
    ''' <returns>返回自定义信息</returns>
    Public Shared Function CreateWordFile(dir As String, fileName__1 As String) As Boolean
        Try
            Dim oMissing As [Object] = System.Reflection.Missing.Value

            If Not Directory.Exists(dir) Then
                '创建文件所在目录
                Directory.CreateDirectory(dir)
            End If
            '创建Word文档(Microsoft.Office.Interop.Word)
            Dim WordApp As Microsoft.Office.Interop.Word._Application = New Application()
            WordApp.Visible = True
            Dim WordDoc As Microsoft.Office.Interop.Word._Document = WordApp.Documents.Add(oMissing, oMissing, oMissing, oMissing)

            '保存
            Dim filename__2 As Object = dir + fileName__1
            WordDoc.SaveAs(filename__2, oMissing, oMissing, oMissing, oMissing, oMissing, _
                oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, _
                oMissing, oMissing, oMissing, oMissing)
            WordDoc.Close(oMissing, oMissing, oMissing)
            WordApp.Quit(oMissing, oMissing, oMissing)
            Return True
        Catch e As Exception
            Console.WriteLine(e.Message)
            Console.WriteLine(e.StackTrace)
            Return False
        End Try
    End Function


[本日志由 tiancao1001 于 2019-06-02 06:11 PM 编辑]


暂时没有评论
发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:1*5=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©