VBA error: 'Compile Error. Cannot find Project or library' in 2003

srikanth_s

New Member
Joined
Sep 1, 2009
Messages
9
Hi All,

I'm very new to writing code and do a lot of copy/paste from the recorder. With that out of the way, I've written what seems like a fairly simple routine for creating a word doc out of content in an excel file. Both versions 2007. When my boss runs this in Excel/Word 2003 he gets the error cited in the subject line above 'Compile Error. Cannot find Project or Library.' I assume this is because I wrote it in 2007, and I assume I need to write it in a backwards-compatible way. My problem is I just don't know how to get started with figuring out which methods/arguments that I'm using are unavailable in 2003. My code is below; I'd love some guidance not only on what is awry with this code in particular, but more generally how to approach writing backwards-compatible code.

Code:
Private Sub cmdSendToWord_Click()
    Dim i
        Dim msWord As New Word.Application
        msWord.Visible = True
        msWord.Documents.Add
    With msWord
        .Selection.TypeParagraph
        .Selection.InsertDateTime DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
            DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _
            InsertAsFullWidth:=False
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.TypeText Text:="Dear "
        .Selection.FormFields.Add Range:=.Selection.Range, Type:=wdFieldFormTextInput
        .Selection.TypeText Text:=":"
        .Selection.TypeParagraph
        .Selection.Font.Size = 1
        .Selection.TypeParagraph
        .Selection.Font.Size = 11
        .ActiveDocument.FormFields("Text1").Result = "RECIPIENT"
        .Selection.FormFields.Add Range:=.Selection.Range, Type:=wdFieldFormTextInput
        .ActiveDocument.FormFields("Text2").Result = "INTRODUCTORY TEXT"
    End With
    i = 4
    Do
        If Range("B" & i).Text = "" Then
            Exit Do
        End If
        
        If Range("e" & i).Text = True Then
            Range("B" & i, "D" & i).Copy
'            msWord.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
            msWord.Selection.PasteExcelTable False, True, False
'            msWord.Selection.TypeParagraph
            msWord.ActiveDocument.PrintPreview
        End If
        i = i + 1
    Loop
    msWord.Selection.TypeParagraph
    msWord.Selection.FormFields.Add Range:=msWord.Selection.Range, Type:=wdFieldFormTextInput
    msWord.ActiveDocument.FormFields("Text3").Result = "CONCLUDING TEXT"
    Application.CutCopyMode = False
    ActiveSheet.Shapes("Picture 125").Copy
    msWord.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    msWord.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    msWord.ActiveWindow.Selection.Paste
    msWord.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    msWord.ActiveDocument.FormFields.Shaded = Not msWord.ActiveDocument.FormFields.Shaded
    msWord.ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

Thanks in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You need to make sure both of your references are the same. Sounds like you two are out of sync with your references more than a 2003/2007 issue.

In VBE if you go to Tools | References, see what you have checked. Confirm your boss has the same ones checked.

In fact, as I type this, it's probably both. His references for Word will be MS word 11.0. Yours are MS word 12.0. He'll need to change his references.

HTH,
Roger
 
Upvote 0
Thanks for your response rconverse.

This raises a larger concern -- I'm looking to deploy this file as a tool to a team of about 15 people of varying technical abilities / comfort levels. Is there a way to avoid having to force pretty excel-illiterate people to check their references by writing the code with them in mind?

Also, on making sure both sets of references are the same -- as you said, if I write it in 2007 I'll have the office 12.0 library. How would he have access to that in 2003?

Thanks again ...
 
Upvote 0
You can use late binding instead, which means you don't need to set any references. Declare all your word objects simply As Object and use CreateObject to initialise the word application. You will however need to either use literal values for any word constants (such as wdFieldFormTextInput) or declare them yourself at the top of your code.
 
Upvote 0
Hmm ... that sounds promising.

Is anyone aware of a good tutorial/article on late- vs. early-binding? As I said I'm very, very new to this.

Also, I recall attempting to use literal values for constants in this project at one time -- specifically with wdFieldFormTextInput. I think quick info cited the literal value as "70 (&H46)." How do I read that / which portion would I use?

Thanks so much again for your help.
 
Last edited:
Upvote 0
Thanks rorya. I'm converting to late-bound now, and am getting the error, "run-time error 4605: method or property is not available because the clipboard is empty or not available."

The concept of this document is to create a word doc that includes some form fields for user input as well selected rows from a table in the excel sheet.

The debugger is pooping out at the line
Code:
msWord.Selection.PasteExcelTable False, True, False
.

Does this have something to do with error handling / managing multiple instances of word?

My attempt at conversion to late-bound:

Code:
Private Sub cmdSendToWord_Click()
    Dim i
        Dim msWord As Object
        Set msWord = CreateObject("Word.Application")
        msWord.Visible = True
        msWord.Documents.Add
    With msWord
        .Selection.TypeParagraph
        .Selection.InsertDateTime DateTimeFormat:="M/d/yyyy", InsertAsField:=False, _
            DateLanguage:=1033, CalendarType:=0, _
            InsertAsFullWidth:=False
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.TypeText Text:="Dear "
        .Selection.FormFields.Add Range:=.Selection.Range, Type:=70
        .Selection.TypeText Text:=":"
        .Selection.TypeParagraph
        .Selection.Font.Size = 1
        .Selection.TypeParagraph
        .Selection.Font.Size = 11
        .ActiveDocument.FormFields("Text1").Result = "RECIPIENT"
        .Selection.FormFields.Add Range:=.Selection.Range, Type:=70
        .ActiveDocument.FormFields("Text2").Result = "INTRODUCTORY TEXT"
    End With
    i = 4
    Do
        If Range("B" & i).Text = "" Then
            Exit Do
        End If
        
        If Range("e" & i).Text = True Then
            Range("B" & i, "D" & i).Copy
'            msWord.Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=wdInLine, DisplayAsIcon:=False
             msWord.Selection.PasteExcelTable False, True, False
'            msWord.Selection.TypeParagraph
            msWord.ActiveDocument.PrintPreview
        End If
        i = i + 1
    Loop
    msWord.Selection.TypeParagraph
    msWord.Selection.FormFields.Add Range:=msWord.Selection.Range, Type:=70
    msWord.ActiveDocument.FormFields("Text3").Result = "CONCLUDING TEXT"
    Application.CutCopyMode = False
    ActiveSheet.Shapes("Picture 125").Copy
    msWord.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = True
    msWord.ActiveWindow.ActivePane.View.SeekView = 9
    msWord.ActiveWindow.Selection.Paste
    msWord.ActiveWindow.ActivePane.View.SeekView = 0
    msWord.ActiveDocument.FormFields.Shaded = Not msWord.ActiveDocument.FormFields.Shaded
    msWord.ActiveDocument.Protect Type:=2, NoReset:=True
End Sub

Thanks again, in advance.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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