0

I've created a macro to move information from Excel into existing tables in a Word document. When I try to compile, I'm getting a "Compile error: variable not defined" error on wdCollapseStart within the statement Selection.Collapse Direction:=wdCollapseStart. I simplified to Selection.Collapse, but then I got the same error on the next line with wdRow. wdCollapse and wdRow are not variables, so I don't understand why I'm getting this error, especially when the exact same statement functions perfectly if I run it through the immediate window from Word. To clarify, I've written this macro for Excel, but the macro has to manipulate a Word document as well.

Full code below (the offending statement is in the first with statement):

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

Any help would be appreciated.

1 Answer 1

3

Excel VBA doesn't know about predefined Word constants such as wdCollapseStart. According to this wdCollapseStart is equal to 1. One solution is to just directly use the numbers that such constants refer to (using the linked-to documentation). A second solution is to add a reference to Microsoft Word 14.0 Object Library (under Tools/References in the VBA editor) after which your code should work without modification.

Sign up to request clarification or add additional context in comments.

4 Comments

@PortlandRunner You are correct. I edited the answer to reflect that. Thanks.
Turned out, at least partially, to be a missing End If. I did add the Microsoft Word 15.0 Object Library (there was no option for 14.0), but now I'm getting a similar error: "Run-time error '438': Object doesn't support this property or method" This time, it's on Selection.MoveDown Count:=4. I don't get it. Given that I've added Word as an object and that I'm using the with statements, this should work, even without the Word Library. But now that I've added the library, it should work and doesn't. Any more ideas?
I updated the original code so it has the missing End If.
@Vince Earl - "With Word_doc" does not mean that every object within the With block is a member of Word_doc. It's partly a shorthand that lets you say (for example) ".Content" rather than spelling out "Word_doc.Content". To get Word's Selection object you'll probably need to use "Word_app.Selection",which means to use "With" you would use "With Word_App" and then ".Selection"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.