VB控制Word文档实例精选二

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,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,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,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



效果图如下:

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...