+ Reply to Thread
Results 1 to 3 of 3

Trouble running macro for Word.Application

Hybrid View

dannyjoer Trouble running macro for... 10-14-2013, 04:55 AM
OllieB Re: Trouble running macro for... 10-14-2013, 05:07 AM
dannyjoer Re: Trouble running macro for... 10-14-2013, 05:12 AM
  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Trouble running macro for Word.Application

    Hi Guys

    I get an error each time I try to execute the following code. It is a Compile Error: User-defined type not defined.
    This is the only module within this workbook.

    Option Explicit
    Const FilePath As String = "C:\Documents and Settings\DAJO\My Documents\MD-CPH\ExcelSampleFiles\VBABMcopy\d5.docx"
    
    Dim wd As New Word.Application
    Dim PersonCell As Range
    Sub CreateWordDocuments()
    
    Dim doc As Word.Document
    wd.Visible = True
    Dim PersonRange As Range
    'create a reference to all the people
    Range("A1").Select
    Set PersonRange = Range( _
    ActiveCell, _
    ActiveCell.End(xlDown))
    
    For Each PersonCell In PersonRange
    
    Set doc = wd.Documents.Open(FilePath & "Word form.docx")
    'go to each bookmark and type in details
    CopyCell "FirstName", 1
    CopyCell "LastName", 2
    CopyCell "Company", 3
    'save and close this document
    doc.SaveAs2 FilePath & "person " & PersonCell.Value & ".docx"
    doc.Close
    Next PersonCell
    wd.Quit
    MsgBox "Created files in " & FilePath & "!"
    End Sub
    Sub CopyCell(BookMarkName As String, ColumnOffset As Integer)
    'copy each cell to relevant Word bookmark
    wd.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName
    wd.Selection.TypeText PersonCell.Offset(0, ColumnOffset).Value
    End Sub
    What am I doing wrong?

    Best regards
    Danny

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Trouble running macro for Word.Application

    Danny,

    you are using a technique that is called early-binding as is evidenced for example by the statement

    Dim wd As New Word.Application
    If you are getting the message "User defined type..." that probably means that you have not set the reference to the Microsoft Word Object library. Go to Tools/Preferences and select the correct library.
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    Copenhagen
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: Trouble running macro for Word.Application

    Ah of course.. Thanks for the fast reply..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. trouble running a macro that refers to the range using a formula
    By lukensaputra in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-30-2013, 05:13 AM
  2. Trouble hiding page breaks after running macro
    By Einstahb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2011, 10:40 PM
  3. Trouble Running IF Statement while recording Macro
    By MacroNovice in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-16-2011, 03:40 PM
  4. Running macro-powered application
    By MichielM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-04-2011, 05:09 AM
  5. Excel VBA Macro Running DOS Application !!
    By monir in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-25-2006, 11:00 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1