Solved

copy excel range to word document

Posted on 2013-06-16
11
558 Views
Last Modified: 2013-06-26
Hello,

I would like to copy an excel range "Sheet1. range A1:F35" to a Word document with the following path:

C:\Users\Centa\Trader Journals\TEST\test.docx

The macro should run using Sheet1 as the Active Sheet.
Everytime the macro is run, it should add a new page to the word document

I have included an example of the spreadsheet.

Thanks,
CC
example-copyToWord.xlsm
0
Comment
Question by:CC10
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:djsharma
ID: 39251115
0
 

Author Comment

by:CC10
ID: 39251317
maybe a misunderstanding. I need a macro that copies the range.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39252745
Not sure where you are stuck, but you'll need something like this. Don't forget to add a reference to the Microsoft Word Object library.
Sub CopyToWord()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim sh As Worksheet
Dim bNewApp As Boolean

Set sh = ActiveWorkbook.Sheets(1)
sh.Range("A1", "C2").Copy

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
    Set wdApp = CreateObject("Word.Application")
    bNewApp = True
End If
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open("C:\MyFolder\Mydoc.docx")
With wdDoc
    .Bookmarks("\EndofDoc").Range.Text = vbFormFeed
    .Bookmarks("\EndofDoc").Range.Paste
    .Close wdSaveChanges
End With
If bNewApp Then
    wdApp.Quit
End If
End Sub

Open in new window

0
 

Author Comment

by:CC10
ID: 39254007
I added the reference "Microsoft Word 12.0 Object Library" to the Excel Reference list (ticked the box) but obviously that is not the solution as I get a pop up saying Library not registered. I am not savvy with Word macros. What do I have to do there?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39254869
It isn't a Word macro, it's written to run in Excel.

Usually if the library isn't referenced, you get a 'Compile error: User-defined type not defined' message when you try to run the code.

I have never seen that error message. Is that the full text? If not, can you report the full message, please.
Is a particular code line highlighted? If so, which line?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:CC10
ID: 39255337
Error Message:

Run Time error 2147319779 (800280id)
Automation error
Library not registered

At the code line:

at the Set wdApp = createObject ("Word.Application")

If I switch back to the worksheet, it is frozen on the picture of the Logo.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 total points
ID: 39255890
Try this general fix for Word problems here:
http://support.microsoft.com/default.aspx/kb/921541
0
 

Author Comment

by:CC10
ID: 39256409
I found this on the link you sent me:

Important This section, method, or task contains steps that tell you how to modify the registry. However, serious problems might occur if you modify the registry incorrectly. Therefore, make sure that you follow these steps carefully. For added protection, back up the registry before you modify it. Then, you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following article number to view the article in the Microsoft Knowledge Base:
322756 How to back up and restore the registry in Windows

    Find the line of code that is failing, for example:

    Server.CreateObject("MSWC.BrowserType")
                                        

    The text string, "MSWC.BrowserType", is the ProgID (Program ID).
    On the Start menu, click Run. At the command prompt, type the following command to start the registry editor:
    RegEdt32.exe
    In the HKEY_CLASSES_ROOT window, click to expand the ProgID key, and then click CLSID. Record the UUID value of this key. For example, the ClassID for "MSWC.BrowserType" is {0ACE4881-8305-11CF-9427-444553540000}.
    In the HKEY_CLASSES_ROOT window, click to expand the CLSID key, and then select the ClassID entry (UUID). On the Security menu, click Permissions. Make sure that the Everyone entry has Read permission; if not, add this permission entry.

I found the PROGiD key for Word.Application, changed the permission but it did not solve the problem.

To be honest, I have no idea what I am doing here!
0
 

Author Comment

by:CC10
ID: 39258989
in Word, VB, Tools, References, I have ticked the boxes:

Microsoft Word 12.0 Object Library
Microsoft Excel 12.0 Object Library
Microsoft Office 12.0 Object Library
Microsoft HTML 12.0 Object Library
Visual basis For Applications
OLE Automation

In Excel
I have ticked the same

It still doesn't work
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39259020
You seem to have found this page: http://support.microsoft.com/kb/274038. I did have a look at it, but I didn't post it, because it refers to a similar message in ASP, not Word.

The link that I posted addresses more general problems with Word, and has a Fix-it button to do the job.

If that doesn't work, we'll look at registering some DLLs, but that's a bit more complicated.
0
 

Author Comment

by:CC10
ID: 39259125
I tried the Fix it button but that only concentrates on standard stuff, printers, add.ins etc...
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printer Setup Pop-up when Opening Excel File 4 368
Microsoft Access 2010, using same Header 4 51
Word 2010 mail merge 3 65
PowerPoint Links 7 38
As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now