Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2017, 08:52 PM
AXbean AXbean is offline Opening excel from word Windows 10 Opening excel from word Office 2013
Novice
Opening excel from word
 
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
  #2  
Old 02-25-2017, 10:32 PM
gmayor's Avatar
gmayor gmayor is offline Opening excel from word Windows 10 Opening excel from word Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Try the following, however Word nd Excel formats are different from one another so I am not sure of the point of the exercise.

Code:
Sub ExportWordtoExcelNew()
Dim wordDoc As Document
Dim oXL As Object
Dim Target As Object
Dim tSheet As Object
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 = ActiveDocument
        wordDoc.Range.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 = CreateObject("Excel.Application")
        End If
        On Error GoTo 0

        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
    End If
    Set wordDoc = Nothing
    Set oXL = Nothing
    Set Target = Nothing
    Set tSheet = Nothing
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 02-26-2017, 12:59 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline Opening excel from word Windows 7 64bit Opening excel from word Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,779
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

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)

http://www.excelguru.ca/forums/showt...xcel-from-word
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening excel from word Hyperlinks error msg on opening in Excel, Word matilda Excel 3 05-04-2015 07:06 AM
Opening excel from word Run Time Error 4248 when opening Word Doc from Excel CIF Excel Programming 12 02-21-2014 02:27 PM
Opening excel from word Word Document Not Opening With Excel VBA JennEx Word VBA 3 06-20-2013 10:10 AM
Slow Opening Word and Excel files itoa Office 1 05-11-2009 02:01 PM
Trick to restrict opening excel/word ?? 9morebusy Excel 0 11-12-2008 04:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:35 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft