VBA code giving error on line .Wrap = wdFindContinue

floggingmolly

Board Regular
Joined
Sep 14, 2019
Messages
167
Office Version
  1. 365
Platform
  1. Windows
I have a VBA code that fills in a word template from data in an excel sheet. The macro runs fine on 2 computers, but on the third computer it gives an error on the line .Wrap = wdFindContinue. The error says Compile error: Can't find project or library. The reference to Word is checked. Trying to troubleshoot and find a solution but not having any luck. If anyone cares to takes a look at the code and make any suggestions it would be appreciated.

Code:
Sub CreateWordDocuments()
Dim CustRow, CustCol, lastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet1
  
  If .Range("B3").Value = Empty Then
    MsgBox "Please select a correct template from the drop down list"
    .Range("D3").Select
    Exit Sub
  End If
    TemplRow = .Range("B3").Value 'Set Template Row
    TemplName = .Range("D3").Value 'Set Template Name
    FrDays = .Range("K3").Value 'Set From Days
    ToDays = .Range("M3").Value 'Set To Days
    DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
    
    'Open Word Template
    On Error Resume Next 'If Word is already running
    Set WordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
    'Launch a new instance of Word
    Err.Clear
    'On Error GoTo Error_Handler
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True 'Make the application visible to the user
    
    
    End If
    
    
    lastRow = .Range("E500").End(xlUp).Row  'Determine Last Row in Table
        For CustRow = 8 To lastRow
                DaysSince = .Range("S" & CustRow).Value
                If TemplName <> .Range("S" & CustRow).Value And DaysSince >= FrDays And DaysSince <= ToDays Then
                                Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
                                For CustCol = 1 To 19 'Move Through 9 Columns
                                    TagName = .Cells(7, CustCol).Value 'Tag Name
                                    TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
                                     With WordDoc.Content.Find
                                        .Text = TagName
                                        .Replacement.Text = TagValue
                                        .Wrap = wdFindContinue
                                        .Execute Replace:=wdReplaceAll 'Find & Replace all instances
                                     End With
                                Next CustCol
                        
                        If .Range("F3").Value = "PDF" Then
                                       FileName = ThisWorkbook.Path & "\EXTENSION LETTERS\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("I" & CustRow).Value & " - " & .Range("Y1").Value & " - " & .Range("X1").Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
                                       WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
                                                                                                                   
                                       FileName = ThisWorkbook.Path & "\EXTENSION LETTERS\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("I" & CustRow).Value & " - " & .Range("Y1").Value & " - " & .Range("X1").Value & ".docx"
                                       WordDoc.SaveAs FileName
                                       Application.Wait (Now + TimeValue("0:00:05"))
                                       
                                       WordDoc.Close False
                                       
                                   Else: 'If Word
                                       FileName = ThisWorkbook.Path & "\Word Files\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("E" & CustRow).Value & ".docx"
                                       WordDoc.SaveAs FileName
                                       Application.Wait (Now + TimeValue("0:00:05"))
                                     
                                   End If
                                   .Range("T" & CustRow).Value = TemplName 'Template Name
                                   .Range("U" & CustRow).Value = Now
                                    If .Range("H3").Value = "Email" Then
                                                  Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
                                                  Set OutMail = OutApp.CreateItem(0) 'Create Email
                                                  With OutMail
                                                     .SentOnBehalfOfName = "BDSNoReply@fedex.com"
                                                      .To = Sheet1.Range("R" & CustRow).Value
                                                      .Subject = "GOLD status recognition for " & Sheet1.Range("F" & CustRow).Value
                                                      .Body = "Upon reviewing the most recent Fiscal Quarter of Service Provider results in MEDALS,  " & Sheet1.Range("C" & CustRow).Value & vbCrLf & _
                                                      "has been identified as having attained Gold status for each month of the Quarter. This is a noteworthy" _
                                                      & vbCrLf & "achievement and we ask that you recognize this business in a public forum. Attached you will find" _
                                                      & vbCrLf & "a letter from Paul Melander, Senior Vice President of Safety & Transportation, congratulating " _
                                                      & vbCrLf & "and thanking " & Sheet1.Range("C" & CustRow).Value & "for this achievement. Please use the following guidelines:" _
                                                      & vbCrLf & vbCrLf & "   " & Chr(149) & "The business should be recognized in a meeting with their peers" _
                                                      & vbCrLf & "   " & Chr(149) & "The attached letter should be printed on high quality paper and presented to the Authorized Officer in this meeting" _
                                                      & vbCrLf & "   " & Chr(149) & "They should be thanked for their dedication to safety and service" _
                                                      & vbCrLf & vbCrLf & "We appreciate your efforts in this task. For further questions feel free to reach out to your local Business Development Solutions resources." _
                                                      & vbCrLf & vbCrLf & "Thank you," _
                                                      & vbCrLf & "Business Development Solutions - Agreement Management"
                                                      
                                                      .Attachments.Add FileName
                                                      .Display 'To send without Displaying change .Display to .Send
                                                      Application.Wait (Now + TimeValue("0:00:3"))
                                                  End With
                                    Else:
                                           
                                           
                                          WordDoc.Close False
                                          
                                    End If
                       
            End If '3 condition met
        Next CustRow
        WordApp.Quit
