Consulting

Results 1 to 4 of 4

Thread: Copying text from Word to Excel with VBA

  1. #1
    VBAX Newbie
    Joined
    Feb 2017
    Posts
    1
    Location

    Copying text from Word to Excel with VBA

    Hello, here I am on my continued journey to paste copied data from Word to Excel. In my latest version of the code, The data is copied, but Excel opens two files (Book1 and Book2) and pastes the info in both windows. I have pored over the code, but I can't figure out what is causing the second file to open. Any help would be greatly appreciated. Thanks, Rob
    Here is my current 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
    
    
    Set Target = oXL.Workbooks.Add
    Set tSheet = Target.Sheets(1)
    tSheet.Paste
    
        Else
        End If
    Set oXL = Nothing
    End Sub

  2. #2
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    try the set target = oxl.workbooks.add

    see if opening the application means that it already has a workbook open
    Remember: it is the second mouse that gets the cheese.....

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    > Set wordDoc = GetObject(, "word.application")
    > wordDoc.Selection.WholeStory
    > Selection.Copy


    you copy the excel range of book1


    try1:
    Set wordDoc = GetObject(, "word.application")
    wordDoc.Selection.WholeStory
    wordDoc.Selection.Copy


    or


    try2:
    Set wordDoc = GetObject(, "word.application")
    wordDoc.activedocument.Range.Copy


    Option Explicit
    
    
    Sub test()
        Dim wordDoc As Object
    
    
        Set wordDoc = GetObject(, "word.application")
        wordDoc.activedocument.Range.Copy
             
        ActiveSheet.Paste
     
    End Sub

  4. #4
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    Does this work?

    My comment before is based on:
    1, you open an excel workbook
    2, you set target to excel(oxl).worbooks.add, and (assuming I do understand this), create a new workbook in the process of setting this object
    Remember: it is the second mouse that gets the cheese.....

Posting Permissions

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