Results 1 to 4 of 4

Thread: [RESOLVED]Copying an Excel Column to a Word Doc

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Resolved [RESOLVED]Copying an Excel Column to a Word Doc

    I have a series of Excel spreadsheets, each with multiple worksheets.
    I'd like to copy a certain column (user-defined) from each worksheet on the spreadsheet and paste it to a word doc, with the name of the worksheet above each copied column (the columns become tables when pasted to Word).

    The contents of the clipboard seem to get lost when moving between the two applications. What would be the best way to transfer the data from one program to the other, retaining the cell-style layout?

    What I have so far is:
    VB Code:
    1. Sub CopytoDocument()
    2.  
    3.     Dim cColumn As String
    4.     Dim WordObj As Object
    5.     Dim CellContents As String
    6.     On Error Resume Next
    7.     Err.Number = 0
    8.    
    9.     Set WordObj = GetObject(, "Word.Application")
    10.     If Err.Number = 429 Then
    11.         Set WordObj = CreateObject("Word.Application")
    12.         Err.Number = 0
    13.     End If
    14.    
    15.     cColumn = InputBox("Which column (indicate the letter)?")
    16.     Columns(cColumn & ":" & cColumn).Select
    17.    
    18.     Selection.Copy
    19.     'CellContents = Selection
    20.    
    21.     WordObj.Visible = True
    22.     WordObj.Documents.Add
    23.    
    24.     Selection.Paste
    25.    
    26. End Sub
    Last edited by pickarooney; May 12th, 2005 at 09:28 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying an Excel Column to a Word Doc

    Since both Excel and Word have a "Selection" object you need to differenciate between the two. Try creating variables
    like xlSel and wdSel. You may do beter with the clipboard.copy and clipboard.paste.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Copying an Excel Column to a Word Doc

    I tried Clipboard.Copy but although it compiles, it doesn't seem to do anything, and the help file has no mention of this object.

    In order to use the xlSel and wdSel variables, what type should I declare them as? There seems to be no Selection object. My VB editor in Excel is acting strangely, it doesn't offer me any choices of objects/properties when I type the variable name followed by a dot, the way it does in Word.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    103

    Re: Copying an Excel Column to a Word Doc

    Ahh, I get it now! Here's the (qorking) preliminary code - cheers!

    VB Code:
    1. Sub CopytoDocument()
    2.  
    3.     Dim cColumn As String
    4.     Dim WordObj As Object
    5.     Dim ExcelObj As Object
    6.     Dim CellContents As String
    7.     On Error Resume Next
    8.     Err.Number = 0
    9.    
    10.     Set WordObj = GetObject(, "Word.Application")
    11.     If Err.Number = 429 Then
    12.         Set WordObj = CreateObject("Word.Application")
    13.         Err.Number = 0
    14.     End If
    15.    
    16.     cColumn = InputBox("Which column (indicate the letter)?")
    17.     Columns(cColumn & ":" & cColumn).Select
    18.    
    19.     Selection.Copy
    20.     ExcelObj.PutToClipboard
    21.        
    22.     WordObj.Visible = True
    23.     WordObj.Documents.Add
    24.    
    25.     WordObj.GetFromClipboard
    26.     WordObj.Selection.Paste
    27.    
    28. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width