End With
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I have a VBA code that fills in a word template from data in an excel sheet. The macro runs fine on 2 computers, but on the third computer it gives an error on the line .Wrap = wdFindContinue. The error says Compile error: Can't find project or library. The reference to Word is checked. Trying to troubleshoot and find a solution but not having any luck. If anyone cares to takes a look at the code and make any suggestions it would be appreciated.

Code:
Sub CreateWordDocuments()
Dim CustRow, CustCol, lastRow, TemplRow, DaysSince, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet1
 
  If .Range("B3").Value = Empty Then
    MsgBox "Please select a correct template from the drop down list"
    .Range("D3").Select
    Exit Sub
  End If
    TemplRow = .Range("B3").Value 'Set Template Row
    TemplName = .Range("D3").Value 'Set Template Name
    FrDays = .Range("K3").Value 'Set From Days
    ToDays = .Range("M3").Value 'Set To Days
    DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
   
    'Open Word Template
    On Error Resume Next 'If Word is already running
    Set WordApp = GetObject("Word.Application")
    If Err.Number <> 0 Then
    'Launch a new instance of Word
    Err.Clear
    'On Error GoTo Error_Handler
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True 'Make the application visible to the user
   
   
    End If
   
   
    lastRow = .Range("E500").End(xlUp).Row  'Determine Last Row in Table
        For CustRow = 8 To lastRow
                DaysSince = .Range("S" & CustRow).Value
                If TemplName <> .Range("S" & CustRow).Value And DaysSince >= FrDays And DaysSince <= ToDays Then
                                Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
                                For CustCol = 1 To 19 'Move Through 9 Columns
                                    TagName = .Cells(7, CustCol).Value 'Tag Name
                                    TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
                                     With WordDoc.Content.Find
                                        .Text = TagName
                                        .Replacement.Text = TagValue
                                        .Wrap = wdFindContinue
                                        .Execute Replace:=wdReplaceAll 'Find & Replace all instances
                                     End With
                                Next CustCol
                       
                        If .Range("F3").Value = "PDF" Then
                                       FileName = ThisWorkbook.Path & "\EXTENSION LETTERS\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("I" & CustRow).Value & " - " & .Range("Y1").Value & " - " & .Range("X1").Value & ".pdf" 'Create full filename & Path with current workbook location, Last Name & First Name
                                       WordDoc.ExportAsFixedFormat OutputFileName:=FileName, ExportFormat:=wdExportFormatPDF
                                                                                                                  
                                       FileName = ThisWorkbook.Path & "\EXTENSION LETTERS\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("I" & CustRow).Value & " - " & .Range("Y1").Value & " - " & .Range("X1").Value & ".docx"
                                       WordDoc.SaveAs FileName
                                       Application.Wait (Now + TimeValue("0:00:05"))
                                      
                                       WordDoc.Close False
                                      
                                   Else: 'If Word
                                       FileName = ThisWorkbook.Path & "\Word Files\" & "\" & .Range("D" & CustRow).Value & " - " & .Range("E" & CustRow).Value & ".docx"
                                       WordDoc.SaveAs FileName
                                       Application.Wait (Now + TimeValue("0:00:05"))
                                    
                                   End If
                                   .Range("T" & CustRow).Value = TemplName 'Template Name
                                   .Range("U" & CustRow).Value = Now
                                    If .Range("H3").Value = "Email" Then
                                                  Set OutApp = CreateObject("Outlook.Application") 'Create Outlook Application
                                                  Set OutMail = OutApp.CreateItem(0) 'Create Email
                                                  With OutMail
                                                     .SentOnBehalfOfName = "BDSNoReply@fedex.com"
                                                      .To = Sheet1.Range("R" & CustRow).Value
                                                      .Subject = "GOLD status recognition for " & Sheet1.Range("F" & CustRow).Value
                                                      .Body = "Upon reviewing the most recent Fiscal Quarter of Service Provider results in MEDALS,  " & Sheet1.Range("C" & CustRow).Value & vbCrLf & _
                                                      "has been identified as having attained Gold status for each month of the Quarter. This is a noteworthy" _
                                                      & vbCrLf & "achievement and we ask that you recognize this business in a public forum. Attached you will find" _
                                                      & vbCrLf & "a letter from Paul Melander, Senior Vice President of Safety & Transportation, congratulating " _
                                                      & vbCrLf & "and thanking " & Sheet1.Range("C" & CustRow).Value & "for this achievement. Please use the following guidelines:" _
                                                      & vbCrLf & vbCrLf & "   " & Chr(149) & "The business should be recognized in a meeting with their peers" _
                                                      & vbCrLf & "   " & Chr(149) & "The attached letter should be printed on high quality paper and presented to the Authorized Officer in this meeting" _
                                                      & vbCrLf & "   " & Chr(149) & "They should be thanked for their dedication to safety and service" _
                                                      & vbCrLf & vbCrLf & "We appreciate your efforts in this task. For further questions feel free to reach out to your local Business Development Solutions resources." _
                                                      & vbCrLf & vbCrLf & "Thank you," _
                                                      & vbCrLf & "Business Development Solutions - Agreement Management"
                                                     
                                                      .Attachments.Add FileName
                                                      .Display 'To send without Displaying change .Display to .Send
                                                      Application.Wait (Now + TimeValue("0:00:3"))
                                                  End With
                                    Else:
                                          
                                          
                                          WordDoc.Close False
                                         
                                    End If
                      
            End If '3 condition met
        Next CustRow
        WordApp.Quit
End With
End Sub
This was resolved by removing the reference to Microsoft word, then rechoosing the reference.
 
Upvote 0
No need for a Word reference....
Code:
.Wrap = 1 'wdFindContinue
.Execute Replace:= 2 'wdReplaceAll
Use of XL's enumerated constant for wdfindContinue (ie. 1) allows you to use late binding (which you are using anyways) which doesn't require a reference to the Word object. However, you have dimmed the variable WordContent as Word.Range which does require a reference. I don't see where you're using the WordContent variable anyways? Speaking of declaring variables, when you
Code:
Dim WordDoc, WordApp, OutApp, OutMail As Object
only the Outmail variable is declared as an object... the others remain Variant. It should be...
Code:
WordDoc As Object, WordApp As Object, OutApp As Object , OutMail As Object
Also, objects should be set to nothing at the end of the code so they don't remain in memory. One other hopefully helpful suggestion, replace your Application.Wait with DoEvents.
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

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