我写过一些我非常满意的VBA代码。它通过工作表上的列表,切换到另一个并设置变量(从而更改了一些图形),然后打开单词,在图形中复制到各种书签,并将文档保存为变量名称。
它像一个魅力,我是一个快乐的男孩(为一个人节省了一个美好的一周和一些工作)。我没有触及过它 - 或者就此而言的工作表 - 今天打开了它,它在第一批上给了我一个类型的错配。我真的很喜欢一些建议,因为它让我摸不着头脑。
Public X As Integer
Public Y As String
Sub Macro2()
'Set up variables that are required
Y = ""
LoopCounter = 2
Do Until Y = "STOP"
'Grab the value from a list
Sheets("CPD data 13-14").Select
Range("A" & LoopCounter).Select
Y = Range("A" & LoopCounter).Value
'Change the chart values
Sheets("Pretty Display (2)").Select
Range("A1").Value = Y
'Open word template
Set wordapp = CreateObject("word.Application")
wordapp.documents.Open "LOCATION"
wordapp.Visible = True
wordapp.Activate
wordapp.ActiveDocument.Bookmarks("InstitutionName").Range = Y
wordapp.ActiveDocument.Bookmarks("Graph1").Range = ActiveSheet.ChartObjects("Chart 3")
'Close document
Mystring = Replace(Y, " ", "")
wordapp.ActiveDocument.SaveAs Filename:="LOCATION" & Mystring & ".docx"
wordapp.Quit
Set wordapp = Nothing
'Increase count and loop
LoopCounter = LoopCounter + 1
Loop
错误发生在以下行:
wordapp.ActiveDocument.Bookmarks("Graph1").Range = ActiveSheet.ChartObjects("Chart 3")
修改
正如所建议的那样,我已经更新了我的代码而不是使用select,所以它现在读取:
Set ws = Sheets("CPD data 13-14")
Set pd = Sheets("Pretty Display (2)")
'Set up variables that are required
Y = ""
LoopCounter = 2
Do Until Y = "STOP"
'Grab the value from a list
Y = ws.Range("A" & LoopCounter).Value
'Change the chart values
pd.Range("A1").Value = Y
'Open word template
Set wordapp = CreateObject("word.Application")
wordapp.documents.Open "LOCATION"
wordapp.Visible = True
wordapp.Activate
wordapp.ActiveDocument.Bookmarks("InstitutionName").Range = Y
wordapp.ActiveDocument.Bookmarks("Graph1").Range = pd.ChartObjects("Chart 3")
'Close document
Mystring = Replace(Y, " ", "")
wordapp.ActiveDocument.SaveAs Filename:="LOCATION" & Mystring & ".docx"
wordapp.Quit
Set wordapp = Nothing
'Increase count and loop
LoopCounter = LoopCounter + 1
Loop
我仍然在同一点获得相同的运行时错误。
答案 0 :(得分:1)
我的建议是设置Explicit标志并尝试反编译代码。任何未标注的变量都会引发错误。这是对变量进行尺寸标注并适当键入数据的好时机。
如果这样做不会引发错误,那么它应该是错误的,因为至少有一个变量LoopCounter
没有标注尺寸并因此可能导致数据类型错误,请尝试将Public X As Integer
更改为{{ 1}}以避免超出Integer数据类型限制的值。
Public X As Long
,有时也需要 .Activate
。选择一个工作表应该使它成为活动工作表,但情况并非总是如此。
答案 1 :(得分:0)
试试这个
Option Explicit
Public X As Integer
Public Y As String
Sub Macro2()
Dim wordApp As Object
Dim LoopCounter As Integer
Dim Mystring As String
Dim ws As Worksheet, pd As Worksheet
Set ws = Sheets("CPD data 13-14")
Set pd = Sheets("Pretty Display (2)")
'Set up variables that are required
Y = ""
LoopCounter = 2
' open one Word session for all the documents to be processed
Set wordApp = CreateObject("word.Application")
Do Until Y = "STOP"
'Grab the value from a list
Y = ws.Range("A" & LoopCounter).Value
With pd
.Range("A1").Value = Y 'Change the chart values
.ChartObjects("Chart 3").Copy ' Copy the chart
End With
'act on Word application
With wordApp
'open word template
.documents.Open "LOCATION"
.Visible = True
' paste into bookmarks, "save as" document and close it
With .ActiveDocument
.Bookmarks("InstitutionName").Range = Y
.Bookmarks("Graph1").Range.PasteSpecial
Mystring = Replace(Y, " ", "")
.SaveAs Filename:="LOCATION" & Mystring & ".docx"
.Close
End With
End With
'Increase count and loop
LoopCounter = LoopCounter + 1
Loop
'Close Word
wordApp.Quit
Set wordApp = Nothing
End Sub
我无法将“Range”对象直接设置为Excel“Chart”对象
所以我不得不复制图表并使用Word“Range”对象的“PasteSpecial”方法
此外,我只使用一个Word会话,这将导致更快的工作
最后,我还制作了一些“comsetics”,使代码更具可读性/可维护性
仅作为建议:我总是使用“Option Explicit”语句。这将迫使你做一些额外的工作来明确声明你使用的每一个变量,但这也可以更好地控制你的工作并导致更少的诽谤问题,从而节省时间