[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

copy excel range to word document

Posted on 2013-06-16
11
Medium Priority
?
573 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 77

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

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 77

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
 

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 77

Accepted Solution

by:
GrahamSkan earned 1200 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 77

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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
I came across an unsolved Outlook issue and here is my solution.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

612 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