Solved

copy excel range to word document

Posted on 2013-06-16
11
556 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
Comment Utility
0
 

Author Comment

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

Expert Comment

by:GrahamSkan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:CC10
Comment Utility
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
Comment Utility
Try this general fix for Word problems here:
http://support.microsoft.com/default.aspx/kb/921541
0
 

Author Comment

by:CC10
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I tried the Fix it button but that only concentrates on standard stuff, printers, add.ins etc...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now