Thread: [Solved] Opening excel from word
View Single Post
 
Old 02-25-2017, 08:52 PM
AXbean AXbean is offline Windows 10 Office 2013
Novice
 
Join Date: Feb 2017
Posts: 5
AXbean is on a distinguished road
Default Opening excel from word

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 ExportwordtoexcelNew()
    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 ExcelWasNotRunning As Boolean
    

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


'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")



'Install Add-ins
If Err Then
     ExcelWasNotRunning = True
     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
'oXL.ActiveWindow.Close SaveChanges:=False

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

'Minimize word so you can see Excel
'wordDoc.Application.WindowState = wdWindowStateMinimize


    Else
    End If
Set oXL = Nothing
End Sub
Reply With Quote