VBA macro to create a word report

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52
Hello,

I am trying to create a macro that will dump values from several excel fields into a Word template and begin printing it. I'd like to embed the word template within the spreadsheet, so that users do not need to download 2 files -I'm not sure how to go about embedding the word document or making the macro interface with Word.

Any help is appreciated. I'm using Excel 2007 for reference and I already have the Developer tab up and all that fun stuff -just not sure how to write the code!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
What you have asked may take a while to answer since it involves several concepts. You will get help faster if you ask one question per thread. You can always add a link to a related thread in a new one.

To start off, insert your MSWord file as an object by Insert > Object. Name the object, oDoc.

Some concepts that will help you, can be gleamed from this code:
Code:
'Useful to paste MSWord in files to Excel sheets, xld, http://www.vbaexpress.com/forum/showthread.php?t=32947
Sub test_PasteOLEObjectIntoSheet()
  PasteOLEObjectContentIntoSheet "oDoc", "Sheet2"
End Sub

'Set Reference to Microsoft Word xx.x Object Library
Sub PasteOLEObjectContentIntoSheet(sOLE As String, sSheet As String)
  Dim wdApp As Word.Application
  Dim myDoc As Word.Document
  Dim wdstory As Word.Range
  Dim oOLE As OLEObject
    
  Set oOLE = ActiveSheet.OLEObjects(sOLE)
  'MsgBox oOLE.OLEType  'Linked or not.
  oOLE.Verb xlPrimary
  Set wdApp = GetObject(, "Word.Application")
  'wdApp.Visible = True
  wdApp.Visible = False 'OLEObect will show briefly even with this setting.
  Set myDoc = wdApp.ActiveDocument
    
  Do
  Loop Until Not myDoc Is Nothing

'  MsgBox myDoc.Name
  Set wdstory = myDoc.Content
  'Same as that above
  'Set wdstory = myDoc.StoryRanges(wdMainTextStory)
  With wdstory.Font
    .Name = "Arial"
    .ColorIndex = wdRed
    .Size = 16
  End With

      
  With Excel.ThisWorkbook.Worksheets(sSheet)
    .UsedRange.Clear
    'Setting the value like this does not include attributes.
    .Range("A1").Value = myDoc.Content
    
    myDoc.Range(myDoc.Content.Start, myDoc.Content.End).Copy
    
    Excel.Worksheets(sSheet).Range("A2").PasteSpecial
    'Another method to paste.
    'ThisWorkbook.Worksheets(sSheet).Activate
    'Excel.ActiveSheet.Paste
    'ActiveSheet.Paste
    Application.CutCopyMode = False
  End With
   
  myDoc.Close
  wdApp.Quit
  Set wdApp = Nothing
  Set myDoc = Nothing
  Set wdstory = Nothing
End Sub

Doing batch processes is a fairly common FAQ. Here is an example of the Dir() method.
Code:
Sub t()
  Dim FileName As String
  FileName = Dir(ThisWorkbook.Path & "\*_*_*.xls")
'   Loop until no more matching files are found
    Do While FileName <> ""
        Debug.Print FileName
        FileName = Dir()
    Loop
End Sub

In the first code, once the myDoc object is set, you can use standard MSWord object model methods and properties as illustrated to work with it. Some other methods can be seen in these links.

'TypeText method
'http://www.excelforum.com/excel-programming/650672-populate-word-document-from-excel.html#post1946784
'http://www.excelforum.com/showthread.php?p=1946784
'http://vbaexpress.com/forum/showthread.php?p=169877
'http://vbaexpress.com/forum/showthread.php?t=24693

'Copy from Excel, paste to Word
'Lucas, http://vbaexpress.com/forum/showthread.php?p=178364

'FormFields
'http://www.mrexcel.com/forum/showthread.php?p=1639696
'http://www.mrexcel.com/forum/showthread.php?t=333200

'Add Hyperlink to Bookmark
'http://www.excelforum.com/excel-programming/664078-use-excel-vba-to-add-a-hyperlink-to-a-word-document.html#post2006430
'Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054

'Save OLEObject as MSWord Document
'http://vbaexpress.com/forum/showthread.php?t=21619

'Add Table to MSWord
'http://vbaexpress.com/forum/showthread.php?t=23975
'http://vbaexpress.com/forum/showthread.php?p=168731

'Import Word Tables
'vog, http://www.mrexcel.com/forum/showthread.php?t=382541

'Save OLEObject as MSWord DOC
'http://vbaexpress.com/forum/showthread.php?t=21619

'Get Optionbutton info from MSWord DOC
'http://vbaexpress.com/forum/showthread.php?t=22454

'FindReplace Text
'http://www.excelforum.com/excel-programming/682014-replace-word-in-ms-word-with-varable-from-ms-excel.html

'Bookmarks
'http://vbaexpress.com/forum/showthread.php?p=185718
 
Upvote 0
Thank you, I will look this over tonight.

I wasn't aware it had been answered so recently, I'm probably going to have to start regularly coming to this board, its just a great resource.
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

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