1.怎样在EXCEL的VBA中打开一个WORD文件
'首先要在vba中引用Microsoft Word 11.0 Object LibraryDim Wdapp As Word.ApplicationDim WdDocument As Word.DocumentDim UserFile As StringPrivate Sub CommandButton1_Click() Set Wdapp = New Word.Application UserFile = Wdapp.Path & "\1.doc" Set WdDocument = Wdapp.Documents.Open("c:\1.doc") Wdapp.Visible = TrueEnd Sub。
2.如何在EXECL中用VBa打开Word,并输出数据到WORD中,保存,关闭
在EXcel的VB编辑器中插入一个模块,输入如下代码试试看。
Sub ExcelToWord()
Dim WordObject As Object '声明一个对象变量,这里即将声明为Word对象
On Error Resume Next
Set WordObject = CreateObject("Word.Application") '用set来创建Word对象,这里是运行Word程序,但未新建文档
WordObject.Visible = 0 '后台运行Word对象,只在任务管理器中存在WinWord.exe进程,但在任务栏上看不到word;如果为1或者True则可以看到word运行界面
WordObject.Documents.Add DocumentType:=wdNewBlankDocument '新建一word文档
'以下为获取Excel表格中的内容,准备把数据传送给Word,可以根据自己的实际需要定制代码,这里只是示例代码
Excel.Application.Sheets(1).Activate '切换当前电子表格的表1为当前激活表
Excel.Application.Sheets(1).UsedRange.Select '选中当前激活表的所有数据
Selection.Copy '将选中的区域进行复制
WordObject.Application.Activate '将后台运行的Word激活为当前窗口
WordObject.ActiveWindow.Selection.Paste '将刚才从Excel中复制进剪贴板中的内容粘贴进word中来
WordObject.Saved = True '将保存文档的Saved属性设置为True,这样后台运行的Word在保存文档时就不会弹出是否保存的对话框了,达到悄无声息的效果
WordObject.ActiveDocument.SaveAs "D:\temp\导出数据.doc" '调用saveas命令保存文档,根据实际,指定文档的保存路径和名称
WordObject.Application.Quit '退出并关闭程序文档
Set WordObject = Nothing '释放对象
End Sub
3.excel中如何通过VBA打开word文件和ppt文件
一、打开word文件代码:Set wo = CreateObject("Word.Application")
wo.Documents.Open ThisWorkbook.Path & "\流程.doc"
wo.Visible = True
二、打开ppt文件代码:方法1:
Set wo = CreateObject("Powerpoint.Application")
wo.Visible = True
wo.Presentations.Open ThisWorkbook.Path & filename方法2:Sub dd()Dim filepath$, filename$
filepath = Chr(34) & ThisWorkbook.Path & filename & Chr(34)
Shell "POWERPNT.EXE " & filepathEnd Sub附:双击打开PPS文件,在演示完后退出PPS时并没有PowerPoint主窗口保留,但在Excel中使用VBA打开的PPS文件,在演示完PPS退出后,PowerPoint主窗口仍然打开。
这里使用一个循环判断演示窗口是否存在,加上错误捕捉程序来处理上面这个问题。
PrivateSub CommandButton1_Click()
Dim wo AsObject Dim app AsObject
' 创建PowerPoint应用实例
Set app = CreateObject("Powerpoint.Application")
' 使PowerPoint可见
app.Visible = True ' 打开PPS文件
Set wo = app.Presentations.Open(ThisWorkbook.Path & "\a.pps")
' 当PPS演示结束时,wo对象的SlideShowWindow不存在,捕捉到错误
OnErrorGoTo errHandle
' PPS演示时全屏
4.excel VBA用何种代码实现调用word中的内容
只要定义一个Word.Application对象,
Set WordApp = CreateObject("Word.Application")
然后操作这个WordApp 对象就可以了,就像在Word中使用VBA一样,
打开文件用 WordApp.Documents.Open 文件路径
定位主要用 WordApp.Selection.Move系列指令,也可以用WordApp.Selection.Goto
具体的你可以用录制宏的方法在Word中看看用什么代码合适。
5.怎么能运用excel的vba读取word文档中的内容
Sub AAA() Dim FilePath As String '要读取的文件路径 Dim S1 As String '文档的内容 Dim S2 As String '提取到的内容 Dim Ar As Variant '用于保存最终结果 Dim L1 As Long '记录当前查找到的字符位置 FilePath = Application.GetSaveAsFilename(fileFilter:="Word文档,*.doc;*.docx") If FilePath = "False" Then MsgBox "您没有选择文件,将退出程序。
": Exit Sub With CreateObject("word.application") With .Documents.Open(FilePath, True, True) S1 = .Content .Close False End With .Quit End With L1 = InStr(S1, "<") '第一个 < 位置="" do="" until="" l1="0" if="" len(s2)=""><> 0 Then S2 = S2 & "Crazy0qwer" & Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1) Else S2 = Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1) End If L1 = InStr(L1 + 1, S1, "<") Loop Ar = Split(S2, "Crazy0qwer") Range("A1").Resize(UBound(Ar) + 1) = Application.Transpose(Ar)End Sub。
6.VBA excel调用word内容
在有word文件的文件夹中新建一个excel工作簿,打开工作簿,按Alt+F11,把下面的代码复制进去,按F5执行。
代码会复制work文件的前300个字符到excel中。
Sub test()
Dim i%, myName$, myPath$, AppWord As Object
Set AppWord = CreateObject("Word.Application")
myPath = ThisWorkbook.Path & "\"
myName = Dir(myPath & "*.doc*")
With ActiveSheet
.Columns("A:B").ClearContents
Do While myName ""
AppWord.Documents.Open Filename:=myPath & myName
i = i + 1
.Cells(i, 1) = myName
.Cells(i, 2) = AppWord.ActiveDocument.Range(Start:=0, End:=300).Text
AppWord.ActiveDocument.Close False
myName = Dir
Loop
End With
AppWord.Quit
Set AppWord = Nothing
MsgBox "已完成。"
End Sub
7.怎么能运用excel的vba读取word文档中的内容
Sub AAA()
Dim FilePath As String '要读取的文件路径
Dim S1 As String '文档的内容
Dim S2 As String '提取到的内容
Dim Ar As Variant '用于保存最终结果
Dim L1 As Long '记录当前查找到的字符位置
FilePath = Application.GetSaveAsFilename(fileFilter:="Word文档,*.doc;*.docx")
If FilePath = "False" Then MsgBox "您没有选择文件,将退出程序。": Exit Sub
With CreateObject("word.application")
With .Documents.Open(FilePath, True, True)
S1 = .Content
.Close False
End With
.Quit
End With
L1 = InStr(S1, "<;") '第一个 <; 位置
Do Until L1 = 0
If Len(S2) <> 0 Then
S2 = S2 & "Crazy0qwer" & Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
Else
S2 = Mid(S1, L1 + 1, InStr(L1, S1, ">") - L1 - 1)
End If
L1 = InStr(L1 + 1, S1, "<")
Loop
Ar = Split(S2, "Crazy0qwer")
Range("A1").Resize(UBound(Ar) + 1) = Application.Transpose(Ar)
End Sub
转载请注明出处51数据库 » vbaexcel中打开word
下面给你吃好吗