Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3296|回复: 2

[已解决]在Excel VBA中建立word文件

[复制链接]
发表于 2013-8-26 08:57 | 显示全部楼层 |阅读模式
本帖最后由 kevin_2010 于 2013-8-26 11:04 编辑

Sub re()
Dim tes As Object, fd As Object
Dim filename As String
filename = ThisWorkbook.Path & "\tt.doc"
Set tes = CreateObject("word.application")
Set fd = CreateObject("word.Document")
tes.Visible = True
fd = tes.Application.documents.Add
fd.SaveAs filename
Set tes = Nothing
End Sub

以上有“应用程序定义或者对象定义错误”,为什么?我在网上摘抄的啊,这样都不行?
***************************************************
不好意思,题目写错了:(

我加了一个 on error resume next,结果就没有这个报错了,功能也正常。但只是不知道为什么会有这个报错。
最佳答案
2013-8-26 12:06
  1. Sub re()
  2.     Dim tes As Object, fd As Object
  3.     Dim filename As String
  4.     filename = ThisWorkbook.Path & "\tt.doc"
  5.     Set tes = CreateObject("word.application")
  6.     'Set fd = CreateObject("word.Document")
  7.     With tes
  8.         '.Visible = True
  9.         Set fd = .documents.Add
  10.         fd.SaveAs filename
  11.         .Quit
  12.     End With
  13.     Set tes = Nothing
  14. End Sub
复制代码
发表于 2013-8-26 10:15 | 显示全部楼层
在word工具栏里有个插入excel的按钮,可以根据需要点击直接插入excel表格。
word插入excel表格.gif
回复

使用道具 举报

发表于 2013-8-26 12:06 | 显示全部楼层    本楼为最佳答案   
  1. Sub re()
  2.     Dim tes As Object, fd As Object
  3.     Dim filename As String
  4.     filename = ThisWorkbook.Path & "\tt.doc"
  5.     Set tes = CreateObject("word.application")
  6.     'Set fd = CreateObject("word.Document")
  7.     With tes
  8.         '.Visible = True
  9.         Set fd = .documents.Add
  10.         fd.SaveAs filename
  11.         .Quit
  12.     End With
  13.     Set tes = Nothing
  14. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 18:12 , Processed in 0.124206 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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