Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automation XL2Word (Win XP / O2003 SP3 UK)

    Hi,

    Got a special here. Trying to create an 'instant insert' button within Excel. Intention is that it inserts whatever is selected into the activedocument in Word at the point where the cursor is positioned - regardless.

    In Excel I've come this far - however the final 'paste' will not work... (I think 'Selection' is the problem, but am not sure as my object is late bound)

    Public Sub TEST()
    Dim oWd As Object
    Dim oDoc As Object

    On Error Resume Next

    Set oWd = GetObject(Class:="Word.application")

    If Err.number = 429 Then
    Set oWd = CreateObject(Class:="Word.application")
    Err.Clear
    ElseIf Err.number <> 0 Then
    MsgBox Err.number & "; " & Err.Description
    End If

    Set oDoc = ActiveDocument
    Debug.Print oDoc.Name

    '....some sort of selection - paste'

    oWd.Visible = True

    Set oDoc = Nothing
    Set oWd = Nothing

    End Sub

    Good suggestions are more than welcome / LoL
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation XL2Word (Win XP / O2003 SP3 UK)

    Try

    oWd.Selection.Paste

  3. #3
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation XL2Word (Win XP / O2003 SP3 UK)

    OK - thanks (no good reason why I hadn't try that <img src=/S/barf.gif border=0 alt=barf width=64 height=23>

    Here comes the curl...

    I already has a 'master template' installed and loaded as a global add-in.

    In that one there's two large subs - one handling chart insertion another handling table insertion (let's call them 'Table_insert' and 'Chart_insert')
    Is it possible from within Excel - instead of using oWD.Selection.Paste

    To call the appropiate macro and then again having it inserted at the selected place???
    macro would then look sth. like:

    Public Sub TEST()
    Dim oWd As Object
    Dim oDoc As Object

    'code to decide on type - probably sth. like
    If Typename(Selection) = chart then
    'set chart flag
    elseif Typename(Selection) = Table then
    'set table flag
    end if
    '.................................................

    On Error Resume Next

    Set oWd = GetObject(Class:="Word.application")

    If Err.number = 429 Then
    Set oWd = CreateObject(Class:="Word.application")
    Err.Clear
    ElseIf Err.number <> 0 Then
    MsgBox Err.number & "; " & Err.Description
    End If

    Set oDoc = ActiveDocument
    Debug.Print oDoc.Name

    ' then call one of the two macros in 'Master_Template' based on flag....
    ' and insert the 'item' in the activedocument at 'selection - paste'

    oWd.Visible = True

    Set oDoc = Nothing
    Set oWd = Nothing

    End Sub
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Automation XL2Word (Win XP / O2003 SP3 UK)

    I forgot to mention that

    Set oDoc = ActiveDocument

    is very dangerous - it may create an orphan instance of Word. When using Automation, you must always refer objects back to the application object, directly or indirectly. Here:

    Set oDoc = oWd.ActiveDocument

    You can call a macro in a global template as follows:

    oWd.Run "NameOfMacro"

  5. #5
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Denmark
    Posts
    347
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Automation XL2Word (Win XP / O2003 SP3 UK)

    Hi Hans,

    Thanks - as usual; it worked like a charm <img src=/S/hailpraise.gif border=0 alt=hailpraise width=27 height=22> (almost getting trivial, isn't it <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15> )
    Bests,
    RD


    PS: Wish there were a knob on the TV to turn up the intelligence. There's a knob called "brightness," but that doesn't work

Posting Permissions

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