How do I run a macro automatically without opening Excel?
I'm trying to write a macro to create a table in a word document that has all of my data in an Excel spreadsheet. I have over 200 records so it would be difficult to just copy and paste. I want to automate this process.
I'm running the macro from within VBHere is my code: Sub CreateWordDocTable(). Set wdDoc = wdApp.Documents.Add
Set wdApp = Nothing. Set wdDoc = Nothing. End Sub. You can just call the macro from inside Excel. I would make sure that you make the macro accessible from Word, but not from Excel. I also would suggest calling your macro from a different module, not your current one.
I think you're going to want something like this: Sub Wordmacro(). ' Make sure this is only accessible from Excel. Private Sub WorkbookOpen(). Call Wordmacro. End Sub. ' This is the macro you want to call. Sub Wordmacro(). Dim wdApp As Word.Application Dim wdDoc As Word.Document Dim wdRange As Word.Range Set wdApp = New Word.Application Set wdDoc = wdApp.Add ' Set the range. Set wdRange = wdDoc.Content ' Do whatever you want here. ' Put your data into the range. wdRange.InsertBreak wdDoNotInsertBreaks ' Close the range