VB控制Word文档实例精选二

VB控制Word文档实例精选二,第1张

概述        1、返回Word文档的段落文字,控制分页,设置页眉和页脚 '先引用Microsoft Word 11.0 Object LibraryOption ExplicitDim WordApp As Word.Application '创建Word应用程序Private Sub Command1_Click() Dim i As Long On E

1、返回Word文档的段落文字,控制分页,设置页眉和页脚

'先引用Microsoft Word 11.0 Object libraryOption ExplicitDim 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 Pictures13年元旦.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 SubEnd SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = nothingEnd Sub


效果图如下:

2、控制Word文档中的文本框对象

'先引用Microsoft Word 11.0 Object libraryOption ExplicitDim 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 Pictures8254_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 SubEnd SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = nothingEnd Sub


效果图如下:

3、在Word文档中设置Excel风格的页码

'先引用Microsoft Word 11.0 Object libraryOption ExplicitDim 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 SubEnd SubPrivate Sub Form_Unload(Cancel As Integer)    On Error Resume Next    WordApp.Quit    Set WordApp = nothingEnd Sub


效果图如下:

总结

以上是内存溢出为你收集整理的VB控制Word文档实例精选二全部内容,希望文章能够帮你解决VB控制Word文档实例精选二所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1280050.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存