+ Reply to Thread
Results 1 to 5 of 5

Run-time error :5174 "Application-defined or object -defined error"

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    76

    Post Run-time error :5174 "Application-defined or object -defined error"

    Hi guys,
    I need help in solving this error. My goal is to populate a word document template by values from the cells marked by their respective bookmarks from the excel sheet called "DataSheet" to template called "Tier2 Dashboards". I have written the vba code that does the following but gets "Run-time error:5174" gets to Set Wrd = CreateObject("Word.Application"). Below is the vba code that gives the error:

    Sub main()
    Dim lRow As Long, lRowEnd As Long
    ' Step 1. This section of the code declares your variables - that is, telling the program that
    ' you're going to be using the following variables.
        
        Dim myRow As Integer
        Dim Wrd As New Word.Application
        
        Set Wrd = CreateObject("Word.Application")
        Dim MergeDoc As String
        Dim FName As String
        'Dim rChart As String
        'Dim mChart As String
        Dim wdRng As Word.Range
        Dim FolderName As String
        'the following code is to move the rows
        'for the respective company to the columns alongside.
        Application.ScreenUpdating = False
        With Sheet1
        lRowEnd = .UsedRange.Rows.Count
        For lRow = lRowEnd To 2 Step -1
            If .Cells(lRow, 1).Value = "" Then
                .Range(.Cells(lRow, 1).End(xlToRight), Cells(lRow, Columns.Count).End(xlToLeft)).Copy
                .Cells(lRow - 1, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial (xlPasteValues)
                .Rows(lRow).EntireRow.Delete
            End If
        Next lRow
    End With
    Application.ScreenUpdating = True
        ' Reads where the excel file is located, uses that directory as the working directory
        MergeDoc = Application.ActiveWorkbook.Path
        
     
        ' Reads in the name of the template
        MergeDoc = MergeDoc + "\" + Worksheets("CodeInfo").Cells(2, 2).Value
    
        'Reads in the name of the folder where will save completed dashboards
        FolderName = Worksheets("CodeInfo").Cells(3, 2).Value
        
        'start looping through your schools, districts, etc.
        myRow = 5  'Assumes the first school is in row 4
        FName = Worksheets("DataSheet").Cells(myRow, 1).Value 'Reads the data worksheet 3b
        Do While FName <> ""
            Wrd.Documents.Add MergeDoc 'opens a document using the template
            Wrd.Visible = True  'makes the word file visible
        'starts reading the bookmarks
            bmkrow = 2  ' the bookmarks start in row 3
            'read in the name of the bookmark, exactly as it is in word
            bmkname = Worksheets("Setup").Cells(bmkrow, 2).Value
            
            Do While Not IsEmpty(bmkname) 'while there are bookmarks left
                bmkCol = Worksheets("Setup").Cells(bmkrow, 1).Value  'read in the value of the bookmark
                
                'paste the value into word document
                With Wrd.ActiveDocument.Bookmarks
                    .Item(bmkname).Range.Text = Worksheets("DataSheet").Cells(myRow, bmkCol).Value
                End With
                
                'move to the next row
                bmkrow = bmkrow + 1
                bmkname = Worksheets("Setup").Cells(bmkrow, 2).Value  'read in the new bookmark name
                
            Loop
            'save the word document with all information pasted in
            'use the value stored in FName as the name of the new file
            Wrd.ActiveDocument.SaveAs (Application.ActiveWorkbook.Path + "\" + FolderName + "\" + FName + ".doc")
            Wrd.ActiveDocument.Close 'close the word document
        
            'move to the next document (row in Dashboard Data)
            myRow = myRow + 1
            'read in the new file name
            FName = Worksheets("DataSheet").Cells(myRow, 1).Value
        
        Loop
        End Sub
    I would like to have some help on this issue.
    Last edited by mvneema; 03-06-2014 at 11:20 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Run-time error :5174 "Application-defined or object -defined error"

    I don't think you need that line of code.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-24-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Run-time error :5174 "Application-defined or object -defined error"

    But I need to set the variable Wrd. right?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Run-time error :5174 "Application-defined or object -defined error"

    Try getting rid of New and using this.
    Dim wrd As Word.Application
    
        Set wrd = CreateObject("Word.Application")

  5. #5
    Registered User
    Join Date
    02-24-2014
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    76

    Re: Run-time error :5174 "Application-defined or object -defined error"

    I tried doing that but I get the same error. No Idea where I need to change the code to make it work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Error " Run-time error '1004': application defined or object defined error
    By lengwer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-11-2013, 07:26 AM
  2. Error When Implementing Code: "1004 - Application-defined or object-defined error"
    By blueblazingdemon in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-10-2013, 11:21 AM
  3. Replies: 1
    Last Post: 05-06-2013, 06:07 AM
  4. How to catch "<Application-defined or object-defined error>" in Excel 2010 VBA?
    By cjweitz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-19-2013, 11:31 PM
  5. Run-time error "1004" Application defined or object-define-error
    By josros60 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-21-2013, 01:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1