vb操作word大全

2020-03-27 19:50

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

Option Explicit

Private Sub Command1_Click() Dim filename As String CD.ShowSave

filename = CD.filename OutWord filename MsgBox \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 \編号:\

.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 & \報告\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 \项目名称:\.Content.InsertAfter \应急类型:\

.Content.InsertAfter \预警状态:正常/警界/危机\

.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 <> \表 (格子)\.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 \委 托 人:\.Content.InsertAfter \预 警 机 构:\.Content.InsertAfter \报告负责人:\.Content.InsertAfter \时 间:\

.Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft

.Tables.Add Range:=.Range(Start:=.Range.End - 1, End:=.Range.End), NumRows:=8, NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed With .Tables(2)

If .Style <> \表 (格子)\.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

.Range.Cells(4).Row.Cells.Merge

.Range.Cells(4).Range.Text = \情况描述/检索结果:\.Range.Cells(5).Row.Cells.Merge

.Range.Cells(5).Range.Text = \影响分析:\.Range.Cells(6).Row.Cells.Merge

.Range.Cells(6).Range.Text = \建议:\vbCrLf & vbCrLf

.Range.Cells(7).Row.Cells.Merge

.Range.Cells(7).Range.Text = \专家组成员:\

vbCrLf & vbCrLf & vbCrLf

.Range.Cells(8).Row.Cells.Merge

.Range.Cells(8).Range.Text = \附件目录:\& vbCrLf & vbCrLf

.Range.Cells(9).Row.Cells.Merge

.Range.Cells(9).Range.Text = \报告负责人:\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(\WordApp.Visible = True

Set Word = WordApp.Documents.Open(\

知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容 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 (\

微软的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用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(\ 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(\ 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, 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是自己用代码生成的,所以


vb操作word大全.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:癌症的级别 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: