vb *** 作WORD大全

vb *** 作WORD大全,第1张

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

很多人都知道,用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).Range.Font.name = "宋体"
.Paragraphs(.Paragraphs.Count).Range.Font.Size = 10.5
.Paragraphs(.Paragraphs.Count).Alignment = wdalignParagraphRight
.Content.InsertAfter "編号:" & vbCrLf

.Paragraphs(.Paragraphs.Count).Range.Font.name = "宋体"
.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).Range.Font.name = "宋体"
.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,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).Range.Font.name = "宋体"
.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,NumRows:=8,NumColumns:=2,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,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,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,1)
'合并
wdBook.Application.Selection.Cells.Merge
'拆分成7行2列
Call wdBook.Application.Selection.Cells.Split(7,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

wdApp.Selection.Font.name = "黑体"
wdApp.Selection.Font.Size = 22
wdApp.Selection.ParagraphFormat.Alignment = wdalignParagraphCenter
wdApp.Selection.TypeText Text:="通风机调试报告"
wdApp.Selection.TypeParagraph
wdApp.Selection.TypeParagraph
wdApp.Selection.Font.name = "仿宋_GB2312"
wdApp.Selection.Font.Size = 12
Call wdApp.Activedocument.tables.Add(wdApp.Application.Selection.Range,autoFitBehavior:= _
wdautoFitFixed)
Set mySelection = wdApp.documents.Application.Selection
mySelection.Cells.borders(-7).linestyle = 1

'选中表格的第2行第3列
--> table.Cell(2,True)
Set wdBook = nothing


对策:

不好意思啊,上次在自己机器上测试了一下,没有拷全部代码,不过就这个应该也能理解到呀,table就是要 *** 作的那个table,一个word里面有可能有多个table,我们首先要选中要 *** 作的那个table,我们这个table是自己用代码生成的,所以有一个方便的代码就是
dim table
set table = wdApp.Activedocument.tables.Add(wdApp.Application.Selection.Range,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,_
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

但是,每次程序执行到MyWord.Application.Selection.MoveDown Unit:=wdline,Count:=30这一句,就跳出程序。为什么?

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

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

For I = 1 To INT_Col
Call MyWord.Activedocument.tables.Add(MyWord.Selection.Range,_
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 中生成多个表格,尤其是在很多时 涉及到换页的 *** 作时,
大家各我看看,我的邮箱是[email protected] qq:26749732
下面是我的代码 :
For n = 1 To 6
Activedocument.tables.Add Range:=Selection.Range,NumRows:=10+n,NumColumns:= _
6,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,0) 总结

以上是内存溢出为你收集整理的vb *** 作WORD大全全部内容,希望文章能够帮你解决vb *** 作WORD大全所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/langs/1293490.html

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

发表评论

登录后才能评论

评论列表(0条)

保存