
1. vba在word文档中如何插入文字并设置相应的文字属性
下面的VBA代码功能,第一步,word当前文档的上面添加文字dzwebs文字。然后,又设置第一段居中对齐,并在该段落之后添加半英寸的间距。将dzwebs的格式设为36磅,隶书字体。
Dim rngFormat As Range
Set rngFormat = ActiveDocument.Range(Start:=0, End:=0)
With rngFormat
.InsertAfter Text:="dzwebs"
.InsertParagraphAfter
With .Font
.Name = "隶书"
.Size = 36
.Bold = True
End With
End With
With ActiveDocument.Paragraphs(1)
.Alignment = wdAlignParagraphCenter
.SpaceAfter = InchesToPoints(0.5)
End With
部分代码解释:
Dim rngFormat As Range 定义变量
Set rngFormat = ActiveDocument.Range(Start:=0, End:=0) 设置rngFormat变量的位置,位于文档的最上面
With rngFormat
里面的代码功能是设置字体字号等属性
End With
With ActiveDocument.Paragraphs(1)
.Alignment = wdAlignParagraphCenter
.SpaceAfter = InchesToPoints(0.5)
End With
该代码代码功能是添加段后距离为0.5英寸
2. 如何用vba代码将word文档中插入的表格对象内容复制到Excel中
试试下面的代码,在网上找的
Sub 宏1()
Dim wordapp As Object
Dim mydoc
Dim mypath$, myname$
Dim wdRng As Object
Dim pos1%, pos2% '定义找到的字段的首位位置
Application.DisplayAlerts = False
Set wordapp = CreateObject("word.application")
mypath = ThisWorkbook.Path & ""
myname = Dir(mypath & "*.doc*")
Set mydoc = wordapp.Documents.Open(mypath & myname)
Set wdRng = mydoc.Range
wdRng.Find.Execute ("(一)")
pos1 = wdRng.Start
Set wdRng = mydoc.Range
wdRng.Find.Execute ("五、")
pos2 = wdRng.Start
mydoc.Range(pos1, pos2).Copy '选中找到的两个字段中间的内容
mydoc.Close False
wordapp.Quit
Worksheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
3. 如何Word中用VBA打开Excel表格,读取其中的内容写入到Word
可用CreateObject函数创建对EXCEL的引用,然后就可打开EXCEL工作簿,将表格中的数据复制到WORD中,参考代码如下:
Set xlapp = CreateObject("excel.application")
With xlapp.Open("带路径的EXCEL文件名")
.Sheets(1).Range("A1:H8").Copy
'粘贴到WORD指定位置
End With
xlapp.Quit
4. word vba 在固定页 固定字后面添加指定内容
Sub test()
Dim Matches, N&
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "(^|[\r\n])([ \t\xa0]*\S\S条)"
If .test(ThisDocument.Range.Text) Then
Set Matches = .Execute(ThisDocument.Range.Text)
For N = Matches.Count - 1 To 0 Step -1
With Matches(N)
ThisDocument.Range(.firstindex + .Length, .firstindex + .Length).Text = "hello"
End With
Next N
End If
End With
End Sub
转载请注明出处51数据库 » wordvba插入内容
本来很帅61482213