Opening excel from word, no add-ins

rbwallis

New Member
Joined
Mar 5, 2015
Messages
9
Hello experts! I have a macro in Word that is supposed to open Excel and paste a previous selection from Word into a new spreadsheet. It all works very well, except when Excel starts up programmatically, my custom add-in does not load. It loads fine if I start Excel by itself, so it must be something with opening programmatically.
Here is the code from Word:

Code:
Sub Exportwordtoexcel(control As IRibbonControl)
    Dim wordDoc As Object
    Dim oXL As Excel.Application
    Dim DocTarget As Word.Document
    Dim Target As Excel.Workbook
    Dim tSheet As Excel.Worksheet
    
Dim YesOrNoAnswerToMessageBox As String
Dim QuestionToMessageBox As String
    QuestionToMessageBox = "Do you want Excel to open and paste your selection?"
    YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "QuikBots for Word")
    If YesOrNoAnswerToMessageBox = vbYes Then
    
Set wordDoc = GetObject(, "word.application")
Selection.Copy

'If Excel is running, get a handle on it; otherwise start a new instance of Excel
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
      Set oXL = New Excel.Application
End If

oXL.Visible = True

Set Target = oXL.Workbooks.Add
Set tSheet = Target.Sheets(1)
tSheet.Paste
 
    Else
    End If

End Sub


Can you help?

Thanks, Robert
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Thank you for all of your advice. I still have the problem, with the selection pasting into two windows. If anyone can help make this not open in Excel twice, I would really appreciate it. Here is the current code:
Code:
Sub Exportwordtoexcel(control As IRibbonControl)
    Dim wordDoc As Object
    Dim oXL As Excel.Application
    Dim DocTarget As Word.Document
    Dim Target As Excel.Workbook
    Dim tSheet As Excel.Worksheet
    Dim oAddIn As Excel.AddIn
    
Dim YesOrNoAnswerToMessageBox As String
Dim QuestionToMessageBox As String
    QuestionToMessageBox = "Do you want Excel to open and paste your selection?"
    YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "QuikBots for Word")
    If YesOrNoAnswerToMessageBox = vbYes Then
    
Set wordDoc = GetObject(, "word.application")
wordDoc.Selection.WholeStory
Selection.Copy

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err Then
    Set oXL = New Excel.Application
    For Each oAddIn In oXL.AddIns
        With oAddIn
            If .Installed Then
                .Installed = False
                .Installed = True
            End If
        End With
    Next oAddIn
End If

oXL.Visible = True

Set Target = oXL.Workbooks.Add
Set tSheet = Target.Sheets(1)
tSheet.Paste
 
    Else
    End If

End Sub
 
Upvote 0
It sounds like you have two windows open on the same workbook but there is nothing in the code that should cause that. Do you have a book.xltx template in your XLSTART folder?
 
Upvote 0
Why don't you try emulating the opening of excel like via the user interface by using the Shell Command .. that way all addins should be automatically loaded.
Then use GetObject to get a pointer to the newly opened application/workbook and do your pasting.
 
Upvote 0
Cross-posted:
Opening excel from word
Opening excel from word
Opening excel from word

Seems like you posted this question all over the place. While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule #13 here: http://www.mrexcel.com/forum/showthread.php?t=99490). That way people can see what has already been suggested and they don't waste their time working on solutions that might have already been proposed elsewhere, or answering a question which may already be answered elsewehere.
 
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,658
Members
452,575
Latest member
Fstick546

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