Excel to Word Macro

Sethomas5

Board Regular
Joined
Oct 5, 2015
Messages
204
I have a macro that opens a word template, puts data from an excel worksheet into different bookmarks in word, saves, closes and repeats with next worksheet. All of that works fine, just giving some background.
The problem I am having is with a piece of code inside that.
Code:
If Application.CountIf(Range("I1:Q1"), "Computer Literacy") = 0 Then
.Bookmarks("Computer_Class1").Range.Text = ('Core Category'!"I3") & "(" & ("CIS 212/INF 104") & ('Core Category'!"I10") & ("CIS 212/INF 104/TEC 161") & ")"
End If

I want this specific piece of code to look through the current worksheet, see that no headers say Computer Literacy and then do this code. The code is supposed to put the data from I3 on the worksheet called Core Category into a bookmark, and then the rest. Hopefully that is enough explanation for someone to get me where I need to go.

I know I don't have my syntax right, so please help.


Thank you.

Full macro below:

Code:
Sub SendToWord()
Dim objWord As Object
Dim objDoc As Object
Dim ws As Worksheet


    Set objWord = CreateObject("Word.Application")
    
    objWord.Visible = True

    For Each ws In ActiveWorkbook.Worksheets
        
        
        If ws.Name <> "equivalents" And ws.Name <> "Core Category" And ws.Name <> "Sheet4" Then ' dont open equivalents and Core Category

            ' create new document based on template
            Set objDoc = objWord.Documents.Add("C:\Users\thomassa\Desktop\thistemplate.dotm")      ' change as required

            With objDoc

                .Bookmarks("EKU_Major").Range.Text = ws.Name ' puts wksht name as the major
                
             If Application.CountIf(Range("I1:Q1"), "Computer Literacy") = 0 Then
.Bookmarks("Computer_Class1").Range.Text = Cells('Core Category'!"I3") & "(" & ("CIS 212/INF 104") & Cells('Core Category'!"I10") & ("CIS 212/INF 104/TEC 161") & ")"
End If
             For k = 9 To 17
                Select Case ws.Cells(1, k).Value
                Case "Written Communication"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Writing_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Writing_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                            
                Case "Oral Communication"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Oral_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Oral_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Oral_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                 
                Case "Natural Sciences"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Science_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Science_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Science_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Science_Class4").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                
                Case "Foreign Languages"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Foreign_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Foreign_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Foreign_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Foreign_Class4").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                
                Case "Heritage"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Heritage_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Heritage_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Heritage_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Heritage_Class4").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Heritage_Class5").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                
                Case "Humanities"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Humanities_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Humanities_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Humanities_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Humanities_Class4").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Humanities_Class5").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                
                Case "Social & Behavioral Sciences"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                             .Bookmarks("Social_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                 Next i
                         For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                         Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class4").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class5").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class6").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                             If Cells(i, k) <> "" Then
                             str1 = Cells(i, k)
                                .Bookmarks("Social_Class7").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                Next i
                
                Case "Quantitative Reasoning"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                            str1 = Cells(i, k)
                                .Bookmarks("Quantitative_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                    Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                            str1 = Cells(i, k)
                                .Bookmarks("Quantitative_Class2").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                    Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                            str1 = Cells(i, k)
                                .Bookmarks("Quantitative_Class3").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                    Next i
                    
                    Case "Computer Literacy"
                        For i = 2 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                            str1 = Cells(i, k)
                                .Bookmarks("Computer_Class1").Range.Text = Cells(i, k) & "(" & Cells(i, "G") & ")"
                        Exit For
                            End If
                    Next i
                        For i = i + 1 To Cells(Rows.Count, k).End(xlUp).Row
                            If Cells(i, k) <> "" Then
                            str1 = Cells(i, k)
                                .Bookmarks("Computer_Class1").Range.Text = Cells("'Core Category'!I3") & "(" & ("CIS 212/INF 104") & Cells("'Core Category'!I10") & ("CIS 212/INF 104/TEC 161") & ")"
                        Exit For
                            End If
                        
                    Next i
                    
                End Select
            Next k


                .SaveAs ThisWorkbook.Path & "\" & ws.Name & ".docx"

                .Close

            End With

            Set objDoc = Nothing
        End If

    Next ws

    objWord.Quit

    Set objWord = Nothing

End Sub
 
Also, even if I just put in words I want the Bookmark to have instead of the worksheet/cell reference, the macro stops and doesn't continue onto the other bookmarks.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top