vb操作word

Private Sub Command1_Click()
Dim appword As Object
Dim worddoc
Set appword = CreateObject("word.application")
Set worddoc = appword.documents.Add("d:\doc1.dot", False, 0)
appword.Visible = True

Dim i As Integer
Debug.Print "-----------------------"
i = 1
For i = 1 To worddoc.Shapes.Count
If worddoc.Shapes(i).Type = 17 Then

If InStr(worddoc.Shapes(i).TextFrame.TextRange.Text, "$add$") > 0 Then

worddoc.Shapes(i).TextFrame.TextRange.Text = Text1.Text '赋给各个文本框的值
End If
End If
Next i





End Sub























多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google、baidu搜爆了,都没有找到我需要的东西。到是搜索到了很多问这个问题的记录。没办法,索性只有自己去尝试了。下面把一些代码发上来,给需要的朋友一点提示。
打开一个已经存在的wrod文件(这个文件包含了表格)


Dim WordApp
Dim Word
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set Word = WordApp.Documents.Open("c:\record.dot")
知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容


Word.Tables(1).cell(1, 2)="内容"
VBA中的这些数组元素下标都是从1开始的,比如excel的第一行一列也是ExSheet.Cells(1,1),而不是ExSheet.Cells(0,0),WORD的表格也是这样,不信自己试一下就知道了。所以上面那句话的意思就是对整个word文档中的第一个表格的第一行第二列的内容改变为“内容”。很简单吧?网上有些人在问是不是
Word.Tables(1).cell(1, 2).range.text或者Word.Tables(1).cell(1, 2).text。试一下就发现这2种都不对。
插入图片其实也很简单,代码如下:
说到这,肯定又有人会问怎么在一个word里插入一个表格。其实很简单:

如果你的程序里涉及到合并及拆分单元格,那么你可能试一下这段代码:

dim Table
set Table = wdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range, NumRows:=27, NumColumns _
:=7, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)

Set mySelection = wdApp.Documents.Application.Selection
mySelection.Cells.Borders(-7).LineStyle = 1
'选中表格的第2行第3列
table.Cell(2, 3).Select
'向下移动6格,第1个参数和第3个是常数
Call wdBook.Application.Selection.MoveDown(5, 6, 1)
'合并
wdBook.Application.Selection.Cells.Merge
'拆分成7行2列
Call wdBook.Application.Selection.Cells.Split(7, 2, True)

如果大家碰到了更复杂的程序,用程序生成起来比较麻烦,那么你就可以用模板来实现了?你可以先用word做一

个模板,把表格什么的全都先写好,然后保存成模板文件。然后你再用程序加载这个模板,然后往模板里填写数据。这样难度要低一些。不过具体情况具体分析。word这些集合的下标都是从1开始,然后只要找到表格那个集合,然后选取第一个表格就是要操作的表格了(假设你想操作的表格是模板中的第一个表格)。 代码如下:

dim table

set table=wdApp.ActiveDocument.Tables(1)



===================================================

1、对其WORD内容设置字体样式,以及在WORD中插入表格,以及表格单元格融合与填充.

Option Explicit
Private Sub Command1_Click()
Dim filename As String
CD.ShowSave
filename = CD.filename
OutWord filename
MsgBox "OK"
End Sub

Private Function OutWord(ByVal filePath As String) As Boolean
Dim newDoc As Word.Document
Set newDoc = New Word.Document

With newDoc
.Paragraphs(.Paragraphs.Count)https://www.360docs.net/doc/127932957.html, = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 10.5
.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphRight
.Content.InsertAfter "編号:" & vbCrLf

.Paragraphs(.Paragraphs.Count)https://www.360docs.net/doc/127932957.html, = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 26
.Paragraphs(.Paragraphs.Count).Range.Font.Bold = True
.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphCenter
.Content.InsertAfter vbCrLf & "XXXXXXXXX報告" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf

