Excel Macro to open and save a word doc

paulontour

New Member
Joined
Jul 28, 2011
Messages
13
Hi

Could someone help me with some code to open an existing word document from Excel, paste in some information from the excel workbook to the word doc INTO A CERTAIN PLACE ON THE WORD DOC, then save it using data held in one of the cells in the excel workbook. The data to save it is basically a number, followed by 3 words which are used on every one.

Thanks
Paul
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to the forum Paul.

Try this to Open and Add data to Document. You need to set the reference from Excel to Use Word code. Use Alt + F11 to go into VBA from your workbook, then select Tools Menu > Reference > Scroll down to Microsoft Word XX.Object Library (XX being a number) > click the box, and OK next Insert Menu and then Paste in this Code.

I am using a BookMark name in the Word Document, so make sure there is a BookMark in the Word Document before you run this code.

Sub copyToWord()
'The following code needs the References set to Microsoft Word
'Select Tools Menu > References > Scroll down to Microsoft Word XX.Object Library
'Tick the box and OK

Dim wrdApp As Word.Application
Set wrdApp = CreateObject("Word.Application")
Application.ScreenUpdating = False
Range("A1:B10").Copy 'Change range and maybe add sheet name
With wrdApp
.Documents.Open "Path to Doc.doc" 'Place in full path and Document name including .doc
.Selection.GoTo What:=wdGoToBookmark, Name:="test1" 'Using a bookmark in the document
.Selection.PasteAndFormat (wdPasteDefault)
.Visible = True
.Documents.Save
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
MsgBox "Data Copied Across", vbInformation, "Sample"

End Sub
 
Upvote 0
As it's been a couple of hours and no-one's responded yet, perhaps I can set the ball rolling.

I suggest you start with a new Excel workbook for test purposes. In VBA, create a reference to the Microsoft Word Object Library (Tools > References) and then create a new general code module and paste this code into it:-
Code:
[FONT=Fixedsys]Option Explicit[/FONT]
 
[FONT=Fixedsys]Public Sub ModifyWordDoc()[/FONT]
 
[FONT=Fixedsys] Dim wordApp As Word.Application[/FONT]
[FONT=Fixedsys] Dim wordDoc As Word.Document[/FONT]
[FONT=Fixedsys] Dim wordRange As Word.Range[/FONT]
[FONT=Fixedsys] Dim sFilename As String[/FONT]
 
[FONT=Fixedsys] Set wordApp = CreateObject("Word.Application")[/FONT]
[FONT=Fixedsys][COLOR=magenta] sFilename = Application.GetOpenFilename(FileFilter:="Word documents (*.doc*), *.doc*")[/COLOR][/FONT]
[FONT=Fixedsys][COLOR=magenta] If sFilename = "False" Then Exit Sub[/COLOR][/FONT]
 
[FONT=Fixedsys] With wordApp[/FONT]
[FONT=Fixedsys]   .WindowState = Word.WdWindowState.wdWindowStateMaximize[/FONT]
[FONT=Fixedsys]   Set wordDoc = .Documents.Open(sFilename)[/FONT]
[FONT=Fixedsys]   Set wordRange = wordDoc.Range[/FONT]
[FONT=Fixedsys]   With wordRange[/FONT]
[FONT=Fixedsys][COLOR=blue]     .Move wdStory, 1[/COLOR][/FONT]
[FONT=Fixedsys][COLOR=blue]     .InsertAfter ThisWorkbook.Sheets(1).Range("A1").Value & vbCrLf[/COLOR][/FONT]
[FONT=Fixedsys]   End With[/FONT]
[FONT=Fixedsys]   .ActiveDocument.Save[/FONT]
[FONT=Fixedsys]   .ActiveDocument.Close[/FONT]
[FONT=Fixedsys]   .Application.Quit[/FONT]
[FONT=Fixedsys] End With[/FONT]
 
[FONT=Fixedsys] Set wordDoc = Nothing[/FONT]
[FONT=Fixedsys] Set wordApp = Nothing[/FONT]
 
[FONT=Fixedsys][COLOR=red] MsgBox "Done: " & sFilename & " modified" & Space(10), vbOKOnly + vbInformation[/COLOR][/FONT]
 
[FONT=Fixedsys]End Sub[/FONT]
To test, enter some text in cell A1 of your worksheet and run the code. The Word document must already exist - as this is what you indicated in your request - so create a new empty test document first if necessary. When the macro ends, open the document and check that the text in cell A1 of the worksheet has appeared in there.

The code in pink is for invoking a file dialog box for the selection of your Word document: if you wish to hard-code the file name, simply replace all of the pink code with an sFilename="C:\Folder\Document.doc" type statement.

The MsgBox command can be removed entirely if you wish, obviously.

The code in blue is the bit where you navigate your way through the Word document in order to find the exact place where you want your worksheet text to be inserted. Sadly, this is one bit I can't help you with. As it stands, the text will always be inserted at the end of the document - or at least I think that's what's happening! You need to find out what the correct Word commands are for moving to the correct place in your particular document, and replace my blue code with your own commands for doing this.

Hopefully this will give you a good start on your path to a solution.

**Edit: I see there's been another reply whilst I was cobbling this together. I'd go with Trevor's suggestion!
 
Upvote 0
Hi ;
Please can some one still intersted with this thread.
I have a similar probleme , But , I want to save records from multiple word documents in One word document.
I take into account to modify my macro code for optimization due to:
My macro creates for each line of the excel file a new document and saves it under the name of the 2nd column of row j .. and as result I 'to get an excel file with 5 lines, 5 word document with multiple users and the number becomes huge! So I would like to assemble in one single word document, I mean, instead of creating a new line for each document, the macro should create a new page in a single document, and as result I would get with only 1 word document 5 pages for an excel file 5 online. is - Can I please??

 
Last edited:
Upvote 0
Did you know you can record macros in Word and then adapt the code to work from Excel. This is sample, I have basically recorded to move to the end of a document and enter a page break that would give you the code you need to stay within a document and add a page break then do what you need to do.

Sub Macro20()
'
' Macro20 Macro
'
'
Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,575
Messages
6,179,637
Members
452,934
Latest member
Jdsonne31

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top