ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 9953|回复: 11

[讨论] 在excel-VBA遍历Word.

[复制链接]

TA的精华主题

TA的得分主题

发表于 2010-1-19 06:53 | 显示全部楼层 |阅读模式
下面这个程序,只能遍历.ActiveDocument.Tables
问:如何改.ActiveDocument.Tables,就能遍历所有内容。
Sub MyTable()
  Dim Wrd As Word.Application
  Dim MyRange As Word.Range  
  Dim pos As Long
  Set Wrd = GetObject(, "word.application")
  With Wrd
    Debug.Print .ActiveDocument.Name
    For Each rr In .ActiveDocument.Tables
      Debug.Print TypeName(rr)
    Next rr
  End With
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-1-19 06:55 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
例如这个帖子:http://club.excelhome.net/thread-210793-1-1.html
Application对象
- - - - - - - - - - - - - - - - - - - -
(301) Application.ActivePrinter ‘获取当前打印机
(302) Application.Height '当前应用程序文档的高度
(303) Application.Width ‘当前应用程序文档的宽度
(304) Application.Build ‘获取Word版本号和编译序号
(305) Application.Caption ‘当前应用程序名
(306) Application.DefaultSaveFormat '返回空字符串,表示Word文档
(307) Application.DisplayRecentFiles '返回是否显示最近使用的文档的状态
(308) Application.Documents.Count '返回当前打开的文档数
(309) Application.FontNames.Count ‘返回当前可用的字体数
(310) Application.Left ‘返回当前文档的水平位置
(311) Application.MacroContainer.FullName '返回当前文档名,包括所在路径
(312) Application.NormalTemplate.FullName '返回文档标准模板名称及所在位置
(313) Application.RecentFiles.Count '返回最近打开的文档数目
(314) Application.System.CountryRegion '返回应用程序所在的地区代码
(315) Application.System.FreeDiskSpace ‘返回应用程序所在磁盘可用空间
(316) Application.System.HorizontalResolution '返回显示器的水平分辨率
(317) Application.System.VerticalResolution '返回显示器的垂直分辨率
(318) Application.System.LanguageDesignation '返回系统所使用的语言
(319) Application.System.MathCoprocessorInstalled ‘返回系统是否安装了数学协处理器
(320) Application.System.OperatingSystem ‘返回当前操作系统名
(321) Application.System.ProcessorType '返回计算机处理器名
(322) Application.System.Version ‘返回操作系统的版本号
(323) Application.Templates.Count '返回应用程序所使用的模板数
(324) Application.UserName '返回应用程序用户名
(325) Application.Version ‘返回应用程序的版本号
- - - - - - - - - - - - - - - - - - - - - -
Documents/Document对象
- - - - - - - - - - - - - - - - - - - - - -
(326) ActiveDocument.AttachedTemplate.FullName '返回当前文档采用的模板名及模板所在位置
(327) ActiveDocument.Bookmarks.Count '返回当前文档中的书签数
(328) ActiveDocument.Characters.Count '返回当前文档的字符数
(329) ActiveDocument.CodeName ‘返回当前文档的代码名称
(330) ActiveDocument.Comments.Count ‘返回当前文档中的评论数
(331) ActiveDocument.Endnotes.Count '返回当前文档中的尾注数
(332) ActiveDocument.Fields.Count '返回当前文档中的域数目
(333) ActiveDocument.Footnotes.Count ‘返回当前文档中的脚注数
(334) ActiveDocument.FullName '返回当前文档的全名及所在位置
(335) ActiveDocument.HasPassword '当前文档是否有密码保护
(336) ActiveDocument.Hyperlinks.Count '返回当前文档中的链接数
(337) ActiveDocument.Indexes.Count '返回当前文档中的索引数
(338) ActiveDocument.ListParagraphs.Count '返回当前文档中项目编号或项目符号数
(339) ActiveDocument.ListTemplates.Count '返回当前文档中使用的列表模板数
(340) ActiveDocument.Paragraphs.Count '返回当前文档中的段落数
(341) ActiveDocument.Password=XXX '设置打开文件使用的密码
(342) ActiveDocument.ReadOnly '获取当前文档是否为只读属性
(343) ActiveDocument.Saved '当前文档是否被保存
(344) ActiveDocument.Sections.Count '当前文档中的节数
(345) ActiveDocument.Sentences.Count ‘当前文档中的语句数
(346) ActiveDocument.Shapes.Count '当前文档中的形状数
(347) ActiveDocument.Styles.Count '当前文档中的样式数
(348) ActiveDocument.Tables.Count ‘当前文档中的表格数
(349) ActiveDocument.TablesOfAuthorities.Count ‘返回当前文档中的引文目录数
(350) ActiveDocument.TablesOfAuthoritiesCategories.Count ‘返回当前文档中引文目录类别数
(351) ActiveDocument.TablesOfContents.Count ‘返回当前文档中的目录数
(352) ActiveDocument.TablesOfFigures.Count '返回当前文档中的图表目录数
- - - - - - - - - - - - - - - - - - - - - -
Paragraphs/Paragraph对象
- - - - - - - - - - - - - - - - - - - - - -
(353) Selection.Paragraphs.Count '返回所选区域的段落数
(354) Selection.Paragraphs.First '返回所选区域中的第一段
(355) ActiveDocument.Paragraphs(1).LeftIndent '返回当前文档中第一段的左缩进值
(356) ActiveDocument.Paragraphs(1).LineSpacing '返回当前文档中第一段的行距
(357) ActiveDocument.Paragraphs(1).OutlineLevel  ‘返回或设置当前文档中第一段的大纲级别
(358) ActiveDocument.Paragraphs(1).RightIndent ‘返回当前文档中第一段的右缩进量
(359) ActiveDocument.Paragraphs(1).SpaceBefore '返回当前文档中第一段的段前间距
(360) ActiveDocument.Paragraphs(1).SpaceAfter ‘返回当前文档中第一段的段后间距
(361) ActiveDocument.Paragraphs(1).Range.Text '返回当前文档中第一段的内容
(362) ActiveDocument.Paragraphs(1).Range.Style.NameLocal '返回当前文档中第一段应用的样式名
(363) ActiveDocument.Paragraphs(1).Range.Style.Description '返回当前文档中第一段所应用样式的详细描述
(364) ActiveDocument.Paragraphs(1).Range.Style.Font.Name '返回当前文档中第一段所应用样式的字体名
(365) ActiveDocument.Paragraphs(1).Range.Style.Font.NameFarEast '返回或设置一种东亚字体名
(366) ActiveDocument.Paragraphs(1).Range.Style.Font.Size '返回或设置当前文档中第一段所应用样式的字体大小
(367) ActiveDocument.Paragraphs(1).Range.Style.Font.Spacing '返回或设置字符间距
(368) Selection.Words.Count '所选区域的字数
- - - - - - - - - - - - -
Sentences对象
- - - - - - - - - - - - -
(369) Selection.Sentences.Item(1) '所选区域中的第一句的内容
- - - - - - - - - -
Words对象
- - - - - - - - - -
(371) ActiveDocument.Words(1).Select '选择当前文档中的第一个词
(372) ActiveDocument.Range.Words(1).InsertAfter "我爱你!" '在当前文档中的第一个词后插入“我爱你”
- - - - - - - - - - - - -
Characters对象
- - - - - - - - - - - - -
(373) Selection.Characters.Count '当前文档中所选区域的字符数
(374) ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter '在当前文档的第一段之后插入一个新段落
- - - - - - - - - - - - - - - - - -
Sections/Section对象
- - - - - - - - - - - - - - - - - -
(375) ActiveDocument.Sections.First '当前文档的第一节
(376) ActiveDocument.Sections.First.PageSetup.BottomMargin '当前文档第一节所在页的底边距
(377) ActiveDocument.Sections.First.PageSetup.LeftMargin '当前文档第一节所在页的左边距
(378) ActiveDocument.Sections.First.PageSetup.RightMargin '当前文档第一节所在页的右边距
(379) ActiveDocument.Sections.First.PageSetup.TopMargin '当前文档第一节所在页的顶边距
(380) ActiveDocument.Sections.First.PageSetup.PaperSize '返回或设置当前文档第一节所在页的大小
(381) ActiveDocument.Sections.First.PageSetup.PageHeight '返回或设置当前文档第一节所在页的高度
(382) ActiveDocument.Sections.First.PageSetup.PageWidth '返回或设置当前文档第一节所在页的宽度
(383) ActiveDocument.Sections.Add Range:=myRange '在当前文档中添加新节
(384) ActiveDocument.Sections.Item(2) '当前文档中的第二节
(385) ActiveDocument.Sections.Last.Range.InsertAfter "文档结束!" '在当前文档中最后一节的结尾添加文字“文档结束!”
- - - - - - - - - -
Range对象
- - - - - - - - - -
(386) ActiveDocument.Range(Start:=0, End:=10) '表示当前文档前10个字符所组成的一个Range对象
(387) Set myRange = ActiveDocument.Range( _
    Start:=ActiveDocument.Paragraphs(2).Range.Start, _
End:=ActiveDocument.Paragraphs(4).Range.End) '将当前文档第2段至第4段设置为一个Range对象
(388) ActiveDocument.Paragraphs(1).Range.Copy  '复制当前文档中的第一段
(389) Selection.Copy
Documents.Add.Content.Paste '复制所选内容到新文档中
(390) ActiveDocument.Bookmarks("Book1").Copy Name:="Book2" '将Book2书签复制Book1书签标记的位置
(391) Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4 '将所选内容移至文档中的第4行
(392) Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext '将所选内容移至下一个表格的第1个单元格
(393) Selection.Range.AutoFormat '为所选内容套用格式
(394) ActiveDocument.Content.Font.Name = "Arial" '将当前文档的字体设置为斜体
(395) ActiveDocument.Content.Select
Selection.Delete '将当前文档中的内容删除
- - - - -
其它
- - - - -
(396) Documents.Add '添加一个新文档
(397) Set myTable = ActiveDocument.Tables.Add(Selection.Range, 2, 2) '在当前文档所选区域添加一个2行2列的表格
(398) Open "C:\my.txt" For Input As #1 '打开一个用于输入的文件并令其编号为1
(399) Line Input #1, TextLine '读取被打开用于输入且编号为1的文件
(400) Close #1  '关闭编号为1的文件

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-1-19 07:02 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
For Each rr In .ActiveDocument.Application 不成立
For Each rr In .ActiveDocument.Range不成立
For Each rr In .ActiveDocument.Paragraphs成立—— 结果Paragraph
For Each rr In .ActiveDocument.Sentences成立—— 结果为Range
For Each rr In .ActiveDocument.Tables    ——结果为Table
For Each rr In .ActiveDocument.Bookmarks        ——  Bookmark
For Each rr In .ActiveDocument.Sections          ——  Section