.Paragraphs(.Paragraphs.Count)https://www.360docs.net/doc/127932957.html, = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 15
.Paragraphs(.Paragraphs.Count).Range.Font.Bold = False
.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft
.Content.InsertAfter "项目名称:" & vbCrLf
.Content.InsertAfter "应急类型:" & vbCrLf
.Content.InsertAfter "预警状态:正常/警界/危机" & vbCrLf

.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphCenter
.Tables.Add Range:=.Range(Start:=.Range.End - 1, End:=.Range.End), NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With .Tables(1)
If .Style <> "表 (格子)" Then
.Style = "表 (格子)"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.Columns.Width = 50
.Rows.Height = 20
End With

.Paragraphs(.Paragraphs.Count)https://www.360docs.net/doc/127932957.html, = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 15
.Paragraphs(.Paragraphs.Count).Range.Font.Bold = False
.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft
.Content.InsertAfter "委 托 人:" & vbCrLf
.Content.InsertAfter "预 警 机 构:" & vbCrLf
.Content.InsertAfter "报告负责人:" & vbCrLf
.Content.InsertAfter "时 间:" & vbCrLf

.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft
.Tables.Add Range:=.Range(Start:=.Range.End - 1, End:=.Range.End), NumRows:=8, NumColumns:=2, DefaultTable

Behavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed
With .Tables(2)
If .Style <> "表 (格子)" Then
.Style = "表 (格子)"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.Cell(2, 1).Range.Text = "项目名称"
.Range.Cells(3).Row.Cells.Merge
.Range.Cells(3).Range.Font.Size = 15
.Range.Cells(3).Range.Text = "信息来源/文献检索范围:" & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(4).Row.Cells.Merge
.Range.Cells(4).Range.Text = "情况描述/检索结果:" & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(5).Row.Cells.Merge
.Range.Cells(5).Range.Text = "影响分析:" & vbCrLf & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(6).Row.Cells.Merge
.Range.Cells(6).Range.Text = "建议:" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(7).Row.Cells.Merge
.Range.Cells(7).Range.Text = "专家组成员:" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(8).Row.Cells.Merge
.Range.Cells(8).Range.Text = "附件目录:" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
.Range.Cells(9).Row.Cells.Merge
.Range.Cells(9).Range.Text = "报告负责人:" & vbCrLf & vbCrLf & vbCrLf & vbCrLf & " 年 月 日"
End With

End With

newDoc.SaveAs filePath
newDoc.Close
End Function

2、

VB程序操作word表格(文字、图片) 很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google、baidu搜爆了,都没有找到我需要的东西。到是搜索到了很多问这个问题的记录。没办法,索性只有自己去尝试了。下面把一些代码发上来,给需要的朋友一点提示。
打开一个已经存在的wrod文件(这个文件包含了表格)
Dim WordApp
Dim Word
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set Word = WordApp.Documents.Open("c:\record.dot") 知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容
Word.Tables(1).cell(1, 2)="内容" VBA中的这些数组元素下标都是从1开始的,比如excel的第一行一列也是ExSheet.Cells(1,1),而不是ExSheet.Cells(0,0),WORD的表格也是这样,不信自己试一下就知道了。所以上面那句话的意思就是对整个word文档中的第一个表格的第一行第二列的内容改变为“内容”。很简单吧?网上有些人在问是不是
Word.Tables(1).cell(1, 2).range.text或者Word.Tables(1).cell(1, 2).text。试一下就发现这2种都不对。
插入图片其实也很简单,代码如下:
Word.Tables(1).cell(1, 3).Range.InlineShapes.AddPicture ("c:\photo.jpg") 微软的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用VB对WORD做更多的应用,却又不知道怎么实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然

后查看宏,一目了然了吧! ------------------
问题:

我在vb中调用word打印报表,代码是在word中录制的宏拷贝过来的,但是在生成表格时编译通不过,代码如下:

