PDA

View Full Version : Wordmerge



RonNCmale
05-06-2010, 04:05 AM
I'm using this wordmerge program but I need to tweak this just a little. I have attached a word doc. that has a drop down box. I wish this program to merge the info and then protect the document so one can choose from the drop down box. It merges as long as the document is not protected. So I then have to protect the document to choose from the drop down box. Any help would be appreciated.

Note: this is Word merge code made by Albert D. Kallal

Also when you download place "Word" document in the word folder so the program can open it.

RonNCmale
05-07-2010, 01:03 PM
This is the VBA code for the merge command: I need to add Unprotect document; then protect the document into this code.

Private Sub cmdMerge_Click()

Dim strExt As String

strExt = IIf(SysCmd(acSysCmdAccessVer) <= 11, ".doc", ".docx")

' merge the doc
If IsNull(Me.lstFiles) = False Then

' for compat
If Dir(strDirPath & Me.lstFiles & strExt) = "" Then
If strExt = ".docx" Then
strExt = ".doc"
End If
End If

Call MergeWord(strDirPath & Me.lstFiles & strExt, strDirPath, strOutDocName)
strTemplate = Me.lstFiles
DoCmd.Close acForm, Me.Name

Else

MsgBox "You need to select a Word document", vbExclamation, "Word Merge"
strTemplate = ""

End If

End Sub

CreganTur
05-12-2010, 05:16 AM
What's the code for the MergeWord sub?

RonNCmale
05-12-2010, 08:16 AM
Function MergeWord(strDocName As String, _
strDataDir As String, _
Optional strOutDocName As String, _
Optional bolPrint As Boolean = False, _
Optional StrPrinter As String)

' This code takes a word document that has been setup as a MERGE document.
' This merge document is opened, then mailmerge is executed. The original
' document is then closed. The result is a raw word document with no connectons
' to the merge.txt (a csv source data file).

'Parms:
' strDocName - full path name of word doc (.doc)
' strDataDir - dir (full path) where docuemnts are placed
' strOutDocName - full path name of merged document (saved).
' bolPrint - if true, then output docuemnt is printed - if strOutDocName is suppled then we close the docuemnt
' strPrinter - sends output to the printer name
'
'
' The above parms are suppled by other routines. You likey should not need to call this
' routine directly. See the sub called MergeNoPrompts.

' Albert D. Kallal (c) 2001
' kalla@msn.com
'
Dim wordApp As Object ' running instance of word
Dim WordDoc As Object ' one instance of a word doc
Dim WordDocM As Object ' one instance of a word doc
Dim strActiveDoc As String ' doc name (no path)
Dim lngWordDest As Long ' const for dest, 0 = new doc, 1 = printer
Dim MyPbar As New clsRidesPBar ' create a instance of our Progress bar.


MyPbar.ShowProgress
MyPbar.TextMsg = "Launching Word...please wait..."
MyPbar.Pmax = 4 ' 4 steps to inc
MyPbar.IncOne ' step 1....start!

On Error GoTo CreateWordApp

If bolPrint = True Then
' we printing a document, so lets launch a seperate copy of word
' as not interfere with the the user's current open copy of word

Set wordApp = GetObject("Word.Application")
Else
Set wordApp = GetObject(, "Word.Application")
End If

On Error Resume Next

MyPbar.IncOne ' step 2, word is loaded.

Set WordDoc = wordApp.Documents.Open(strDocName)

MyPbar.IncOne ' step 3, doc is loaded

strActiveDoc = wordApp.ActiveDocument.Name
'wordApp.Activate

If bolPrint = False Then
wordApp.Visible = True
wordApp.Activate
wordApp.WindowState = 0 'wdWindowStateRestore
End If

WordDoc.MailMerge.OpenDataSource _
Name:=strMergeDataFile, _
ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=0, _
Connection:="", SQLStatement:="", SQLStatement1:=""


With WordDoc.MailMerge
.Destination = 0 ' 0 = new doc
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .datasource
.FirstRecord = 1
' .LastRecord = 1
End With
.Execute Pause:=False
End With
Set WordDocM = wordApp.ActiveDocument

MyPbar.IncOne ' step 4, doc is merged
WordDoc.Close (False)

If bolPrint = False Then
wordApp.Visible = True
End If

If strOutDocName <> "" Then
'wordApp.ActiveDocument.SaveAs strOutDocName
WordDocM.SaveAs strOutDocName
End If

If bolPrint = False Then

WordDocM.Activate

Else

' print this document

If StrPrinter <> "" Then
With wordApp.Dialogs(97) ' 97 - wdDialogFilePrintSetup
.Printer = StrPrinter
.DoNotSetAsSysDefault = True
.Execute
End With
End If


WordDocM.PrintOut
'If strOutDocName <> "" Then
'wordApp.ActiveDocument.Close (False)
' when we print...we *always* close the docuemnt..

WordDocM.Close (False)

'End If

'wordApp.Visible = True


End If


MyPbar.HideProgress

If bolPrint = True Then
' shutdown word
wordApp.Quit
Else
Set wordApp = Nothing
End If
Set WordDoc = Nothing
Set WordDocM = Nothing
Set MyPbar = Nothing

DoEvents

' If bolShowMerge = True Then
' WordApp.Dialogs(676).Show 'wdDialogMailMerge
' End If

Exit Function

CreateWordApp:
' this code is here to use the EXISTING copy of
' ms-access running. If getobject fails, then
' ms-word was NOT running. The below will then
' launch word
Set wordApp = CreateObject("Word.Application")
Resume Next

End Function

CreganTur
05-12-2010, 01:22 PM
If I'm reading that code right you'll need to add something like:
UnProtect:
If WordDoc.ProtectionType := wdNoProtection Then
WordDoc.Unprotect Password:="readonly"
End If

Protect:
WordDoc.Protect Password:="[password]", _
NoReset:=False, Type:=wdAllowOnlyFormFields

RonNCmale
05-12-2010, 03:16 PM
CreganTur: I have no idea where to place the code into the vba code: anyway you can look at the attached file in the first post and give me an idea where to place your code to have the word doc to unprotect, merge the data, then protect the document.
Note: When I say protect the doc i'm referring to restrict formatting and editing and not actually put a password protection in.

RonNCmale
05-14-2010, 02:05 PM
With 106 views, I'm not the only one hoping for an answer to this.

CreganTur
05-17-2010, 07:57 AM
I would suggest unprotecting the document right after WordDoc opens it, and then reprotect just before it is closed. Also, you can assign an empty string for the password. This protection is what keeps users from being able to make changes to the document- the password is just extra security to keep users from making changes unless they know the password.

RonNCmale
05-17-2010, 02:20 PM
Anyone else wanna take a stab at this issue. Note: The original post has the wordmerge access database and a word document "memo" that has a drop down box added to the word document. The document is not protected. Place the word document into the Word folder. click on single merge this record. click memo under select word document and then click, ok-merge to word. It merges with the record source but the drop down box does not work. Anyways, anyone that wants to play around with this and wreck their brains please give it a shot.

Seems like this has been addressed before, but the vba code has changed. Any coders that can enter what was used here to convert to the current VBA code to be able to merge data and then protect document.


The post by DoctorV3774 stated he had been shown Albert Kallal's Mail merge Code which has worked great for taking a single record's worth of data from Access and Merges the fields it into a Word doc. I even was able to get the (document protected after the merge so dropdown fields work as do checkbox form fields.) <------ This is what I'm trying to accomplish.

The Post can be found here:
http://www.utteraccess.com/forum/Mail-Merg...e-t1119678.html (http://www.utteraccess.com/forum/Mail-Merge-Access-prote-t1119678.html)
dated: Feb 27 2006, 02:30 PM

RonNCmale
05-18-2010, 02:41 PM
CreganTur
I don't know how to write vba code. Anyway you can write the code and place what you are suggesting into the original code by Albert Kallal