Consulting

Results 1 to 6 of 6

Thread: Macro Compile Errors - Word 2007

  1. #1

    Macro Compile Errors - Word 2007

    Hi guys,

    I just made the change to word 2007 and noticed that my macros do not work the same as they did in previous versions. I took care of some of my simpler problems, but I have a macro who's logic is all thrown off by the inability to declare variables as they should be.

    The following lines all give me
    -----------------------------------------------------
    Compile Error: Can't find project or library
    -----------------------------------------------------
    Dim fso As FileSystemObject
    Dim fol As Folder
    Dim pic As File
    Dim pth As New MSComDlg.CommonDialog
    pname = .SelectedItems(1)
    Set fso = New FileSystemObject
    Set fol = fso.GetFolder(pname) <--pname is the issue here
    ---------------------------------------------------------
    Spots in the macro are then thrown off by the variables declared and used above.

    Is there a library I have to reference in 2007 to make it work with pre-2007 macros?
    Thanks,

    Abdullah

  2. #2
    Well you haven't declared pname so if you have Option Explicit on (I believe it's now on by default) that will cause an error. Also the call to the property .SelectedItems isn't in a With block so the compiler won't know which object's selected items you're trying to look up. I take it you've added references to Microsoft Scripting Runtime and (I think) the Common Dialog ActiveX Control (COMDLG32.OCX)?

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    pname = .SelectedItems(1)

    as it stands, would fail in ANY version.

    Nelviticus has it bang on, on all counts.

    1. pname should be declared
    2. .SelectedItems(1) needs to be in a With block
    3. Do you have all the proper References?

    BTW: you may want to be more explicit.
    [vba]

    Dim fso As Scripting.FileSystemObject
    Dim fol As Scripting.Folder
    [/vba]

  4. #4
    Guys,

    Sorry for the confusion, for the sake of space I copied and pasted portions of the code. .SelectedItems(1) is in a With block. The full code is at the bottom of this post,

    As for pname, I had it as a path that was selected by the user through a prompt. Not 100% what to declare it as.

    The below macro works in word versions previous to 2008...it references

    Visual Basic for Applications
    Microsoft Word 12.0 Object Library
    OLE Automation
    Microsoft Office 12.0 Object Library
    Microsoft Forms 2.0 Object Library
    MISSING: Microsoft Common Dialog Control 6.0 (SP3)
    Windows Script Host Object Model

    it worked fine (w/pname undeclared and all) in previous versions. Is the Missing Common Dialog Control an issue?

    Will upgrading to SP3 fix it?

    Also, declaring
    Dim fso As Scripting.FileSystemObject
    Still caused unknown library issues.


    Sub LIF()
    '
    ' LIF Macro
    'Variables
    Dim fso As FileSystemObject
    Dim fol As Folder
    Dim pic As File
    Dim tbl As Table
    Dim roe As Row
    Dim cel As Cell
    Dim ish As InlineShape
    Dim pth As New MSComDlg.CommonDialog
    Dim r As Integer
    Dim t As Integer

    entry = 0
    'Browse to folder
    With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = -1 Then
    pname = .SelectedItems(1)
    Else
    MsgBox "You pressed Cancel"
    End If
    End With
    'file path for pictures
    Set fso = New FileSystemObject
    Set fol = fso.GetFolder(pname)
    'set row 1 as header for each page
    Set tbl = ActiveDocument.Range.Tables(1)
    ActiveDocument.Tables(1).Rows(1).HeadingFormat = True
    'FILLING IN TABLE
    For Each pic In fol.Files
    If LCase(Right(pic.Path, 4)) = ".jpg" Or LCase(Right(pic.Path, 5)) = ".jpeg" Then
    'add row and give reference to it
    Set roe = ActiveDocument.Tables(1).Rows.Add
    'gives reference to cell 1 then adds text
    Set cel = roe.Cells(1)
    cel.Range.Text = pic.Name
    entry = entry + 1
    ' cel.Range.Text = pic.Name & vbCr & pic.DateCreated & vbCr & pic.Size & " Bytes"
    ' Set cel = roe.Cells(1)
    ' Selection.Font.Size = 10
    'gives reference to cell 3 then adds pic
    Set cel = roe.Cells(3)
    Set ish = cel.Range.InlineShapes.AddPicture(FileName:=pic.Path, LinkToFile:=False, SaveWithDocument:=True)
    End If
    Next
    ActiveDocument.Tables(1).Rows(2).Delete
    '******************************************************************

    On Error GoTo error
    Set CurrentDoc = Word.Application.Documents(1)
    With Dialogs(wdDialogFileOpen)
    If .Display <> -1 Then
    Exit Sub
    Else
    Set BrowseFile = Word.Application.Documents.Open(WordBasic.FileNameInfo$(.Name, 1))
    End If
    ' r = row number of table
    ' t = line of text in txt file
    r = 2
    t = 1
    For r = 2 To entry + 1
    CurrentDoc.Tables(1).Cell(r, 1).Range = BrowseFile.Paragraphs(t).Range & BrowseFile.Paragraphs(t + 1).Range _
    & BrowseFile.Paragraphs(t + 2).Range & BrowseFile.Paragraphs(t + 3).Range
    t = t + 6
    Next r
    End With
    BrowseFile.Close
    MsgBox "import Successful"
    Exit Sub
    error:
    BrowseFile.Close
    MsgBox Err.Description
    End Sub
    Thanks,

    Abdullah

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    From the OP:

    "The following lines all give me
    -----------------------------------------------------
    Compile Error: Can't find project or library"


    1. FileSystemObject is usually a reference to Microsoft Scripting Runtime, which Nelviticus mentioned. There may be an exact same object in Windows Script Host, I don't know. However, I DO know that FileSystemObject is in Microsoft Scripting Runtime.

    2. MISSING: Microsoft Common Dialog Control 6.0 (SP3)


    "Is the Missing Common Dialog Control an issue?"

    Ummmmm, what do you think? Considering the fact that you have:

    Dim pth As New MSComDlg.CommonDialog

    So, I would have to say that your problem is my #3.

    "3. Do you have all the proper References?"

    The answer appears to be no, you don't.

    "macro works in word versions previous to 2008"

    Sadly, that (more and more) means little.

  6. #6
    Ok, I figured it was a problem...thanks fumei
    and Nelviticus
    Thanks,

    Abdullah

Posting Permissions

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