Dim wdApp As Word.Application
Dim wdBook As Word.Document
Dim Range As Range
Dim NumRows As Long
Dim NumColumns As Long
Set wdApp = CreateObject("Word.Application")
Set wdBook = wdApp.Documents.Add
wdApp.Visible = True
....
....
--〉wdApp.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=16, NumColumns _
:=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
运行时上面这句报实时错误'91'
对象变量或with块变量未设置
该如何写?
对策: 看你的代码是想增加一个表格,VBA的代码和VB的代码是不一样的,需要转换一下,多用几次就清楚了。
应该这样写
Call wdBook.Tables.Add(wdBook.application.Selection.Range, 16, 5, 1, 0)

问题:如果我要增加一个复杂的表格,举例说就是在表格中我要合并、拆分一些单元格,还要绘制表头(带斜线的),但是在word中录制宏时无法对表格进行这些操作,我该怎么办?能不能给出一些代码?
对策:

接着上次那个程序来。。。
Set mySelection = wdApp.Documents.Application.Selection
mySelection.Cells.Borders(-7).LineStyle = 1

'选中表格的第2行第3列
table.Cell(2, 3).Select
'向下移动6格,第1个参数和第3个是常数
Call wdBook.Application.Selection.MoveDown(5, 6, 1)
'合并
wdBook.Application.Selection.Cells.Merge
'拆分成7行2列
Call wdBook.Application.Selection.Cells.Split(7, 2, True)

问题:我按你如上所说的方法去做,但是在执行table.cell(2,3).select时报错,错误为实时错误'424',要求对象,我不知道是不是在table前需要加上wdapp还是wdbook,或者是myselection,不过我都试过了,还是不行,后面的就写不下去了。我主要是想生成一个7列27行的表格,第1列的1、2两行合并为一个单元格,第1列的第5行和第6行合并做一个带斜线的表头,斜线上下要分别输入“压力计”和“测试点”作为表头分类,第4行的第2、3、4列单元格合并为一个单元格,5、6、7列合并为一个单元格,望楼主能详细写一下代码。非常感谢!我的代码如下:
Dim wdApp As Word.Application
Dim wdBook As Word.Document
Set wdApp = CreateObject("Word.Application")
Set wdBook = wdApp.Documents.Add
wdApp.Visible = True

https://www.360docs.net/doc/127932957.html, = "黑体"
wdApp.Selection.Font.Size = 22
wdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
wdApp.Selection.TypeText Text:="通风机调试报告"
wdApp.Selection.TypeParagraph
wdApp.Selection.TypeParagraph
https://www.360docs.net/doc/127932957.html, = "仿宋_GB2312"
wdApp.Selection.Font.Size = 12
Call wdApp.ActiveDocument.Tables.Add(wdApp.Application.Select

ion.Range, NumRows:=27, NumColumns _
:=7, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)
Set mySelection = wdApp.Documents.Application.Selection
mySelection.Cells.Borders(-7).LineStyle = 1

'选中表格的第2行第3列
--> Table.Cell(2, 3).Select
'向下移动6格,第1个参数和第3个是常数
Call wdBook.Application.Selection.MoveDown(5, 6, 1)
'合并
wdBook.Application.Selection.Cells.Merge
'拆分成7行2列
Call wdBook.Application.Selection.Cells.Split(7, 2, True)
Set wdBook = Nothing


对策:

不好意思啊,上次在自己机器上测试了一下,没有拷全部代码,不过就这个应该也能理解到呀,table就是要操作的那个table,一个word里面有可能有多个table,我们首先要选中要操作的那个table,我们这个table是自己用代码生成的,所以有一个方便的代码就是
dim Table
set Table = wdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range, NumRows:=27, NumColumns _
:=7, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)

修改你的程序里面插入table的那句话即可
---------------------------------------------
顺便提一下,像你这样的程序,我个人觉得是否改成模板要方便些呢?你用word做一个模板,把表格什么的全都先写好,然后保存成模板文件。然后你再用程序加载这个模板,然后往模板里填写数据。这样难度要低一些。不过具体情况具体分析。(个人意见)

