Opening excel from word

axbean

New member
Joined
Feb 26, 2017
Messages
1
Reaction score
0
Points
0
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, two windows open and the selection is pasted in both windows. How do I get it to only open one window?
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
 
http://www.msofficeforums.com/word-vba/34491-opening-excel-word.html

Hi, Please,do not crosspost your question on multiple forums without including links here to the other threads on other forums.

Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

We are here to help so help us to help you!

Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
 
Last edited:
Back
Top