Wij hebben een Word sjabloon ontwikkeld, waarbij via VBA gegevens uit het document gebruikt worden voor de naam waaronder het document opgeslagen moet worden. Hierna moet een pop-up scherm verschijnen waarin een wachtwoord ingevoerd kan worden. Het gekke is nu dat na enkele keren gebruik na het opslaan de melding komt "5941 het gevraagde lid van de collectie bestaat niet". Het pop-up scherm voor het wachtwoord verschijnt dan niet meer. Als ik een nieuw account aanmaak in Windows en onder dat account inlog en dan voorgaande actie doe gaat het weer goed totdat na een aantal keren de melding weer verschijnt. Onderstaand de VBA code. Wie weet raad?
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
| Option Explicit Public myPWFlag As String Public myNew As String Sub subMailMergeRecipients() ' ' MailMergeRecipients Macro ' Mail Merge Recipients. ' On Error GoTo fout Dim ffld As FormField Dim mmdf As MailMergeDataField Dim oCC As ContentControl If myPWFlag = "TEMP" Then Else MsgBox "Het document is reeds opgeslagen. U kunt nu niet meer gebruik maken van de gegevensbron" Exit Sub End If If ActiveDocument.MailMerge.DataSource.Name = "" Then MsgBox "Het document heeft geen gegevensbron" Exit Sub End If ActiveDocument.Unprotect Dialogs(wdDialogMailMergeRecipients).Show ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False For Each mmdf In ActiveDocument.MailMerge.DataSource.DataFields For Each oCC In ActiveDocument.ContentControls If mmdf.Name = oCC.Title Then Set oCC = ActiveDocument.SelectContentControlsByTitle(mmdf.Name).Item(1) oCC.Range.Text = mmdf.Value End If Next oCC Next mmdf ActiveDocument.SelectContentControlsByTitle("Ondertekenaar").Item(1).Range.Text = _ ActiveDocument.SelectContentControlsByTitle("Medewerker").Item(1).Range.Text Set oCC = ActiveDocument.SelectContentControlsByTitle("Rapportdatum").Item(1) oCC.Range.Text = Date Set oCC = ActiveDocument.SelectContentControlsByTitle("Branche").Item(1) oCC.Range.Text = "Brand Bedrijven" Set oCC = ActiveDocument.SelectContentControlsByTitle("Re").Item(1) oCC.Range.Text = "re" Set oCC = ActiveDocument.SelectContentControlsByTitle("Re2").Item(1) oCC.Range.Text = "re" For Each oCC In ActiveDocument.ContentControls If oCC.Range.Text = "Klik hier als u tekst wilt invoeren." Then oCC.Range.Text = " " End If Next oCC einde: On Error Resume Next ActiveDocument.Protect Password:="", NoReset:=True, Type:= _ wdAllowOnlyFormFields ActiveDocument.Sections(1).ProtectedForForms = True ActiveDocument.Sections(2).ProtectedForForms = False ActiveDocument.Sections(3).ProtectedForForms = True ActiveDocument.Sections(4).ProtectedForForms = False Exit Sub fout: If Err.Number = 4605 Then MsgBox "Het document heeft geen gegevensbron" Else MsgBox Err.Number & vbCrLf & Err.Description End If Resume einde End Sub Sub subMailMergeOpenDataSource() ' ' MailMergeOpenDataSource Macro ' Opens a data source for mail merge or insert database On Error GoTo fout 'Dim ffld As FormField 'Dim mmdf As MailMergeDataField Dim myFile As String Dim myPassword As String Dim myBron As String ' If myPWFlag = "TEMP" Then ActiveDocument.Unprotect Else MsgBox "Het document is reeds opgeslagen. U kunt niet meer gebruik maken van de gegevensbron" Exit Sub ' myPassword = InputBox("Wachtwoord", "Vul een wachtwoord in") ' ActiveDocument.Unprotect Password:=myPassword End If If myNew = "New" Then 'myBron = "X:\4091_GIT_MS Office Competence Center\Projecten\DLS\DSZ-225-sjablonen_expertiseinspectie\Ontwikkeling\Versie16\Map2.xls" myBron = "C:\synchroniseren\Map1.xls" Call subMerge(myBron) myNew = " " Else myFile = fncBrowseForFile If myFile = "-" Then MsgBox "U heeft niets geselecteerd" GoTo einde End If Call subMerge(myFile) End If einde: On Error Resume Next ActiveDocument.MailMerge.ViewMailMergeFieldCodes = False ActiveDocument.Protect Password:="", NoReset:=True, Type:= _ wdAllowOnlyFormFields ActiveDocument.Sections(1).ProtectedForForms = True ActiveDocument.Sections(2).ProtectedForForms = False ActiveDocument.Sections(3).ProtectedForForms = True ActiveDocument.Sections(4).ProtectedForForms = False Exit Sub fout: If Err.Number = 4605 Then MsgBox "Het document heeft geen gegevensbron" Else MsgBox Err.Number & vbCrLf & Err.Description End If Resume einde End Sub Sub subSave() On Error GoTo fout Dim FileName As String Dim oCC As ContentControl FileName = ActiveDocument.SelectContentControlsByTitle("RisicoAdresBezoekPostCode").Item(1).Range.Text & "_" & _ ActiveDocument.SelectContentControlsByTitle("OrderNr").Item(1).Range.Text & "_" & _ ActiveDocument.SelectContentControlsByTitle("Soort_Code").Item(1).Range.Text With Application.Dialogs(wdDialogFileSaveAs) .Name = FileName If .Show = 0 Then MsgBox "cancel" Exit Sub End If End With 'De volgende coderegel zorgt ervoor dat dit veld na opslaan niet meer gewijzigd kan worden) ActiveDocument.Unprotect ' Set oCC = ActiveDocument.SelectContentControlsByTitle("Auteur").Item(1) ' oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("OrderNr").Item(1) oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("polisnummer").Item(1) oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("OpdrKenmerk").Item(1) oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("opdpersVoorletters").Item(1) oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("opdpersTussenvoegsels").Item(1) oCC.LockContents = True Set oCC = ActiveDocument.SelectContentControlsByTitle("opdpersAchternaam").Item(1) oCC.LockContents = True Selection.EndKey Unit:=wdStory 'focus op een ander veld zetten anders gaat de beveiliging niet goed ActiveDocument.Protect Password:=InputBox("Wachtwoord", "Vul een wachtwoord in"), NoReset:=False, Type:= _ wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False 'Deze code zorgt ervoor dat het document een normaal document wordt. Dus geen 'mail merge'-document ActiveDocument.MailMerge.MainDocumentType = wdNotAMergeDocument 'Deze code zorgt ervoor dat de koppeling met het originele sjabloon opgeheven wordt. 'Indien dit niet gebeurt wordt steeds het sjabloon gestart en krijg je de mededeling over het uitvoeren van de SQl voor de merge 'ActiveDocument.AttachedTemplate = "" ActiveDocument.Close SaveChanges:=wdSaveChanges einde: Exit Sub fout: If Err.Number = 5485 Then MsgBox "Het document is nog met een wachtwoord beveiligd" Else MsgBox Err.Number & vbCrLf & Err.Description End If Resume einde End Sub Sub subBeveiligOpheffen() On Error GoTo fout If myPWFlag = "TEMP" Then MsgBox "Het document moet eerst opgeslagen en afgesloten worden" Else ActiveDocument.Unprotect Password:=InputBox("Wachtwoord", "Vul een wachtwoord in") ActiveDocument.Protect Password:="", NoReset:=True, Type:= _ wdAllowOnlyFormFields ActiveDocument.Sections(1).ProtectedForForms = True ActiveDocument.Sections(2).ProtectedForForms = False ActiveDocument.Sections(3).ProtectedForForms = True ActiveDocument.Sections(4).ProtectedForForms = False End If einde: Exit Sub fout: If Err.Number = 5485 Then MsgBox "wachtwoord is onjuist" Else MsgBox Err.Number & vbCrLf & Err.Description End If Resume einde End Sub Sub subMerge(invoer As String) On Error GoTo fout ' ActiveDocument.MailMerge.MainDocumentType = wdFormLetters ' ActiveDocument.MailMerge.OpenDataSource Name:= _ ' invoer _ ' , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _ ' AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _ ' WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _ ' Format:=wdOpenFormatAuto, Connection:="ESPMijnOrdersMergeNAW", SQLStatement:="", _ ' SQLStatement1:="", SubType:=wdMergeSubTypeOther ' With ActiveDocument.MailMerge ' .MainDocumentType = wdCatalog ' .OpenDataSource Name:="X:\4091_GIT_MS Office Competence Center\Projecten\DLS\DSZ-225-sjablonen_expertiseinspectie\Ontwikkeling\Versie15\Map1.xls", _ ' ReadOnly:=True, _ ' Connection:="ESPMijnOrdersMergeNAW" ' End With 'controle of het bestand bestaat If Dir(invoer) = "" Then MsgBox "Het bronbestand bestaat niet: " & vbCrLf & invoer & vbCrLf & " selecteer het handmatig" Else ActiveDocument.MailMerge.MainDocumentType = wdFormLetters ActiveDocument.MailMerge.OpenDataSource Name:=invoer _ , 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=X:\4091_GIT_MS Office Competence Center\Projecten\DLS\DSZ-225-sjablonen_expertiseinspectie\Ontwikkeling\Versie16\Map1.xls;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database" _ , SQLStatement:= _ "SELECT * FROM `ESPMijnOrdersMergeNAW$ESPMijnOrdersMergeNAW`", _ SQLStatement1:="", SubType:=wdMergeSubTypeAccess End If einde: Exit Sub fout: MsgBox Err.Number Resume einde End Sub Function fncBrowseForFile() As String Dim varItem As Variant fncBrowseForFile = "-" With Application.FileDialog(msoFileDialogFilePicker) 'setup File Dialog .AllowMultiSelect = True .ButtonName = "Select" .InitialView = msoFileDialogViewList .Title = "Select Input Files" 'add filter for excel With .Filters .Clear .Add "Excel Spreadsheets", "*.xls*" End With .FilterIndex = 1 'display file dialog box If .Show Then 'get selected files For Each varItem In .SelectedItems ' If Not (AddListItem(lstInput, False, varItem)) Then ' ErrorMsgBox ("The input file (" & varItem & ") is already selected. Try Again.") ' End If fncBrowseForFile = varItem Next varItem End If End With End Function |
[ Voor 0% gewijzigd door Verwijderd op 10-12-2009 09:47 ]