vb操作word全.docx

上传人:b****5 文档编号:7403606 上传时间:2023-01-23 格式:DOCX 页数:9 大小:21.71KB
下载 相关 举报
vb操作word全.docx_第1页
第1页 / 共9页
vb操作word全.docx_第2页
第2页 / 共9页
vb操作word全.docx_第3页
第3页 / 共9页
vb操作word全.docx_第4页
第4页 / 共9页
vb操作word全.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

vb操作word全.docx

《vb操作word全.docx》由会员分享,可在线阅读,更多相关《vb操作word全.docx(9页珍藏版)》请在冰豆网上搜索。

vb操作word全.docx

vb操作word全

利用VB操作WORD的基本方法

DimMyWordAsWord.Application

DimMyWordBookAsWord.Document

SetMyWord=NewWord.Application

SetMyWordBook=MyWord.Documents.Add(ModelPath&"test.dot")'打开test.dot用户自定义的WORD模版

'MyWord=CreateObject("Word.Application")

'MyWord.Visible=False

'MyWord.Documents.Open("test.doc")

'MyWord.ActiveDocument.Bookmarks.Item("MM").Select

'MyWord.Selection.Text="从资料库取出的相应资料"

'MyWord.ActiveDocument.SaveAs("test1.doc")

MyWordBook.Activate

WithMyWordBook

'   .Paragraphs(.Paragraphs.Count).Range.Font.Size=30'设置文字字体大小(其它设置类推)如果去除.Paragraphs(.Paragraphs.Count)则表示所有行

'   .Paragraphs(.Paragraphs.Count).Alignment=wdAlignParagraphCenter'设置文字居中等

'   .Content.InsertAfter"hello"&vbCrLf    '向WORD写入一行文字并回车

'   .Content.InsertAfter"hello"

'   .Paragraphs(.Paragraphs.Count).Range.Font.Size=10

'   .Content.InsertAfter"hello"

   .Bookmarks("mm").Range.InsertAfter"name"   '在WORD指定位置(标签)写入文本,指定位置指WORD标签

   .Tables

(1).Cell(2,1).Range.InsertAfter"nameFDSAFDSAFDSA"'在WORD第一个表格的第2行第1列插入文本

   .Tables

(1).Cell(2,2).Range.InsertAfter"MM"

   .Range(Start:

=.Paragraphs

(2).Range.Start+3,End:

=.Paragraphs

(2).Range.Start+8).Font.Size=30'给选定的文本(第二段开始位置加3至8的字体)设置字体,其它的累推

   .Tables

(1).Cell(2,1).Range.Font.Size=20'给选定的单元格设置字体,其它的累推

EndWith

MyWord.Visible=True

SetMyWord=Nothing

SetMyWordBook=Nothing

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

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

OptionExplicit

PrivateSubCommand1_Click()

DimfilenameAsString

CD.ShowSave

filename=CD.filename

OutWordfilename

MsgBox"OK"

EndSub

PrivateFunctionOutWord(ByValfilePathAsString)AsBoolean

DimnewDocAsWord.Document

SetnewDoc=NewWord.Document

WithnewDoc

.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.InsertAftervbCrLf&"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.AddRange:

=.Range(Start:

=.Range.End-1,End:

=.Range.End),NumRows:

=1,NumColumns:

=3,DefaultTableBehavior:

=wdWord9TableBehavior,AutoFitBehavior:

=wdAutoFitFixed

With.Tables

(1)

If.Style<>"表(格子)"Then

.Style="表(格子)"

EndIf

.ApplyStyleHeadingRows=True

.ApplyStyleLastRow=True

.ApplyStyleFirstColumn=True

.ApplyStyleLastColumn=True

.Columns.Width=50

.Rows.Height=20

EndWith

.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.AddRange:

=.Range(Start:

=.Range.End-1,End:

=.Range.End),NumRows:

=8,NumColumns:

=2,DefaultTableBehavior:

=wdWord9TableBehavior,AutoFitBehavior:

=wdAutoFitFixed

With.Tables

(2)

If.Style<>"表(格子)"Then

.Style="表(格子)"

EndIf

.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&"年月日"

EndWith

EndWith

newDoc.SaveAsfilePath

newDoc.Close

EndFunction

2、

VB程序操作word表格(文字、图片)

         很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google、baidu搜爆了,都没有找到我需要的东西。

到是搜索到了很多问这个问题的记录。

没办法,索性只有自己去尝试了。

