我是Excel中的VBA代码新手。我编写了一些代码,用于打开Word文档,按顺序搜索值,并将值替换为从Excel工作簿中提取的值。可能不是最快的方式,但它的工作原理。我需要" SaveAs"一个不同的文件名,最终需要使用相同的子目录上传到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:此对象不支持此属性或方法&#34;当它命中时:
wordapp.documents.SaveAs2 "C:\temp\SLATemp1.docx"
有人可以建议为什么会这样吗?
谢谢, 史蒂芬
答案 0 :(得分:0)
这是因为handle()
集合没有方法Documents
。您需要处理特定的SaveAs
对象。
声明并设置Word文档的变量:
Document
稍后,您可以使用此对象专门保存此文档:
Dim wordDoc as Word.Document
Set wordDoc = wordapp.Documents.Open("file path here")
在Sub结束时,您应该明确释放Word对象:
wordDoc.SaveAs2 "file path here"
并且,如果您不再使用Set wordDoc = Nothing
Set wordapp = Nothing
:
wordapp
如果您不wordapp.Quit
,这一点尤为重要,因为否则Word应用程序将在内存中运行 - 每次运行宏时都会运行一次 - 并且它们不会结束,直到Windows关闭(或有人使用任务管理器)。
答案 1 :(得分:-1)
wordapp.documents.SaveAs2 "C:\temp\SLATemp1.docx"
应该是
Thisdocument.saveas "C:\temp\SLATemp1.docx"