之前我不是说了如果是自己创建的表格可以很方便的得到表格对象吗?就在创建时直接取得了。其实还有另外一种办法就是:你的其他程序都不变,只把出错的那句话改成:
wdApp.ActiveDocument.Tables(1).Cell(2, 3).Select
就像我开篇说的,word、excel这些集合的下标都是从1开始,然后只要找到表格那个集合,然后选取第一个表格就是要操作的表格了(因为程序只创建了一个表格)。
如果是模板的话,就应该是对已经存在的表格进行操作了,就只有用这中办法弄了。不知道我说清楚没有。
问题:
我想通过vb在word里添加多个表格,因为数据的列数较多,希望能

分成多个表格显示数据,方便查看。

部分代码如下:

For I = 1 To INT_COL
Call MyWord.ActiveDocument.Tables.Add(MyWord.Application.Selection.Range, NumRows:=30, _
NumColumns:=8, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
With MyWord.Application.Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
MyWord.Application.Selection.MoveDown Unit:=wdLine,Count:=30
MyWord.Application.Selection.TypeParagraph
Next I

但是,每次程序执行到My

Word.Application.Selection.MoveDown Unit:=wdLine,Count:=30这一句,就跳出程序。为什么?

另:您有什么好的建议,处理这种多行多列数据吗?

对策:
程序修改如下,可以出现多个表格,但是在给每一个表格输入数据时,我就不知道怎么处理了,盼回复

For I = 1 To INT_COL
Call MyWord.ActiveDocument.Tables.Add(MyWord.Selection.Range, NumRows:=30, _
NumColumns:=8, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
MyWord.Selection.Tables(1).cell(1, 1) = "Year"
Call MyWord.Selection.MoveDown(5, 30)
MyWord.Selection.TypeParagraph
Next I

你的代码基本上都差不多了,我没测试,看样子是你的思路有点问题(循环不对)

你可以先生成1个表格,再处理表格的数据。
也可以把所有表格全部生成,再处理表格数据,
看你的代码,你选择的是第2种,不管是哪种,肯定是多重循环的,你的代码只有1重循环。
思路如下:
for 表格数
生成这个表格,得到表格对象
for 表格的行
for 表格行的单元格
单元格="单元格的内容"
next
next
next
呵呵,不知道说清楚没有
你的问题并非上述原因,是我没有把光标移出表格。



# re: VB程序操作word表格(文字、图片) 2006-12-15 09:40 | 苏宁
请高手回答一下,怎么用VBA发现WORD中的合并单元格?
怎么跟椐一个字符串查找到相应的单元格? 回复 更多评论
# re: VB程序操作word表格(文字、图片) 2006-12-20 20:46 | SUNJIE
楼主实在是高,看过楼主的帖子解决了不少问题。但是还有一个不会,就是怎样将在VB picture中生成的图片导入WORD中呢?? 回复 更多评论
# re: VB程序操作word表格(文字、图片) 2007-02-05 10:17 | 落雁lilac
楼主实在是高
有个问题一直很奇怪,怎么把光标移到文档末尾,比如我要把剪切板上的东西复制到文件尾。我写的是
ActiveDocument.Range(ActiveDocument.End,ActiveDocument.End).Select
Selection.Paste
这样是不对的如何用vb在word 中生成多个表格,尤其是在很多时 涉及到换页的操作时,
大家各我看看,我的邮箱是999999999liguang@https://www.360docs.net/doc/127932957.html, qq:26749732
下面是我的代码 :
For n = 1 To 6
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=10+n, NumColumns:= _
6, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed


With wdDoc.Tables(n)
.Cell(1, 1).Merge .Cell(1, 6)
End With


With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
ActiveWindow.ActivePane.VerticalPercentScrolled = 45
Selection.MoveDown Unit:=wdLine, Count:=10+n + 1

Selection.TypeParagraph
Next n

总之微软

的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用VB对WORD做更多的应用,却又不知道怎么实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然后查看宏,一目了然了吧?呵呵。。。.不过宏代码和真正VB的代码是有点差别的,需要转换一下,多用几次就清楚了。

Word.Tables(1).cell(1, 3).Range.InlineShapes.AddPicture ("c:\photo.jpg")



Call word.Tables.Add(wdBook.application.Selection.Range, 16, 5, 1, 0)

相关文档
最新文档