如何在Excel VBA 中读写word文档 步骤
2个回答
展开全部
1. 库的配置
在默认情况下,新创建的excel vba中不支持定义word对象。
所以需要先引入word库,操作步骤如下:
1.1 打开excel vba 界面
1.2 选中其中的一个Module
1.3 选择菜单, Tools --> References
在打开的对话框中选择类似 "Microsoft Word 14.0 Object Library".
1.4 点击OK保存配置。
2. 打开文档
Set wordApplication = CreateObject("Word.Application")
wordApplication.Visible = False
Dim hasOpenDoc As Boolean
hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend
If hasOpenDoc = True then
Set wordDoc = GetObject(filePath)
End if
If hasOpenDoc = False Then
Set wordDoc = wordApplication.Documents.Open(filePath)
End if
wordDoc.Active
With wordApplication
Dim aParagraph As Word.Paragraph
For Each aParagraph In wordDoc.Paragraphs
' do some thing to every paragraph.
Next aParagraph
End with
wordDoc.Close
Set wordDoc = nothing
' 如下这段代码引用某位牛人的,非常感谢他。由于路径丢失,不能给出链接, 抱歉
' 如下的找寻方式,能够正确的找出文件是否被打开
Function IsOpen(fileName As String) As Boolean
IsOpen = False
Dim findFile As Integer
findFile = FreeFile()
On Error GoTo ErrOpen
Open fileName For Binary Lock Read Write As findFile
Close findFile
Exit Function
ErrOpen:
If Err.Number <> 70 Then
Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description
MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext
Else
IsOpen = True
End If
End Function
在默认情况下,新创建的excel vba中不支持定义word对象。
所以需要先引入word库,操作步骤如下:
1.1 打开excel vba 界面
1.2 选中其中的一个Module
1.3 选择菜单, Tools --> References
在打开的对话框中选择类似 "Microsoft Word 14.0 Object Library".
1.4 点击OK保存配置。
2. 打开文档
Set wordApplication = CreateObject("Word.Application")
wordApplication.Visible = False
Dim hasOpenDoc As Boolean
hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend
If hasOpenDoc = True then
Set wordDoc = GetObject(filePath)
End if
If hasOpenDoc = False Then
Set wordDoc = wordApplication.Documents.Open(filePath)
End if
wordDoc.Active
With wordApplication
Dim aParagraph As Word.Paragraph
For Each aParagraph In wordDoc.Paragraphs
' do some thing to every paragraph.
Next aParagraph
End with
wordDoc.Close
Set wordDoc = nothing
' 如下这段代码引用某位牛人的,非常感谢他。由于路径丢失,不能给出链接, 抱歉
' 如下的找寻方式,能够正确的找出文件是否被打开
Function IsOpen(fileName As String) As Boolean
IsOpen = False
Dim findFile As Integer
findFile = FreeFile()
On Error GoTo ErrOpen
Open fileName For Binary Lock Read Write As findFile
Close findFile
Exit Function
ErrOpen:
If Err.Number <> 70 Then
Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description
MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext
Else
IsOpen = True
End If
End Function
博思aippt
2024-07-20 广告
2024-07-20 广告
博思AIPPT是基于ai制作PPT的智能在线工具,它提供了4种AI制作PPT的方式,包括AI生成大纲、AI直接生成PPT、文本生成PPT、AI提炼word文档生成PPT,一站式集成多种AI生成PPT的方式,可满足办公用户的不同需求和使用场景...
点击进入详情页
本回答由博思aippt提供
2017-07-24
展开全部
参考这个:
Sub ExcelToWord() ' 利用Word程序创建文本文件
Dim WordApp As Object
Dim Records As Integer, i As Integer
Dim Region As String, SalesAmt As String, SalesNum As String, strTitle As String
Set WordApp = CreateObject("Word.Application") '创建word对象
Records = Application.CountA(Sheets("sheet1").Range("A:A")) 'A列数据个数
WordApp.Documents.Add '新建文档
'写Title
strTitle = Cells(1, 5)
With WordApp.Selection
.Font.Size = 28
.ParagraphFormat.Alignment = 1 '左对齐0 居中1 右对齐2
.Font.Bold = True
.TypeText Text:=strTitle
.TypeParagraph
End With
'写内容
For i = 1 To Records
'Region = Data.Cells(i, 1).Value '将第一列某行的值赋值给变量
Region = Cells(i, 1)
'SalesNum = Data.Cells(i, 2).Value '获取该行B列数据
SalesNum = Cells(i, 2)
'SalesAmt = Data.Cells(i, 3).Value '获取该行C列数据
SalesAmt = Cells(i, 3)
With WordApp.Selection
.Font.Size = 14 '设置字体字号
.Font.Bold = True '字体粗
.ParagraphFormat.Alignment = 0 '设置对齐
.TypeText Text:=Region & SalesNum
.TypeParagraph
.Font.Size = 12 '设置字体
.ParagraphFormat.Alignment = 0 '设置对齐
.Font.Bold = False '字体不加粗
.TypeText Text:=vbTab & SalesAmt
.TypeParagraph '回车
.TypeParagraph '回车
End With
Next i
WordApp.ActiveDocument.SaveAs Filename:="AAA" '保存文件
WordApp.Quit '退出程序
Set WordApp = Nothing '清空
MsgBox "文件保存在<a href="https://www.baidu.com/s?wd=%E6%88%91%E7%9A%84%E6%96%87%E6%A1%A3&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ny7WmhFhnHu-njcLnjDL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRLPHDknHmznWT4rjbkrH6zn0" target="_blank" class="baidu-highlight">我的文档</a>底下的AAA文件"
End Sub
Sub ExcelToWord() ' 利用Word程序创建文本文件
Dim WordApp As Object
Dim Records As Integer, i As Integer
Dim Region As String, SalesAmt As String, SalesNum As String, strTitle As String
Set WordApp = CreateObject("Word.Application") '创建word对象
Records = Application.CountA(Sheets("sheet1").Range("A:A")) 'A列数据个数
WordApp.Documents.Add '新建文档
'写Title
strTitle = Cells(1, 5)
With WordApp.Selection
.Font.Size = 28
.ParagraphFormat.Alignment = 1 '左对齐0 居中1 右对齐2
.Font.Bold = True
.TypeText Text:=strTitle
.TypeParagraph
End With
'写内容
For i = 1 To Records
'Region = Data.Cells(i, 1).Value '将第一列某行的值赋值给变量
Region = Cells(i, 1)
'SalesNum = Data.Cells(i, 2).Value '获取该行B列数据
SalesNum = Cells(i, 2)
'SalesAmt = Data.Cells(i, 3).Value '获取该行C列数据
SalesAmt = Cells(i, 3)
With WordApp.Selection
.Font.Size = 14 '设置字体字号
.Font.Bold = True '字体粗
.ParagraphFormat.Alignment = 0 '设置对齐
.TypeText Text:=Region & SalesNum
.TypeParagraph
.Font.Size = 12 '设置字体
.ParagraphFormat.Alignment = 0 '设置对齐
.Font.Bold = False '字体不加粗
.TypeText Text:=vbTab & SalesAmt
.TypeParagraph '回车
.TypeParagraph '回车
End With
Next i
WordApp.ActiveDocument.SaveAs Filename:="AAA" '保存文件
WordApp.Quit '退出程序
Set WordApp = Nothing '清空
MsgBox "文件保存在<a href="https://www.baidu.com/s?wd=%E6%88%91%E7%9A%84%E6%96%87%E6%A1%A3&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3ny7WmhFhnHu-njcLnjDL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRLPHDknHmznWT4rjbkrH6zn0" target="_blank" class="baidu-highlight">我的文档</a>底下的AAA文件"
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询