|
-
May 11th, 2005, 09:27 AM
#1
Thread Starter
Lively Member
[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:
Sub CopytoDocument()
Dim cColumn As String
Dim WordObj As Object
Dim CellContents As String
On Error Resume Next
Err.Number = 0
Set WordObj = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set WordObj = CreateObject("Word.Application")
Err.Number = 0
End If
cColumn = InputBox("Which column (indicate the letter)?")
Columns(cColumn & ":" & cColumn).Select
Selection.Copy
'CellContents = Selection
WordObj.Visible = True
WordObj.Documents.Add
Selection.Paste
End Sub
Last edited by pickarooney; May 12th, 2005 at 09:28 AM.
-
May 11th, 2005, 12:32 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 12th, 2005, 08:21 AM
#3
Thread Starter
Lively Member
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.
-
May 12th, 2005, 09:25 AM
#4
Thread Starter
Lively Member
Re: Copying an Excel Column to a Word Doc
Ahh, I get it now! Here's the (qorking) preliminary code - cheers!
VB Code:
Sub CopytoDocument()
Dim cColumn As String
Dim WordObj As Object
Dim ExcelObj As Object
Dim CellContents As String
On Error Resume Next
Err.Number = 0
Set WordObj = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set WordObj = CreateObject("Word.Application")
Err.Number = 0
End If
cColumn = InputBox("Which column (indicate the letter)?")
Columns(cColumn & ":" & cColumn).Select
Selection.Copy
ExcelObj.PutToClipboard
WordObj.Visible = True
WordObj.Documents.Add
WordObj.GetFromClipboard
WordObj.Selection.Paste
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|