英文:
Inherited a VBA macro, edited it, now getting 5941 error that selected objects don't exist...but they do
问题
我继承了一个宏,该宏将Excel表格和图表复制并粘贴到具有特定书签的Word模板中。
我按照原始编写的方式运行了这个宏,它正常运行。
我编辑了宏,以在Excel中包括另一个工作表(Sheet4_new),然后通过添加具有Sheet4_new书签的页面来编辑Word模板。
现在当我运行宏时,它会粘贴一些表格和图表(包括我添加的工作表中的表格和图表),但说其他图表不存在......但它们仍然存在于宏和Word模板中。调试器突出显示以下代码行:myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Select
我想知道为什么会出现这个错误。只有一些图表和表格没有粘贴到Word模板中。
以下是继承的宏的部分代码:
'Array for the various tabs of interest
TabArray = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4_new", "Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9")
'Arrays to switch between tables and charts of interest
TableArray = Array("J15:L24", "A4:I14", "I16:K23", "B4:H15")
ChartArray = Array("Chart 1", "Chart 2")
'List of Word Document Bookmarks (To Paste To)
TableBookmarkArray = Array("Sheet1", "Sheet1Table", "Sheet2", "Sheet2Table", "Sheet3", "Sheet3Table", "Sheet4_new", "Sheet4_newTable", "Sheet5", "Sheet5Table", "Sheet6", "Sheet6Table", "Sheet7", "Sheet7Table", "BlankTable", "Sheet8", "Sheet9", "Sheet9Table")
ChartBookmarkArray = Array("Sheet1Chart", "Sheet1Chart2", "Sheet2Chart", "Sheet2Chart2", "Sheet3Chart", "Sheet3Chart2", "Sheet4_newChart", "Sheet4_newChart2", "Sheet5Chart", "Sheet5Chart2", "BlankChart", "BlankChart", "BlankChart", "BlankChart", "Sheet8Chart", "Sheet8Chart2", "Sheet9Chart", "Sheet9Chart2")
'Variable for cycling through all the tables and charts in both arrays at the same time
BookmarkCounter = 1
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Set Variable Equal To Destination Word Document
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
WordApp.Activate
WordFilePath = "locationoncomputer"
Set myDoc = WordApp.Documents.Open(WordFilePath & "nameofdoc.docx")
'Loop Through and Copy/Paste Multiple Excel Tables and Charts
For x = LBound(TabArray) To UBound(TabArray)
'...(这里是一些宏的主要代码,包括复制和粘贴表格和图表)
Next x
尝试删除并重新添加书签,但仍然遇到相同的错误。
英文:
I inherited a macro that copies and pastes Excel tables and charts into a Word template with specific bookmarks.
I ran the macro as it was written originally, and it ran fine.
I edited the macro to include another sheet (Sheet4_new) in Excel and then edited the Word template by adding a page with Sheet4_new bookmarks.
Now when I run the Macro it pastes some of the tables and charts (including the ones from the sheet I added) but says that other charts don't exist...but they do and are still in the macro and Word template. The debugger highlights this line of code: myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Select
I'm looking for some help as to why I'm getting this error. It's only select charts and tables that are not pasting into the Word template.
Here is the inherited macro:
'Array for the various tabs of interest
TabArray = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4_new", "Sheet5", "Sheet6", "Sheet7", "Sheet8", "Sheet9")`
'Arrays to switch between tables and charts of interest
TableArray = Array("J15:L24", "A4:I14", "I16:K23", "B4:H15")
ChartArray = Array("Chart 1", "Chart 2")
'List of Word Document Bookmarks (To Paste To)
TableBookmarkArray = Array("Sheet1", "Sheet1Table", "Sheet2", "Sheet2Table", "Sheet3", "Sheet3Table", "Sheet4_new", "Sheet4_newTable", "Sheet5", "Sheet5Table", "Sheet6", "Sheet6Table", "Sheet7", "Sheet7Table", "BlankTable", "Sheet8", "Sheet9", "Sheet9Table")
ChartBookmarkArray = Array("Sheet1Chart", "Sheet1Chart2", "Sheet2Chart", "Sheet2Chart2", "Sheet3Chart", "Sheet3Chart2", "Sheet4_newChart", "Sheet4_newChart2", "Sheet5Chart", "Sheet5Chart2", "BlankChart", "BlankChart", "BlankChart", "BlankChart", "Sheet8Chart", "Sheet8Chart2", "Sheet9Chart", "Sheet9Chart2")
'Variable for cycling through all the tables and charts in both arrays at the same time
BookmarkCounter = 1
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
'Set Variable Equal To Destination Word Document
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
WordApp.Activate
WordFilePath = "locationoncomputer"
Set myDoc = WordApp.Documents.Open(WordFilePath & "nameofdoc.docx")
'Loop Through and Copy/Paste Multiple Excel Tables and Charts
For x = LBound(TabArray) To UBound(TabArray)`
ActiveWorkbook.Worksheets(TabArray(x)).Activate
If x = 2 Then
RangeSwitcher = 1
IsThereSomething = ThisWorkbook.Worksheets(TabArray(x)).Range("K23").Value
'ElseIf x = 4 Or x = 5 Then
'RangeSwitcher = 5
'IsThereSomething = ThisWorkbook.Worksheets(TabArray(x)).Range("J19").Value
Else
RangeSwitcher = 3
IsThereSomething = ThisWorkbook.Worksheets(TabArray(x)).Range("J20").Value
End If
'Switch between the two charts and tables in any tab
For y = 1 To 2
If x <> 7 Then
'Copy Table Range from Excel
Set tbl = ThisWorkbook.Worksheets(TabArray(x)).Range(TableArray(RangeSwitcher))
tbl.CopyPicture Appearance:=xlScreen, Format:=xlPicture
'Paste Table into MS Word (using inserted Bookmarks -> ctrl+shift+F5)
myDoc.Bookmarks(TableBookmarkArray(BookmarkCounter)).Range.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
ElseIf y = 2 Then
Set tbl = ThisWorkbook.Worksheets(TabArray(x)).Range(TableArray(RangeSwitcher))
tbl.CopyPicture Appearance:=xlScreen, Format:=xlPicture
myDoc.Bookmarks(TableBookmarkArray(BookmarkCounter)).Range.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, _
Placement:=wdInLine, DisplayAsIcon:=False
End If
For Each iShape In WordApp.ActiveDocument.InlineShapes
If iShape.AlternativeText = "" Then
Set pShape = iShape
pShape.AlternativeText = "table"
Exit For
End If
Next
If x <> 5 And x <> 6 Then
'Check if there are no counts & costs in the current table selection
If IsThereSomething = 0 Then
'If there's no counts or costs for this county, then we put in some replacement text instead of a chart
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Select
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Text = NothingReplacementTextArray(BookmarkCounter)
Else
'Otherwise, copy the corresponding chart
With ActiveSheet.ChartObjects(ChartArray(y))
.Activate
.Select
End With
ActiveChart.ChartArea.Copy
'Used this at first but it's not that nice looking for charts when they're pasted in
'myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
'Placement:=wdInLine, DisplayAsIcon:=False
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Select
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Text = ""
WordApp.ActiveDocument.Application.Selection.PasteSpecial Link:=False, DataType:=14, _
Placement:=wdInLine, DisplayAsIcon:=False
For Each iShape In WordApp.ActiveDocument.InlineShapes
If iShape.AlternativeText = "" Then
Set pShape = iShape
pShape.ScaleHeight = 65
pShape.ScaleWidth = 65
pShape.AlternativeText = "chart"
Exit For
End If
Next
End If
End If
ThisWorkbook.Worksheets(TabArray(x)).Range("C2").Select
'Move to the next bookmark in the list
BookmarkCounter = BookmarkCounter + 1
'Switch table range to second table on tab
RangeSwitcher = RangeSwitcher + 1
Next y
Next x
Tried deleting and re-adding bookmarks, get the same error.
答案1
得分: 2
这是问题:
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Text = ""
设置书签范围的文本将删除书签。
举例说明:
Dim bm As Bookmark
Set bm = ActiveDocument.Bookmarks("Test")
bm.Range.Text = ""
Set bm = ActiveDocument.Bookmarks("Test") '错误:集合中的请求成员不存在
这是一个解决方案:
Sub Tester()
Dim bm As Bookmark
Set bm = ActiveDocument.Bookmarks("Test")
'bm.Range.Text = "" '这会删除书签
SetBookmarkText bm, "" '...这不会删除(实际上)
End Sub
'设置书签的文本;在删除后重新创建它
Sub SetBookmarkText(bm As Bookmark, txt As String)
Dim nm As String, rng As Range
nm = bm.Name
Set rng = bm.Range
With rng
.Text = txt
.Bookmarks.Add Name:=nm '重新添加书签
End With
End Sub
英文:
This is the issue:
myDoc.Bookmarks(ChartBookmarkArray(BookmarkCounter)).Range.Text = ""
Setting a bookmarked range's Text will delete the bookmark.
To illustrate:
Dim bm As Bookmark
Set bm = ActiveDocument.Bookmarks("Test")
bm.Range.Text = ""
Set bm = ActiveDocument.Bookmarks("Test") 'Error: The requested member of the collection does not exist
Here's a solution:
Sub Tester()
Dim bm As Bookmark
Set bm = ActiveDocument.Bookmarks("Test")
'bm.Range.Text = "" 'this will delete the bookmark
SetBookmarkText bm, "" '...this will not (effectively)
End Sub
'Set a bookmark's Text; recreate it after it's deleted
Sub SetBookmarkText(bm As Bookmark, txt As String)
Dim nm As String, rng As Range
nm = bm.Name
Set rng = bm.Range
With rng
.Text = txt
.Bookmarks.Add Name:=nm 're-add the bookmark
End With
End Sub
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论