【发布时间】: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"