vb操作WORD大全


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

Dim WordApp
Dim Word
Set WordApp =CreateObject("Word.Application")
WordApp.Visible =True
Set Word = WordApp.Documents.Open("c:\record.dot")

知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容

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

set table=wdApp.ActiveDocument.Tables(1)

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

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

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

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

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

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


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

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做更多的应用,却又不知道怎样实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然后查看宏,一目了然了吧! ------------------
问题:

'选中表格的第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

.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 <> "表 (格子)" 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

Selection.TypeParagraph
Next n

.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唯品会

我在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

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)
问题:我按你如上所说的方法去做,但是在执行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

.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


对策:

不好意思啊,前次在自己机器上测试了一下,没有拷全部代码,不过就这个应该也能理解到呀,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做一个模板,把表格什么的全都先写好,然后保存成模板文件。然后你再用顺序加载这个模板,然后往模板里填写数据。这样难度要低一些。不过具体情况具体阐发。(个人意见)

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

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

部门代码如下:

 

但是,每次顺序执行到MyWord.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@.163.com qq:26749732
下面是我的代码 :
For n = 1 To 6
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=10+n, NumColumns:= _
6, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed

.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

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

2、

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

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

Word.Tables(1).cell(1, 2)="内容"

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

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

来源: http://hi.baidu.com/%B0%CB%CD%F2%C0%EF/blog/item/92c89358784312362934f00b.html


中国服装网联合瑞丽杂志
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目前,GitHub Copilot 是一个由 OpenAI 和 GitHub 合作开发的代码智能助手,它目前仅支持在 Visual Studio Code 编辑器中使用。而 Windows 11 是微软最新发布的操作系统,因此您可以按照以下步骤在 Windows 11 上安装 Copilot: 1. 安装 Visual Studio Code:访问 Visual Studio Code 官方网站(https://code.visualstudio.com/)下载最新版本的 Visual Studio Code 编辑器。根据您的操作系统选择合适的安装程序进行下载和安装。确保您选择适用于 Windows 11 的版本。 2. 打开 Visual Studio Code:安装完成后,打开 Visual Studio Code 编辑器。 3. 安装 Copilot 插件:在 Visual Studio Code 编辑器中,点击左侧的扩展图标(四个方块连接在一起的图标),搜索并找到 "GitHub Copilot" 插件。点击插件卡片右侧的 "Install" 按钮进行安装。 4. 登录 GitHub 账号:安装完成后,点击 Visual Studio Code 编辑器左下角的 GitHub Copilot 图标,然后点击 "Sign in with GitHub" 进行登录。确保您使用的是与 Copilot 关联的 GitHub 账号进行登录。 5. 使用 Copilot:安装和登录完成后,您可以在编写代码时享受 Copilot 提供的智能代码建议和补全功能。Copilot 可以根据上下文和您输入的代码,生成相关的代码片段和建议。 请注意,GitHub Copilot 目前仍然处于技术预览阶段,可能会有一些限制和使用上的问题。确保您已经阅读并理解了 Copilot 的使用条款和隐私政策。 希望这些步骤能帮助您在 Windows 11 上安装和使用 Copilot。如果您遇到任何问题,请及时与我联系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值