VBAtypes错配

我写了一些我很满意的VBA代码。 它通过一个工作表上的列表,切换到另一个,并设置一个variables(从而改变了一些图),然后打开单词,在graphics中复制到各种书签,并将文档保存为variables名称。

它像一个魅力工作,我是一个快乐的男孩(节省了一个好的一周,为某人一些工作)。 我还没有触及它,或者就此而言的工作表 – 今天打开它,它是第一批给我一个types错配。 我真的很喜欢一些build议,因为它让我挠了脑袋。

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") 

编辑

build议我已经更新我的代码不使用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 

我仍然在同一时间得到相同的运行时错误。

尝试这个

 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 

我不能有一个单词“范围”对象直接设置为Excel“图表”对象

所以我不得不复制图表,并使用Word“Range”对象的“PasteSpecial”方法

此外,我只用一个Word会话,这导致了一个更快的工作

最后,我还做了一些“comsetics”,使代码更可读/可保存

就像一个build议:我总是使用“Option Explicit”语句。 这会迫使你做一些额外的工作来显式声明你所使用的每一个variables,但是这样做也会对你的工作有更多的控制,并且导致更less的debbugging问题,从而最终节省时间

我的build议是设置Explicit标志并尝试反编译代码。 任何你没有维度的variables都会抛出一个错误。 这是确定variables的尺寸和适当input数据的好时机。

如果do not't抛出一个错误,它应该,因为你至less有一个variablesLoopCounter没有LoopCounter ,因此可能会导致数据types错误,然后尝试更改Public X As Integer Public X As Long以避免超出整型数据types的限制。

.Activate有时甚至在使用时也是必需的。从我的经验中select。 select一个工作表应该使其成为活动工作表,但情况并非总是如此。