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
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 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |