Mail Merge macro (word 2003,2000)

C

Chris Hoare

Hi all,

Am a wondering .net developer who doesn't know enough VBA
i am afraid. Its a simple problem hopefully

I have been trying to avoid writing vbs into my pages as
the users will need to have their security settings etc.

I have a create a document template which does a client
side mail merge. The docment is stored as an Image in
MSSQL, i open the template via a URL, and it opens in
word / office.

To keep things simple for now i have used the microsoft
method for writing out data for mail merge (from
http://support.microsoft.com/default.aspx?
scid=http://support.microsoft.com:80/support/kb/articles/q
285/1/76.asp&NoWebContent=1)

The document when you upload is flagged (by mime type) as
application/msword not as a template or otherwise. If i
save the document to disk from the link the mail merge
still takes place; only if i open it via url does the
merge not happen.

What the the macro that would force the merge to take
place; or what security settings do i need in word for it
to happen

Thanks

Chris
 
P

Peter Huang [MSFT]

Hi Chris,

Did you mean the Execute method?
Execute Method (Dialog, KeyBinding, and MailMerge Objects)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/ht
ml/output/f1/d4/s5ab85.asp
MailMerge Object
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/ht
ml/output/f1/d4/s5ab85.asp

170988 ACC: How to Create a Word 97 Merge Document Using Automation
http://support.microsoft.com/?id=170988
still takes place; only if i open it via url does the
merge not happen.
The url is a link to the mail merged doc file.
If you did not save the merged mail doc to the disk, then what the url will
link to?
Can you post your code that when the mail merge will not take place and
what you are going to do?

Did I misunderstand your meaning?
If you have any related question, please feel free to let me know.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
 
C

Chris Hoare

I solved it myself over the weekend...


Thanks for your help

Chris

In case anyone ever wants to figure use it for themselves


sub MyClick()
ButtonClick
DoMerge
end sub

Sub CreateDataDoc(oApp)
' Declare variables.
Dim sServer,oDoc,oRS,sTemp,sHead,oRange,oField

' Place your server's name here.
sServer = "chnote"
' Create a new document.
Set oDoc = oApp.Documents.Add
' Create a new recordset.
Set oRS = CreateObject("ADODB.Recordset")
' Open the XML recordset from the server and pass the
SQL statement
' to the Getdata.asp page.
sSQL = "YourSQL"
oRS.Open "http://" & sServer & "/mailmerge/Getdata.asp?
SQL=" & sSql
' Convert the recordset to a string.
sTemp = oRS.GetString(2, -1, vbTab) ' 2 = adClipString

' Append the field names to the front of the string.
For Each oField In oRS.Fields
sHead = sHead & oField.Name & vbTab
Next

' Strip off the last tab.
sTemp = Mid(sHead, 1, Len(sHead) - 1) & vbCrLf & sTemp

' Get a range object and insert the text into the
document.
Set oRange = oDoc.Range
oRange.Text = sTemp

' Convert the text to a table.
oRange.ConvertToTable vbTab
' Save the document to a temp file.
oDoc.SaveAs "C:\data.doc"
' Close the document (no save).
oDoc.Close True
End Sub


Sub ButtonClick()
Dim oApp
Dim oDoc
Dim oMergedDoc

' Create an instance of Word.
Set oApp = CreateObject("Word.Application")

' Create our data file.
CreateDataDoc oApp


' Uncomment these lines to save the merged document
locally.
'oMergedDoc.SaveAs "C:\test.doc"
'oMergedDoc.Close False
'oApp.Quit False

End Sub

Dim objWord
Sub DoMerge()

Set objWord = CreateObject("Word.Application")
'objWord.ActiveDocument.MailMerge.MainDocumentType
= wdFormLetters
objword.Documents.Open("<%
=request.querystring("page")%>")
objWord.ActiveDocument.MailMerge.OpenDataSource
("C:\data.doc")', ConfirmConversions:=False,
ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", WritePasswordDocument:="",
WritePasswordTemplate:="", Revert:=False,
Format:=wdOpenFormatAuto, Connection:="",
SQLStatement:="", SQLStatement1:="",
SubType:=wdMergeSubTypeOther

objWord.ActiveDocument.MailMerge.ViewMailMergeFieldCodes
= wdToggle

objWord.Visible = true
End Sub
 
P

Peter Huang [MSFT]

Hi Chris,

I am glad that you have resolved the problem.
If you have any related question, please feel free to let me know.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top