Mailmerge from Excel to premade template in Word
up vote
3
down vote
favorite
I've written the following code to take data from the Excel file "Data" and use it for a mailmerge in a prepare Word template named "Template." I'm new to using VBA in word or Access, so any feedback in these areas is very welcome
Sub AccessTransfer()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
Dim lastRow As Long
lastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).row
Dim rng As Range
Set rng = Sheets("Data").Range("A" & lastRow, "M" & lastRow)
strPath = "L:...MailMergeTEMPDB.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Visible = False
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath, False)
objAccess.Visible = False
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MailMergeTable", strExcelPath, _
True, Range:="Data!")
Access.Application.CloseCurrentDatabase
Dim wdApp As Object, newDoc As Object
Dim strFile As String
strFile = "L:...Template.docm"
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open strFile, ReadOnly:=False
wdApp.Visible = True
wdApp.Run "Project.Module1.WordMailMerge"
Kill "L:....MailMergeTEMPDB.accdb"
End Sub
And then Macro called from within Word is:
Sub WordMailMerge()
Dim strFile As String
strFile = "L:...MailMergeTEMPDB.accdb"
ActiveDocument.MailMerge.OpenDataSource Name:= _
strFile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=strFile;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path=""""" _
, SQLStatement:="SELECT * FROM `MailMergeTable`", SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Windows("AH_MacroMailMergeTemplate").Activate
ActiveDocument.Close
End Sub
I'm also new to using VBA to navigate trough directories, so feedback in this aspect would be appreciated too.
vba excel
add a comment |
up vote
3
down vote
favorite
I've written the following code to take data from the Excel file "Data" and use it for a mailmerge in a prepare Word template named "Template." I'm new to using VBA in word or Access, so any feedback in these areas is very welcome
Sub AccessTransfer()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
Dim lastRow As Long
lastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).row
Dim rng As Range
Set rng = Sheets("Data").Range("A" & lastRow, "M" & lastRow)
strPath = "L:...MailMergeTEMPDB.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Visible = False
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath, False)
objAccess.Visible = False
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MailMergeTable", strExcelPath, _
True, Range:="Data!")
Access.Application.CloseCurrentDatabase
Dim wdApp As Object, newDoc As Object
Dim strFile As String
strFile = "L:...Template.docm"
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open strFile, ReadOnly:=False
wdApp.Visible = True
wdApp.Run "Project.Module1.WordMailMerge"
Kill "L:....MailMergeTEMPDB.accdb"
End Sub
And then Macro called from within Word is:
Sub WordMailMerge()
Dim strFile As String
strFile = "L:...MailMergeTEMPDB.accdb"
ActiveDocument.MailMerge.OpenDataSource Name:= _
strFile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=strFile;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path=""""" _
, SQLStatement:="SELECT * FROM `MailMergeTable`", SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Windows("AH_MacroMailMergeTemplate").Activate
ActiveDocument.Close
End Sub
I'm also new to using VBA to navigate trough directories, so feedback in this aspect would be appreciated too.
vba excel
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
2
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I've written the following code to take data from the Excel file "Data" and use it for a mailmerge in a prepare Word template named "Template." I'm new to using VBA in word or Access, so any feedback in these areas is very welcome
Sub AccessTransfer()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
Dim lastRow As Long
lastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).row
Dim rng As Range
Set rng = Sheets("Data").Range("A" & lastRow, "M" & lastRow)
strPath = "L:...MailMergeTEMPDB.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Visible = False
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath, False)
objAccess.Visible = False
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MailMergeTable", strExcelPath, _
True, Range:="Data!")
Access.Application.CloseCurrentDatabase
Dim wdApp As Object, newDoc As Object
Dim strFile As String
strFile = "L:...Template.docm"
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open strFile, ReadOnly:=False
wdApp.Visible = True
wdApp.Run "Project.Module1.WordMailMerge"
Kill "L:....MailMergeTEMPDB.accdb"
End Sub
And then Macro called from within Word is:
Sub WordMailMerge()
Dim strFile As String
strFile = "L:...MailMergeTEMPDB.accdb"
ActiveDocument.MailMerge.OpenDataSource Name:= _
strFile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=strFile;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path=""""" _
, SQLStatement:="SELECT * FROM `MailMergeTable`", SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Windows("AH_MacroMailMergeTemplate").Activate
ActiveDocument.Close
End Sub
I'm also new to using VBA to navigate trough directories, so feedback in this aspect would be appreciated too.
vba excel
I've written the following code to take data from the Excel file "Data" and use it for a mailmerge in a prepare Word template named "Template." I'm new to using VBA in word or Access, so any feedback in these areas is very welcome
Sub AccessTransfer()
Dim strPath As String
Dim objAccess As Access.Application
Dim strExcelPath As String
Dim lastRow As Long
lastRow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).row
Dim rng As Range
Set rng = Sheets("Data").Range("A" & lastRow, "M" & lastRow)
strPath = "L:...MailMergeTEMPDB.accdb"
Set objAccess = CreateObject("Access.Application")
Call objAccess.NewCurrentDatabase(strPath)
objAccess.Visible = False
strExcelPath = Application.ActiveWorkbook.FullName
Set objAccess = New Access.Application
Call objAccess.OpenCurrentDatabase(strPath, False)
objAccess.Visible = False
Call objAccess.DoCmd.TransferSpreadsheet(acImport, _
acSpreadsheetTypeExcel8, "MailMergeTable", strExcelPath, _
True, Range:="Data!")
Access.Application.CloseCurrentDatabase
Dim wdApp As Object, newDoc As Object
Dim strFile As String
strFile = "L:...Template.docm"
Set wdApp = CreateObject("Word.Application")
wdApp.Documents.Open strFile, ReadOnly:=False
wdApp.Visible = True
wdApp.Run "Project.Module1.WordMailMerge"
Kill "L:....MailMergeTEMPDB.accdb"
End Sub
And then Macro called from within Word is:
Sub WordMailMerge()
Dim strFile As String
strFile = "L:...MailMergeTEMPDB.accdb"
ActiveDocument.MailMerge.OpenDataSource Name:= _
strFile _
, ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
Format:=wdOpenFormatAuto, Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=strFile;Mode=Read;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path=""""" _
, SQLStatement:="SELECT * FROM `MailMergeTable`", SQLStatement1:="", _
SubType:=wdMergeSubTypeAccess
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute Pause:=False
End With
Windows("AH_MacroMailMergeTemplate").Activate
ActiveDocument.Close
End Sub
I'm also new to using VBA to navigate trough directories, so feedback in this aspect would be appreciated too.
vba excel
vba excel
edited Jun 14 '16 at 15:10
asked Jun 14 '16 at 15:00
user1996971
25817
25817
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
2
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55
add a comment |
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
2
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
2
2
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
There's no need to Call
anything, it's obsolete. Instead just use Sub argument, argument
.
What's this
Kill "L:....MailMergeTEMPDB.accdb"
Is this closing the access object? Is this deleting the file? Be explicit - Kill
isn't something you generally want to see in any code.
FSO.Deletefile strPath
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There's no need to Call
anything, it's obsolete. Instead just use Sub argument, argument
.
What's this
Kill "L:....MailMergeTEMPDB.accdb"
Is this closing the access object? Is this deleting the file? Be explicit - Kill
isn't something you generally want to see in any code.
FSO.Deletefile strPath
add a comment |
up vote
0
down vote
There's no need to Call
anything, it's obsolete. Instead just use Sub argument, argument
.
What's this
Kill "L:....MailMergeTEMPDB.accdb"
Is this closing the access object? Is this deleting the file? Be explicit - Kill
isn't something you generally want to see in any code.
FSO.Deletefile strPath
add a comment |
up vote
0
down vote
up vote
0
down vote
There's no need to Call
anything, it's obsolete. Instead just use Sub argument, argument
.
What's this
Kill "L:....MailMergeTEMPDB.accdb"
Is this closing the access object? Is this deleting the file? Be explicit - Kill
isn't something you generally want to see in any code.
FSO.Deletefile strPath
There's no need to Call
anything, it's obsolete. Instead just use Sub argument, argument
.
What's this
Kill "L:....MailMergeTEMPDB.accdb"
Is this closing the access object? Is this deleting the file? Be explicit - Kill
isn't something you generally want to see in any code.
FSO.Deletefile strPath
answered Mar 20 at 9:13
Raystafarian
5,7841047
5,7841047
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f131979%2fmailmerge-from-excel-to-premade-template-in-word%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Apologies. I left out an "End Sub"
– user1996971
Jun 14 '16 at 15:10
2
Why do you transfer data from Access database to Excel? You can create Word MailMerge object directly from that database. See this MSDN documentation
– Maciej Los
Jun 15 '16 at 16:34
Hmm. I did not know that. This changes everything. Thank you.
– user1996971
Jun 16 '16 at 10:55