[ 本帖最后由 ningyong58 于 2010-1-19 07:54 编辑 ]

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-1-19 07:23 | 显示全部楼层
http://club.excelhome.net/thread-524636-1-1.html
Sub 删除指定文件夹下所有Word文档的前三段()
'kqbt原创,原帖发表于2009-12-21 23:53,
'链接为http://club.excelhome.net/thread-516002-1-1.html


    Application.ScreenUpdating = False
    Dim myPath As String, I As Integer, MyDoc As Document
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "选择目标文件夹"
        If .Show = -1 Then
            myPath = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With
    With Application.FileSearch
        .LookIn = myPath
        .FileType = msoFileTypeWordDocuments
        If .Execute > 0 Then
            For I = 1 To .FoundFiles.Count
                Set MyDoc = Documents.Open(FileName:=.FoundFiles(I), Visible:=False)
                MyDoc.Range(MyDoc.Paragraphs(1).Range.start, MyDoc.Paragraphs(3).Range.End).Delete
                MyDoc.Close True
            Next
        End If
    End With
    Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

发表于 2016-4-7 15:47 | 显示全部楼层
楼主看看这个写法有什么问题



Sub Books2Sheets()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)



With fd
If .Show = -1 Then
Dim vrtSelectedItem As Variant


For Each vrtSelectedItem In .SelectedItems '遍历打开被选择文档
Dim tempwb As Document
Set tempwb = Document.Open(vrtSelectedItem)
'-------------------------------------------------------------------
这里是打开文档后我想要做的事

