我创建了一个宏来将信息从Excel移动到Word文档中的现有表中。当我尝试编译时,我在语句wdCollapseStart
中的Selection.Collapse Direction:=wdCollapseStart
上收到“编译错误:变量未定义”错误。我简化为Selection.Collapse
,但后来我在wdRow
的下一行遇到了同样的错误。 wdCollapse
和wdRow
不是变量,所以我不明白为什么我会收到此错误,尤其是当我通过Word的即时窗口运行完全相同的语句时,它完全正常运行。为了澄清,我已经为Excel编写了这个宏,但宏也必须操作Word文档。
下面的完整代码(违规声明在第一个with
声明中):
Option Explicit
Dim Word_App As Object
Dim Word_Doc As Object
Sub IDA_Creator()
Dim Starting_Sheet As Integer: Starting_Sheet = 4
Dim Ending_Sheet As Integer: Ending_Sheet = Worksheets.Count - 1
'Activates first category worksheet
Worksheets(Starting_Sheet).Activate
'Get the name of the destination Word file
Dim IDA_Word As String
IDA_Word = Application.GetOpenFilename(FileFilter:="Word Files (*.DOCX), *.DOCX", Title:="Select File To Be Opened")
'Open Word
Set Word_App = CreateObject("Word.Application")
Word_App.Visible = True
Set Word_Doc = Word_App.Documents.Open(IDA_Word)
Dim i As Integer
For i = 1 To 3
Call TrimTable
Call Move_To_Table(Starting_Sheet, i)
Dim j As Integer
For j = Starting_Sheet To Ending_Sheet
Call Copy_To_Word(i)
'Prepares Word table for next Category (unless it's the last category
If ActiveSheet.Index > Worksheets.Count - 1 Then
ActiveSheet.Next.Select
With Word_Doc
Selection.InsertRowsBelow (2)
Selection.Collapse Direction:=wdCollapseStart
Selection.EndOf Unit:=wdRow, Extend:=wdExtend
Selection.Cells.Merge
Selection.Collapse Direction:=wdCollapseStart
End With
End If
Next j
Worksheets(Starting_Sheet).Activate
Next i
'Save Word Document and Close Word
With Word_Doc
.Save
End With
Word_App.Quit
Set Word_Doc = Nothing
Set Word_App = Nothing
End Sub
'Trims the table to the first category and criterion
Function TrimTable()
With Word_Doc
With Word_App.Selection.Find
.Text = "Table " & i
.Style = "Caption"
.Execute
End With
Selection.MoveDown Count:=4
Selection.EndOf Unit:=wdTable, Extend:=wdExtend
Selection.Rows.Delete
End With
End Function
'Moves the cursor to the first cell of the next table, formats table with correct style
Function Move_To_Table(Starting_Sheet As Integer, i As Integer)
Sheets(Starting_Sheet).Activate
With Word_Doc
With Word_App.Selection.Find
.Text = "Table " & i
.Style = "Caption"
.Execute
End With
'Ensures first row of table is in "CellHeadingL" style
Selection.MoveDown
Selection.EndOf Unit:=wdRow, Extend:=wdExtend
Selection.Style = "CellHeadingL"
Selection.Collapse Direction:=wdCollapseStart
'Clears boilerplate text
Selection.MoveDown
Selection.EndOf Unit:=wdTable, Extend:=wdExtend
Selection.Delete
Selection.Collapse Direction:=wdCollapseStart
End With
End Function
'Copies criteria text from Excel to Word
Function Copy_To_Word(i As Integer)
'Copies category name from Excel
Selection.Copy
'Pastes category name into Word
With Word_Doc
Selection.EndOf Unit:=wdRow, Extend:=wdExtend
Selection.PasteSpecial DataType:=wdPasteText
Selection.Style = "CellBodyL"
Selection.Font.Bold = True
Selection.MoveDown
End With
'Determines number of subcategories and criteria
Range(ActiveCell, ActiveCell.End(xlDown)).Select
Dim Number_Criteria As Integer: Number_Criteria = Selection.Rows.Count - 2
'Adds correct number of rows to Word table, positions cursor at top row
With Word_Doc
Selection.InsertRowsBelow (Number_Criteria - 1)
Selection.Collapse Direction:=wdCollapseStart
Selection.MoveUp
End With
'Selects the first subcategory or criterion from Excel and copies to clipboard
Selection(1).Select
Selection.Offset(2, 0).Select
'Begins copying to Word
For i = 1 To Number_Criteria
Selection.Copy
'Checks whether there's a subcategory (merged) cell in Excel, formats Word table to match and pastes subcategory into Word
If ActiveCell.MergeCells = True Then
With Word_Doc
Selection.EndOf Unit:=wdRow, Extend:=wdExtend
Selection.Cells.Merge
Selection.PasteSpecial DataType:=wdPasteText
Selection.Style = "CellBodyL"
Selection.Font.Bold = True
End With
'Excel cell isn't a subcategory (merged) cell in Excel, paste criterion into Word
Else
With Word_Doc
Selection.PasteSpecial DataType:=wdPasteText
Selection.Style = "CellBodyL"
End With
End If
'Moves to next cell
Selection.Offset(1, 0).Select
Next i
'Puts cursor in position for next pass (Preferred or Optional criteria)
Selection.Offset(5, 0).Select
End Function
任何帮助都将不胜感激。
答案 0 :(得分:3)
Excel VBA不了解预定义的Word常量,例如wdCollapseStart
。根据{{3}} wdCollapseStart
等于1
。一种解决方案是直接使用此类常量引用的数字(使用链接到文档)。第二种解决方案是添加对Microsoft Word 14.0 Object Library
的引用(在VBA编辑器中的Tools/References
下),之后您的代码应该无需修改即可。