1、返回Word文档的段落文字,控制分页,设置页眉和页脚
'先引用Microsoft Word 11.0 Object Library
Option Explicit
Dim WordApp As Word.Application '创建Word应用程序
Private Sub Command1_Click()
Dim i As Long
On Error GoTo Errhandler
CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
Set WordApp = New Word.Application '实例化
WordApp.Documents.Open CommonDialog1.FileName '打开Word文件
WordApp.Visible = True '显示 Office Word 界面
'或者Application.Visible = True
WordApp.DisplayAlerts = False '不提示保存对话框
'返回段落文字,返回的段落文字在文本框控件中
Text1.Text = ""
For i = 1 To ActiveDocument.Paragraphs.Count
Text1.Text = Text1.Text & (ActiveDocument.Paragraphs(i).Range.Text & vbCrLf & vbCrLf)
Next
'控制分页
WordApp.Selection.EndKey unit:=wdStory '将光标移到文档末尾
WordApp.Selection.InsertBreak wdPageBreak '在文档末尾插入一页
'设置图片格式的页眉
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.InlineShapes.AddPicture FileName:="F:\资料\My Pictures\2013年元旦.gif", LinkToFile:=False, SaveWithDocument:=True '加载一图片文件作为页眉
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'设置文本格式的页眉
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.TypeText Text:="办公室常用工具"
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
'隐藏页眉的横线
WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = False
'取得页眉的内容
Debug.Print WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text '获取WORD第一节的页眉的文字内容
'设置页脚
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.TypeText Text:="2013年" '设置页脚
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成的word文档
Errhandler:
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
WordApp.Quit
Set WordApp = Nothing
End Sub
效果图如下:
2、控制Word文档中的文本框对象
'先引用Microsoft Word 11.0 Object Library
Option Explicit
Dim WordApp As Word.Application '创建Word应用程序
Private Sub Command1_Click()
On Error GoTo Errhandler
CommonDialog1.Filter = "MS Office Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
Set WordApp = New Word.Application '实例化
WordApp.Documents.Open CommonDialog1.FileName '打开Word文件
If Documents.Count >= 1 Then
Text1.Text = "打开的Word文件是:" & ActiveDocument.Name & vbCrLf & vbCrLf
End If
WordApp.Visible = True '显示 Office Word 界面
'或者Application.Visible = True
WordApp.DisplayAlerts = False '不提示保存对话框
WordApp.Selection.EndKey unit:=wdStory '将光标移到文档末尾
WordApp.Selection.Font.Bold = 1
WordApp.Selection.Font.Name = "黑体"
WordApp.Selection.Font.Size = 18
WordApp.Selection.TypeText Text:="在Word文件中插入文本框对象"
WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter '居中显示
'创建文本框对象,座标(100,100),宽度200,高度200
With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 400, 300).Fill
'.Transparency = 1 '设置透明色
.ForeColor = vbRed '设置前景颜色
.UserPicture ("F:\资料\My Pictures\758254_960x1000_0.jpg") '设置文本框对象的背景图片
End With
ActiveDocument.Shapes(1).TextFrame.TextRange.Text = "这是一个美女" '给文本框赋值
'ActiveDocument.Shapes(1).Line.Transparency = 1 '设置透明边框线条
'再创建一个透明背景的文本框对象
With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 400, 400, 300).Fill
.Transparency = 1 '设置透明色背景
.ForeColor = vbRed '设置前景颜色
End With
ActiveDocument.Shapes(2).TextFrame.TextRange.Text = "这是一个透明背景的文本框" '给文本框赋值
'ActiveDocument.Shapes(2).Line.Transparency = 1 '设置透明边框线条
'下面是获取文本框对象的内容
Dim i As Long
For i = 1 To ActiveDocument.Shapes.Count
Text1.Text = Text1.Text & ("第" & i & "个文本框的内容:" & ActiveDocument.Shapes(i).TextFrame.TextRange.Text & vbCrLf)
Next
ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成的word文档
Errhandler:
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
WordApp.Quit
Set WordApp = Nothing
End Sub
效果图如下:
3、在Word文档中设置Excel风格的页码
'先引用Microsoft Word 11.0 Object Library
Option Explicit
Dim WordApp As Word.Application '创建Word应用程序
Dim WordDoc As Word.Document '创建Word文档对象
Private Sub Command1_Click()
Dim i As Long
On Error GoTo Errhandler
CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
Set WordApp = New Word.Application '实例化
Set WordDoc = WordApp.Documents.Open(CommonDialog1.FileName) '选择并打开Word文件
WordApp.Visible = True '显示 Office Word 界面
'或者Application.Visible = True
WordApp.DisplayAlerts = False '不提示保存对话框
'设置Word文档第一页页码
Dim WordRange As Range
Set WordRange = WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
With WordRange
.InsertAfter "第"
.Font.Size = 14
.Collapse Direction:=wdCollapseEnd
'插入页码域
.Fields.Add Range:=WordRange, Type:=wdFieldEmpty, Text:="PAGE \* Arabic ", PreserveFormatting:=True
.Expand unit:=wdWord
.InsertAfter "页 "
.InsertAfter "共"
.Collapse Direction:=wdCollapseEnd
'插入页数域
.Fields.Add Range:=WordRange, Type:=wdFieldEmpty, Text:="NUMPAGES \* Arabic ", PreserveFormatting:=True
.Expand unit:=wdWord
.InsertAfter "页"
.InsertAfter "【我的Word文件 作者:ChenJL1031(东方之珠)】"
.ParagraphFormat.Alignment = wdAlignParagraphRight '右对齐
End With
'Text1.Text = WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text
Set WordRange = Nothing
ActiveDocument.SaveAs "c:\MyWord.doc" '保存最后生成的word文档
Errhandler:
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
WordApp.Quit
Set WordApp = Nothing
End Sub
效果图如下: