Consulting

Results 1 to 2 of 2

Thread: Object not defined while trying to clear contents of row

  1. #1

    Object not defined while trying to clear contents of row

    I have a script that imports form fields and content controls into an excel spreadsheet. I need to alter my script to clear all the contents of row 2 before importing the data. When I try to use the correct statement (highlighted in red) I believe I require to do this, I get the error "1004:Application-defined or object defined error". I am assuming I need to use a DIM statement to fix this but I can't seem to get my head around it. Any help is appreciated. Thanks

    Sub GetFormData()     'Note: this code requires a reference to the Word object model
        Application.ScreenUpdating = False
        Dim wdApp As New Word.Application
        Dim wdDoc As Word.Document
        Dim CCtrl As Word.ContentControl
        Dim FmFld As Word.FormField
        Dim strFolder As String, strFile As String
        Dim WkSht As Worksheet, i As Long, j As Long
        strFolder = GetFolder
        If strFolder = "" Then Exit Sub
        Set WkSht = ActiveSheet
        WkSht.Rows("2:" & Rows.Count).ClearContents
        i = 1
        strFile = Dir(strFolder & "\*.doc", vbNormal)
        While strFile <> ""
            i = i + 1
            Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
            With wdDoc
                j = 0
                For Each CCtrl In .ContentControls
                    j = j + 1
                    WkSht.Cells(i, j) = CCtrl.Range.Text
                Next
                For Each FmFld In .FormFields
                    j = j + 1
                    WkSht.Cells(i, j) = FmFld.Result
                Next
            End With
            wdDoc.Close SaveChanges:=False
            strFile = Dir()
        Wend
        wdApp.Quit
        Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
        Application.ScreenUpdating = True
    End Sub
     
    Function GetFolder() As String
        Dim oFolder As Object
        GetFolder = ""
        Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
        If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
        Set oFolder = Nothing
    End Function
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    
    End Sub

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello TiffanyW,

    Your code appears to be in order. Is the worksheet protected?
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •