【问题标题】:Runtime Error 438: This object does not support this property or method"运行时错误 438:此对象不支持此属性或方法”
【发布时间】:2018-05-31 13:33:03
【问题描述】:

我是 Excel 中 VBA 代码的新手。我编写了一些代码来打开一个 Word 文档,按顺序搜索值并将这些值替换为从 Excel 工作簿中提取的值。可能不是最快的方法,但它确实有效。我需要“另存为”一个不同的文件名,最终这需要使用相同的子文件上传到 SharePoint,但是当我到达那里时我会越过那座桥。以下是我的代码:

Sub SLAProposal()

ThisCustomer = ThisWorkbook.Sheets("Dashboard").Range("C9").Value
Set wordapp = CreateObject("Word.Application")
wordapp.documents.Open "C:\temp\SLATemp.docx"
wordapp.Visible = True


wordapp.Selection.Find.Text = "<<ClientName>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("Dashboard").Range("C9").Value
wordapp.Selection.EndOf


wordapp.Selection.Find.Text = "<<inrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J20").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<inrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J20").Value
wordapp.Selection.EndOf


wordapp.Selection.Find.Text = "<<afterrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("K20").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<afterrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("K20").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<otherrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("L20").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<agreement>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J7").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<hours>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J5").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<inrate>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J20").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<retainer>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J13").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<servicedescription>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("K17").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<hoursval>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J14").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<retainer>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J13").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<addons>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J15").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<total>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("J17").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<month>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("Lookup Table").Range("P1").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<year>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("Lookup Table").Range("P2").Value
wordapp.Selection.EndOf

wordapp.Selection.Find.Text = "<<maxusers>>"
wordapp.Selection.Find.Execute
wordapp.Selection = ThisWorkbook.Sheets("SLA Costing").Range("K21").Value
wordapp.Selection.EndOf

wordapp.documents.SaveAs2 "C:\temp\SLATemp1.docx"


End Sub

我收到运行时错误 438:此对象不支持此属性或方法”当它命中时:

wordapp.documents.SaveAs2 "C:\temp\SLATemp1.docx"

谁能告诉我为什么会这样?

谢谢, 史蒂文

【问题讨论】:

  • 在模块顶部添加Option Explicit这一行,然后按F9进行编译。这将帮助“强制”您正确声明和处理您的变量、对象等。一旦它完全编译,运行代码,如果您仍然无法继续,请运行代码并 edit 您的问题以及更新的代码和错误信息
  • 我通常不使用 word,但您检查过它应该是 .SaveAs2,而不是 .SaveAs
  • 您尚未指定要保存哪个文档,因为保存方法都不是 Documents 集合的成员。试试:wordapp.ActiveDocument.SaveAs2 "C:\temp\SLATemp1.docx"

标签: excel ms-word vba


【解决方案1】:

这是因为Documents 集合没有方法SaveAs。您需要处理特定的Document 对象。

为 Word 文档声明并设置一个变量:

Dim wordDoc as Word.Document
Set wordDoc = wordapp.Documents.Open("file path here")

以后可以使用这个对象专门保存这个文档:

wordDoc.SaveAs2 "file path here"

在 Sub 结束时,您应该明确释放 Word 对象:

Set wordDoc = Nothing
Set wordapp = Nothing

而且,如果您不再使用 wordapp

wordapp.Quit

如果您不wordapp.Visible = True,这一点尤其重要,因为否则 Word 应用程序将在内存中运行 - 每次宏运行一次 - 并且它们不会在 Windows 关闭(或某人使用任务管理器执行此操作)。

【讨论】:

  • 谢谢辛迪。我已经做出了建议的更改,对我来说一切都有意义。但是,现在打开 word 文档时出现错误:运行时错误 5147。抱歉,我们找不到您的文件。它是移动、重命名还是删除。文件路径绝对正确: Set wordDoc = wordapp.Documents.Open("C:\Temp\SLATemp.docx")
  • 忘记我的最后评论。这非常有效。出于某种原因,我需要创建一个新的 word 文档,一旦我这样做了,就完全没有问题了。谢谢!
  • 很高兴它正在工作...如果您想要一个新文档,请使用 wordApp.Documents.Add("filepath") 而不是 Documents.Open :-)
  • 如何打开存储在 SharePoint Online 库中的文件?通过使用库的 URL 作为文件路径,它很容易保存到库中。但是我发现使用类似的概念打开文档似乎不起作用。
  • @Stevennel 这是不同的问题,应该在新问题中提出。请务必使用与 SharePoint 相关的标签,以便具有 SharePoint 经验的人也能看到它。
【解决方案2】:
 wordapp.documents.SaveAs2 "C:\temp\SLATemp1.docx"

应该是

 Thisdocument.saveas "C:\temp\SLATemp1.docx"

【讨论】:

  • ThisDocument 当然不会在从 Excel 运行的代码中工作!它仅适用于包含宏代码的文档,一般不应使用。应该改用ActiveDocument,但也不能在从 Excel 运行的代码中工作,除非wordapp. 在它前面。
  • 哎呀,你是对的 - 错过了它是从 Excel 运行的 - 我的错
猜你喜欢
  • 2014-03-21
  • 2023-03-30
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多