Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have an excel file that has a bunch of different numbers and text
that I would like to go into different tables in a word file. I am trying to make my creating reports easier, as only certain numbers/ dates/etc. will change. If I could type everything in excel, then run a macro, that would be great. Is there a way to write a macro to work with both excel and word? Will the word file need to be formatted in a certain way? Thanks. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
There are so many things you can do. In your excel workbook, you need to add
a sheet named 'Template' and run this code: In the Visual Basic Editor, set a reference to Word. click Tools references Microsoft Word 10.0 (for Word 2002; 11.0 for newer versions). Sub ControlWord() ' You must pick Microsoft Word 8.0 from ToolsReferences ' in the VB editor to execute Word commands. ' See VB Help topic "Controlling One Microsoft Office Application from Another" ' for more information. ' Originally published by www.MrExcel.com 2/28/1999 Dim appWD As Word.Application ' Create a new instance of Word & make it visible Set appWD = CreateObject("Word.Application.8") appWD.Visible = True Sheets("Data").Select 'Find the last row with data in the database FinalRow = Range("A9999").End(xlUp).Row For i = 2 To FinalRow Sheets("Data").Select ' Copy the name to cell C4 Range("A" & i).Copy Destination:=Sheets("Template").Range("A1:J1") ' Copy data columns, transpose and paste in C10:C13 Range("B" & i & ":J" & i).Copy Sheets("Template").Select Range("C10").PasteSpecial Transpose:=True ' Copy the data for the new document to the clipboard Range("A1:F15").Copy ' Tell Word to create a new document appWD.Documents.Add ' Tell Word to paste the contents of the clipboard into the new document appWD.Selection.Paste ' Save the new document with a sequential file name appWD.ActiveDocument.SaveAs Filename:="File" & i ' Close this new word document appWD.ActiveDocument.Close Next i ' Close the Word application appWD.Quit End Sub You can also try this: Sub CopyWorksheetsToWord() ' requires a reference to the Word Object library: ' in the VBE select Tools, References and check the Microsoft Word X.X object library Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet Application.ScreenUpdating = False Application.StatusBar = "Creating new document..." Set wdApp = New Word.Application Set wdDoc = wdApp.Documents.Add For Each ws In ActiveWorkbook.Worksheets Application.StatusBar = "Copying data from " & ws.Name & "..." ws.UsedRange.Copy ' or edit to the range you want to copy wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Ins ertParagraphAfter wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Pas te Application.CutCopyMode = False wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Ins ertParagraphAfter ' insert page break after all worksheets except the last one If Not ws.Name = Worksheets(Worksheets.Count).Name Then With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range .InsertParagraphBefore .Collapse Direction:=wdCollapseEnd .InsertBreak Type:=wdPageBreak End With End If Next ws Set ws = Nothing Application.StatusBar = "Cleaning up..." ' apply normal view With wdApp.ActiveWindow If .View.SplitSpecial = wdPaneNone Then .ActivePane.View.Type = wdNormalView Else .View.Type = wdNormalView End If End With Set wdDoc = Nothing wdApp.Visible = True Set wdApp = Nothing Application.StatusBar = False End Sub If that doesn't work for you, take a look at this: http://word.mvps.org/faqs/InterDev/C...WordFromXL.htm If that doesn't work, look he http://word.mvps.org/FAQs/InterDev/C...XLFromWord.htm Regards, Ryan--- -- RyGuy " wrote: I have an excel file that has a bunch of different numbers and text that I would like to go into different tables in a word file. I am trying to make my creating reports easier, as only certain numbers/ dates/etc. will change. If I could type everything in excel, then run a macro, that would be great. Is there a way to write a macro to work with both excel and word? Will the word file need to be formatted in a certain way? Thanks. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
From personal experience of having ot do the same (with little/no programming
experience) I can also highly recommend Jon Peltier's site: http://peltiertech.com/Excel/XL_PPT.html " wrote: I have an excel file that has a bunch of different numbers and text that I would like to go into different tables in a word file. I am trying to make my creating reports easier, as only certain numbers/ dates/etc. will change. If I could type everything in excel, then run a macro, that would be great. Is there a way to write a macro to work with both excel and word? Will the word file need to be formatted in a certain way? Thanks. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Converstion of Complex Word VBA macro to Excel Macro | Excel Programming | |||
Excel to run Word macro & Word returns value to be written in spreadsheet | Excel Programming | |||
Need syntax for RUNning a Word macro with an argument, called from an Excel macro | Excel Programming | |||
WORD-DELIMITED string vba macro for excel/word | Excel Programming | |||
passing arguments from an excel macro to a word macro | Excel Discussion (Misc queries) |