乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > 怎样在<em>VB</em>中控制<em>Word</em>-vb 操作

怎样在<em>VB</em>中控制<em>Word</em>-vb 操作

作者:乔山办公网日期:

返回目录:word文档


Option Explicit

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
    ShellExecute Me.hwnd, "open", "c:a.doc", vbNullString, vbNullString, 3
End Sub

Private Sub Command2_Click()
    Dim WordApp As Word.Application
    
    Set WordApp = New Word.Application
    WordApp.Documents.Add ("c:a.doc")
    WordApp.Visible = True
 
    Set WordApp = Nothing
End Sub


(1)有一些语句和函数不能使用,包括:控制结构,如While…Wend和If…Then…Else;声明语句,如Dim;定制对话框相关的语句:FileExit语句;要求数组变量作为参数的语句或函数。
(2)也有一些指令使用方法不同。①返回字符串以一个美元符($)结束的WordBasic函数的关键字必须括在方括号中。例如,在WordBasic宏中的GetBookmark$()636f70797a64361语句:mark$=GetBookmark$("Address"),若用VB调用,必须这样写mark$=Wordobj.[Ge-tBookmark$]("Address")。②选择一个命令按钮用“True”,不选择用“False”
2.3 对OLE自动化的说明
Word可以为OLE自动化给另一个应用提供对象,但是它不能使用OLE自动化访问其它应用中的对象。例如:VB和Excel可以使用OLE自动化访问Word,但是Word不能使用OLE自动化访问它们。
3 在包容器中嵌入Word对象
在VB中,要访问在包容器中嵌入的Word对象,首先要在项目中插入对象。做法如下:在窗体中插入OLE控件,对象类型选择“MicrosoftWord图片”或“Microsoft Word文档”,再按“确定”。
这个题目太大了吧。能不能缩小一下范围呢?不说的话给你来点范文吧。
============================
将格式应用e68a84e8a2ad7a64336于选定内容
下列示例使用 Selection 属性将字符和段落格式应用于选定文本。使用 Font 属性获得字体格式的属性和方法,使用 ParagraphFormat 属性获得段落格式的属性和方法。

Sub FormatSelection()
With Selection.Font
.Name = "Times New Roman"
.Size = 14
.AllCaps = True
End With
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0.5)
.Space1
End With
End Sub
将格式应用于某一区域
下列示例定义了一个 Range 对象,它引用了活动文档的前三个段落。通过应用 Font 和 ParagraphFormat 对象的属性来设置 Range 对象的格式。

Sub FormatRange()
Dim rngFormat As Range
Set rngFormat = ActiveDocument.Range( _
Start:=ActiveDocument.Paragraphs(1).Range.Start, _
End:=ActiveDocument.Paragraphs(3).Range.End)
With rngFormat
.Font.Name = "Arial"
.ParagraphFormat.Alignment = wdAlignParagraphJustify
End With
End Sub
插入文字并应用字符和段落格式
下列示例在当前文档的上部添加单词 Title。第一段居中对齐,并在该段落之后添加半英寸的间距。将单词 Title 的格式设为 24 磅 Arial 字体。

Sub InsertFormatText()
Dim rngFormat As Range
Set rngFormat = ActiveDocument.Range(Start:=0, End:=0)
With rngFormat
.InsertAfter Text:="Title"
.InsertParagraphAfter
With .Font
.Name = "Tahoma"
.Size = 24
.Bold = True
End With
End With
With ActiveDocument.Paragraphs(1)
.Alignment = wdAlignParagraphCenter
.SpaceAfter = InchesToPoints(0.5)
End With
End Sub
在 12 磅和无之间切换段前间距
下列示例切换选定内容中第一段的段前间距。宏将获取当前段前间距的值,如果该值为 12 磅,则删除段前间距格式(将 SpaceBefore 属性设为零)。如果段前间距的值为除 12 外的其他数值,则将 SpaceBefore 属性设为 12 磅。

Sub ToggleParagraphSpace()
With Selection.Paragraphs(1)
If .SpaceBefore <> 0 Then
.SpaceBefore = 0
Else
.SpaceBefore = 6
End If
End With
End Sub
切换加粗格式
下列示例切换选定文本的加粗格式。

Sub ToggleBold()
Selection.Font.Bold = wdToggle
End Sub
将左边距增加 0.5 英寸
下列示例将左边距和右边距增加 0.5 英寸。PageSetup 对象包含文档的所有的页面设置属性(左边距、下边距、纸张大小等)。LeftMargin 属性用于返回和设置左边距设置。RightMargin 属性用于返回和设置右边距设置。

Sub FormatMargins()
With ActiveDocument.PageSetup
.LeftMargin = .LeftMargin + InchesToPoints(0.5)
.RightMargin = .RightMargin + InchesToPoints(0.5)
End With
End Sub

Dim wordapp As Word.Application
Dim wdDoc As Word.Document

Set fso = CreateObject("Scripting.FileSystemObject")
Set files = fso.GetFolder("C:\path").Files
Set wordapp = CreateObject("Word.Application")
For Each polozka In files
Set wdDoc = wordapp.Documents.Open(polozka.ShortPath)
wordapp.Visible = True
With wdDoc.Selection
.HomeKey Unit:=6
.Find.Text = "Název (typ):"
.Find.Wrap = wdFindContinue
...
End With
...
wordapp.Quit
Set wordapp = Nothing
Next

相关阅读

关键词不能为空
极力推荐
  • 轻松获取PPT中的各种多媒体素材-word2007下载

  • word2007下载,微信公众号:爱技术,信息技术技能知识时时分享。碰见一些好的PPT课件,里面用到了大量的精美图片、声音、动画和视频等素材。你是不是经常会去这些课件中复制粘贴好

ppt怎么做_excel表格制作_office365_word文档_365办公网