Consulting

Results 1 to 14 of 14

Thread: Exporting data to Word

  1. #1

    Exporting data to Word

    I have the following code, with everything carefully checked and in place, but it errors out at the line:[vba]Set myDoc = wdApp.Documents.Add(Template:="H:\Storage\My Documents\7 - Training\Forums\ExWd.doc")[/vba] with the message "Object Variable or With block variable not set"

    Sub ExportFinalColumnToWord()
    On Error GoTo errorHandler
    Dim wdApp As Word.Application
    Dim myDoc As Word.Document
    Dim mywdRange As Word.Range
    Dim MyColumnA As Excel.Range
    Dim MyColumnB As Excel.Range
    Dim MyColumnC As Excel.Range
    Dim MyColumnD As Excel.Range
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
       Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    Set myDoc = wdApp.Documents.Add(Template:="H:\Storage\My Documents\7 - Training\Forums\ExWd.doc")
    Set MyColumnA = Sheets("MySheet").Range("A1").End(xlDown).Select
    Set MyColumnB = Sheets("MySheet").Range("B1").End(xlDown).Select
    Set MyColumnC = Sheets("MySheet").Range("C1").End(xlDown).Select
    Set MyColumnD = Sheets("MySheet").Range("D1").End(xlDown).Select
    With myDoc.Bookmarks
         .Item("bmMyColumnA").Range.InsertAfter MyColumnA
         .Item("bmMyColumnB").Range.InsertAfter MyColumnB
         .Item("bmMyColumnC").Range.InsertAfter MyColumnC
         .Item("bmMyColumnD").Range.InsertAfter MyColumnD
    End With
    errorHandler:
    Set wdApp = Nothing
    Set myDoc = Nothing
    Set mywdRange = Nothing
    End Sub
    To explain what the code is (SUPPOSED) to do:

    Find the data at the end of columns A,B,C & D and place it into a word document created from a template "
    ExWd.dot" where I have placed my bookmarks.

    Can Anyone tell me why it is erroring here? I have declared the variable at the start of the code!!??!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    No time to test this, but should template not be a DOT file?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    The template itself is - hence calling (Template:="lalala.doc")

    If I call a *.dot file, it will open the template itself, rather than a file based on the template.

    I've just tried it though:
    Set myDoc = wdApp.Documents.Open "H:\Storage\My Documents\7 - Training\Forums\ExWd.doc"
    and
    Set myDoc = wdApp.Documents.Open "H:\Storage\My Documents\7 - Training\Forums\ExWd.dot"
    , just in case! Still, no go!

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This works for me (Office 2000)
     
    Option Explicit
    Sub ExportFinalColumnToWord()
    On Error GoTo errorHandler
    Dim wdApp As Word.Application
    Dim myDoc As Word.Document
    Dim mywdRange As Word.Range
    Dim MyColumnA As Excel.Range
    Dim MyColumnB As Excel.Range
    Dim MyColumnC As Excel.Range
    Dim MyColumnD As Excel.Range
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    Set myDoc = wdApp.Documents.Add(Template:="M:\0000 General\Test.doc")
    Set MyColumnA = Sheets("MySheet").Range("A1").End(xlDown)
    Set MyColumnB = Sheets("MySheet").Range("B1").End(xlDown)
    Set MyColumnC = Sheets("MySheet").Range("C1").End(xlDown)
    Set MyColumnD = Sheets("MySheet").Range("D1").End(xlDown)
    With myDoc.Bookmarks
        .Item("bmMyColumnA").Range.InsertAfter MyColumnA
        .Item("bmMyColumnB").Range.InsertAfter MyColumnB
        .Item("bmMyColumnC").Range.InsertAfter MyColumnC
        .Item("bmMyColumnD").Range.InsertAfter MyColumnD
    End With
    wdApp.Visible = True
    Exit Sub
    errorHandler:
    wdApp.Quit
    Set wdApp = Nothing
    Set myDoc = Nothing
    Set mywdRange = Nothing
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Use DIR() to determine if your file actually exists before using .Add or .Open.

  6. #6
    Quote Originally Posted by mdmackillop
    This works for me (Office 2000)
    ??

    Strange?? Still bugging out at the same place for me in MSO 2003 - even moved files to C:\Tempo to shorten the path!

    copied our added code, (removed '.Select, too - thanks for spotting that), still not having any of it!

    I'm sure it should work!

  7. #7
    Quote Originally Posted by Kenneth Hobs
    Use DIR() to determine if your file actually exists before using .Add or .Open.
    Can you explain that in a bit more detail please Kenneth!

  8. #8
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I could not get your code to show an error on that line.

    Here is how you can test for the file.
    Sub ExportFinalColumnToWord()
        Dim wdApp As Word.Application
        Dim myDoc As Word.Document
        Dim mywdRange As Word.Range
        Dim MyColumnA As Excel.Range
        Dim MyColumnB As Excel.Range
        Dim MyColumnC As Excel.Range
        Dim MyColumnD As Excel.Range
        Dim doc As String
        doc = "x:\msword\MyFile.doc"
        If Dir(doc) = "" Then
            MsgBox "Error, file does not exist." & vbLf & doc, vbCritical, "File is Missing"
            Exit Sub
        End If
        On Error Resume Next
        Set wdApp = GetObject(, "Word.Application")
        If Err.Number <> 0 Then
             Set wdApp = CreateObject("Word.Application")
        End If
        On Error GoTo errorHandler
        Set myDoc = wdApp.Documents.Add(Template:=doc)
        wdApp.Visible = True
        GoTo errorExit
        Set MyColumnA = Sheets("MySheet").Range("A1").End(xlDown).Select
        Set MyColumnB = Sheets("MySheet").Range("B1").End(xlDown).Select
        Set MyColumnC = Sheets("MySheet").Range("C1").End(xlDown).Select
        Set MyColumnD = Sheets("MySheet").Range("D1").End(xlDown).Select
        With myDoc.Bookmarks
             .Item("bmMyColumnA").Range.InsertAfter MyColumnA
             .Item("bmMyColumnB").Range.InsertAfter MyColumnB
             .Item("bmMyColumnC").Range.InsertAfter MyColumnC
             .Item("bmMyColumnD").Range.InsertAfter MyColumnD
        End With
        Set wdApp = Nothing
        Set myDoc = Nothing
        Set mywdRange = Nothing
        Exit Sub
        errorExit:
        On Error Resume Next
        Set wdApp = Nothing
        Set myDoc = Nothing
        Set mywdRange = Nothing
        Exit Sub
        errorHandler:
        MsgBox "Unexpected error: " & Err.Number & vbLf & Err.Description
        Resume errorExit
    End Sub

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Bird Fat,

    Interested in a non-vba solution? Without relying on Word's fragile bookmarks? If so, read on.

    1. open both the Word doc and the Excel wb.
    2. name each of the cells containing the data to be exported to Word
    3. copy one of the cells containing the data to be exported to Word
    4. in Word, use Edit|Paste Special, check 'paste link' and choose an appropriate paste format.
    5. in Word, select the pasted value and press Shift F9 to expose the field code. It should look something like
    { LINK Excel.Sheet.8 "C:\\Users\\System\\Documents\\Technical\\Data.XLS" "Sheet1!R1C1" \a \r }
    6. Change 'Sheet1!R1C1' to your Excel name for this address, then press F9 to update the field.
    7. copy & paste the modified field to each of the other locations where you want data from the Excel wb to appear.
    8. repeat steps 6 & 7, updating the range name to match the relevant locations in the Word doc with the Excel data.

    Having done the above, adding/deleting rows/columns other than the named ones will cause the Word document to point to the new addresses in Excel. Having the links update automatically in Word is as simple as checking the 'update automatic links at open' option under tools|Options|General in Word.

    If, perchance, you have an issue in that the named cells are liable to be overwritten or deleted (ie they don't get relocated), you could use a secondary worksheet in the Excel wb to retrieve the data from the primary worksheet (eg via a formula that retrieves the data from the last row), and use the secondary worksheet as the data source for the Word document.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    Quote Originally Posted by Kenneth Hobs
    I could not get your code to show an error on that line.
    And I can't get it to go PAST that line

    I'm thinking it maybe that I need to reinstall Office - with a proper clean out first!

    I'll do that now and get back to you all!

    And thanks to macropod - didn't think of using field codes - even though I have just recently solved someone elses question by using them, and placing an update on open into the code - DOH!

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm not suggesting Word is the problem, but if you are reinstalling Office, have a read of this.
    http://www.theofficeexperts.com/word...leshootingWord
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Quote Originally Posted by mdmackillop
    I'm not suggesting Word is the problem, but if you are reinstalling Office, have a read of this.
    http://www.theofficeexperts.com/word...leshootingWord
    Thanks, read that one before!

    It WAS the installation - had some stuff leftover in the installation menu AND the registry - cleaned it all up and reinstalled (I do this quite often as I'm a fiddler with reg settings, so I have it as an unattended set up on my HD, so no real hassles!) ...


    ... Guess what?


    ... That's right - all working fine!


    Thanks for all the help guys - see you again soon, I have no doubt ;-)

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I could only get that error when it tried to locate a bookmark that did not exist. Have you tried stepping through the code with F8 to be sure that the Set line is the line that errors?

  14. #14
    Quote Originally Posted by Kenneth Hobs
    I could only get that error when it tried to locate a bookmark that did not exist. Have you tried stepping through the code with F8 to be sure that the Set line is the line that errors?

    Turns out it was something to do with the install at the end of the day Kenneth, thanks anyway.
    Bird

    Windows XP (32bit) > Office 2003 / Windows 7 (64bit) > Office 2007


    • Word - Expert
    • Excel - Expert
    • Outlook
    • PowerPoint

Posting Permissions

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