下面把一些代码发上来,给需要的朋友一点提示

         打开一个已经存在的wrod文件(这个文件包含了表格)

DimWordApp

DimWord

SetWordApp=CreateObject("Word.Application")

WordApp.Visible=True

SetWord=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中录制的宏拷贝过来的,但是在生成表格时编译通不过,代码如下:

DimwdAppAsWord.Application

DimwdBookAsWord.Document

DimRangeAsRange

DimNumRowsAsLong

DimNumColumnsAsLong

SetwdApp=CreateObject("Word.Application")

SetwdBook=wdApp.Documents.Add

wdApp.Visible=True

....

....

--〉wdApp.ActiveDocument.Tables.AddRange:

=Selection.Range,NumRows:

=16,NumColumns_

:

=5,DefaultTableBehavior:

=wdWord9TableBehavior,AutoFitBehavior:

=_

wdAutoFitWindow

运行时上面这句报实时错误'91'

对象变量或with块变量未设置

该如何写?

  

对策:

看你的代码是想增加一个表格,VBA的代码和VB的代码是不一样的,需要转换一下,多用几次就清楚了。

应该这样写

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

问题:

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

能不能给出一些代码?

  

对策:

接着上次那个程序来。

SetmySelection=wdApp.Documents.Application.Selection

mySelection.Cells.Borders(-7).LineStyle=1

'选中表格的第2行第3列

table.Cell(2,3).Select

'向下移动6格,第1个参数和第3个是常数

CallwdBook.Application.Selection.MoveDown(5,6,1)

'合并

wdBook.Application.Selection.Cells.Merge

'拆分成7行2列

CallwdBook.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列合并为一个单元格,望楼主能详细写一下代码。

非常感谢!

我的代码如下:

DimwdAppAsWord.Application

DimwdBookAsWord.Document

SetwdApp=CreateObject("Word.Application")

SetwdBook=wdApp.Documents.Add

wdApp.Visible=True

wdApp.Selection.Font.Name="黑体"

wdApp.Selection.Font.Size=22

wdApp.Selection.ParagraphFormat.Alignment=wdAlignParagraphCenter

wdApp.Selection.TypeTextText:

="通风机调试报告"

wdApp.Selection.TypeParagraph

wdApp.Selection.TypeParagraph

wdApp.Selection.Font.Name="仿宋"

wdApp.Selection.Font.Size=12

CallwdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range,NumRows:

=27,NumColumns_

:

=7,DefaultTableBehavior:

=wdWord9TableBehavior,AutoFitBehavior:

=_

wdAutoFitFixed)

SetmySelection=wdApp.Documents.Application.Selection

mySelection.Cells.Borders(-7).LineStyle=1

'选中表格的第2行第3列

-->Table.Cell(2,3).Select

'向下移动6格,第1个参数和第3个是常数

CallwdBook.Application.Selection.MoveDown(5,6,1)

'合并

wdBook.Application.Selection.Cells.Merge

'拆分成7行2列

CallwdBook.Application.Selection.Cells.Split(7,2,True)

SetwdBook=Nothing

对策:

不好意思啊,上次在自己机器上测试了一下,没有拷全部代码,不过就这个应该也能理解到呀,table就是要操作的那个table,一个word里面有可能有多个table,我们首先要选中要操作的那个table,我们这个table是自己用代码生成的,所以有一个方便的代码就是

dimTable

setTable=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里添加多个表格,因为数据的列数较多,希望能

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

部分代码如下:

ForI=1ToINT_COL

CallMyWord.ActiveDocument.Tables.Add(MyWord.Application.Selection.Range,NumRows:

=30,_

NumColumns:

=8,DefaultTableBehavior:

=wdWord9TableBehavior,_

AutoFitBehavior:

=wdAutoFitFixed)

WithMyWord.Application.Selection.Tables

(1)

If.Style<>"网格型"Then

.Style="网格型"

EndIf

.ApplyStyleHeadingRows=True

.ApplyStyleLastRow=True

.ApplyStyleFirstColumn=True

.ApplyStyleLastColumn=True

EndWith

MyWord.Application.Selection.MoveDown Unit:

=wdLine,Count:

=30

MyWord.Application.Selection.TypeParagraph

NextI

但是,每次程序执行到MyWord.Application.Selection.MoveDown Unit:

=wdLine,Count:

=30这一句,就跳出程序。

为什么?

另:

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

    

对策:

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

ForI=1ToINT_COL

CallMyWord.ActiveDocument.Tables.A

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1