'-------------------------------------------------------------------
'关闭被合并工作簿
tempwb.Close SaveChanges:=False

Next vrtSelectedItem
End If
End With

Set fd = Nothing


End Sub

TA的精华主题

TA的得分主题

发表于 2016-4-7 16:04 | 显示全部楼层
ningyong58 发表于 2010-1-19 07:23
http://club.excelhome.net/thread-524636-1-1.html
Sub 删除指定文件夹下所有Word文档的前三段()
'kqbt ...

楼主看到我~

TA的精华主题

TA的得分主题

发表于 2016-4-8 06:11 | 显示全部楼层
excelwheep 发表于 2016-4-7 15:47
楼主看看这个写法有什么问题

问一句  你的代码放在哪里  word   or  Excel

TA的精华主题

TA的得分主题

发表于 2016-4-8 10:50 | 显示全部楼层
闻启学 发表于 2016-4-8 06:11
问一句  你的代码放在哪里  word   or  Excel

我在excel,我要把一大批word文档的表格整理到excel中~

TA的精华主题

TA的得分主题

发表于 2016-4-8 22:04 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 闻启学 于 2016-4-8 22:06 编辑
excelwheep 发表于 2016-4-8 10:50
我在excel,我要把一大批word文档的表格整理到excel中~

那我晕倒了 在Excel  直接用Word 的对象  这个好比 你用我的杯子一样

TA的精华主题

TA的得分主题

发表于 2016-4-11 13:57 | 显示全部楼层
闻启学 发表于 2016-4-8 22:04
那我晕倒了 在Excel  直接用Word 的对象  这个好比 你用我的杯子一样

高手,那要怎么办,要声明是吗~
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-18 00:11 , Processed in 0.036675 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表