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.
Bookmarks