Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

26
Nationale Officedag 6 maart 2008 VBA voor Word door Lenny Hoeks

Transcript of Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Page 1: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA voor Worddoor Lenny Hoeks

Page 2: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Onderwerpen

Sjablonen systemen• gegevens bewaren in een ini-file• macro’s in een externe file• beveiliging sjablonen

Sjablonen systemen gekoppeld aan databases• invulvelden invoegen vanuit platte tekst• velden verwijderen, koppeling normal• custom document properties, ranges• naam document instellen

Documenten beheer • macro buttons

Functies in Word overnemen

Standaarlettertype instellen, werken met klassen• autoexec, autoclose• DocumentBeforeClose

Page 3: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Gegevens in Ini file (1)

[AANTAL]aantal=6

[OFFICIEEL]officieel1=mevrouw Van den Bergofficieel2=mevrouw Demmersofficieel3=mevrouw Stuivenberg officieel4=mevrouw Schermerofficieel5=mevrouw Van der Willik

[TELEFOON]telefoon1=5 94telefoon2=6 03telefoon3=5 95telefoon4=6 18telefoon5=5 93

Page 4: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Gegevens in Ini file (2)

Public Const strConsulenten1 As String = "\DetacheringsConsulenten.txt"

Dim strConsulenten As StringDim intI As IntegerDim strKeuze As String

Sub ConsulentenLijst()strKeuze = “officieel"

On Error Resume NextstrConsulenten = ActiveDocument.AttachedTemplate.Path & strConsulenten1ActiveDocument.FormFields(strKeuze).DropDown.ListEntries.Clear

With ActiveDocument.FormFields(strKeuze).DropDown.ListEntries For intI = 1 To System.PrivateProfileString(strConsulenten, "AANTAL", "aantal") .Add Name:=System.PrivateProfileString(strConsulenten, UCase(strKeuze),

LCase(strKeuze) & CStr(intI)) Next intIEnd WithEnd Sub

Page 5: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Gegevens in Ini file (3)

Sub Telefoonlijst()

On Error Resume NextstrConsulenten = ActiveDocument.AttachedTemplate.Path & strConsulenten1

strKeuze = "consulent"

For intI = 1 To System.PrivateProfileString(strConsulenten, "AANTAL", "aantal") If ActiveDocument.FormFields(strKeuze).Result = System.PrivateProfileString(strConsulenten, UCase(strKeuze), LCase(strKeuze) & CStr(intI)) Then Exit ForNext intI

strKeuze = "telefoon"ActiveDocument.FormFields("telefoon").Result = System.PrivateProfileString(strConsulenten, UCase(strKeuze), LCase(strKeuze) & CStr(intI))

ActiveDocument.Fields.UpdateEnd Sub

Page 6: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Beveiliging sjablonen

Public Const PASWOORD As String = “Lynx"

Sub BeveiligenDocument()ActiveDocument.Protect wdAllowOnlyFormFields, True, PASWOORDEnd Sub

Sub BeveiligingErAf()ActiveDocument.Unprotect PASWOORDEnd Sub

Page 7: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Sjablonen met database

Public dbMain As New ADODB.Connection

Public rsTeksten As New ADODB.Recordset

Public SQL As String

Denk hierbij aan de ActiveX verwijzing

welke verwijzing!!!

Page 8: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Sjablonen met database (2)

'Briefgegevens ophalendbMain.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Persist Security Info=False;" & _ "Data Source=" & PLAATS_DATABASE

SQL = "SELECT * FROM Teksten WHERE Briefnummer=" & _CInt(TekstInCell(docGegevens.Tables(2).Cell(2, 2).Range.Text)) & ";"

rsTeksten.Open SQL, dbMain, adOpenStatic, adLockReadOnlystrTekst = rsTeksten("Tekstfragment")strBijlage = rsTeksten("Bijlage")strOndertekening = rsTeksten("Ondertekening")blnAanhef = rsTeksten("Aanhef")

rsTeksten.ClosedbMain.Close

Page 9: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Tekst in Database

<b>Artikel 1>>3,0>>Aanstelling</b>

<n>De aanstelling wordt gewijzigd met ingang van [wijzigingsdatum] .

[PlaatsWerkzaamheden] is de plaats waar de werkzaamheden worden verricht. Werkgever houdt zich het recht voor genoemde plaats te wijzigen.</n>

Page 10: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Invulvelden invoegen vanuit platte tekst (1)Dim strTekst As String

Application.ScreenUpdating = False

InstellingenBehEnGebrui.ZoekinstellingenResetten

'TekstvakkenInvoegenActiveDocument.Bookmarks("BeginDocument").Select

Do While Zoeken("\[*\]", , True) = True strTekst = Mid$(Selection.Text, 2, Len(Selection.Text) - 2) Selection.FormFields.Add Range:=Selection.Range, Type:= _ wdFieldFormTextInput Selection.PreviousField.Select Selection.FormFields(1).TextInput.EditType Type:=wdRegularText, Default:=strTekstLoop

Page 11: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Invulvelden invoegen vanuit platte tekst (2)Function Zoeken(Optional strZoekTekst As String = "", _ Optional strVervangTekst As String = "", _ Optional blnJokers As Boolean = False, _ Optional blnFormat As Boolean = False, _ Optional varWrap As Variant = wdFindStop, _ Optional varReplace As Variant = wdReplaceNone) As Boolean With Selection.Find .Text = strZoekTekst .Replacement.Text = strVervangTekst .Wrap = varWrap .Format = blnFormat .MatchWildcards = blnJokersEnd With

Zoeken = Selection.Find.Execute(Replace:=varReplace)

End Function

Page 12: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Invulvelden invoegen vanuit platte tekst (3)Sub ZoekinstellingenResetten()Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormatting

With Selection.Find .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceNoneEnd Sub

Page 13: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Velden verwijderen

Sub BeveiligingErafEnOntkoppelen()

On Error GoTo ErrHandActiveDocument.Sections(3).Range.Fields.UnlinkActiveDocument.UnprotectActiveDocument.AttachedTemplate = Normal

Exit Sub

ErrHand:MsgBox Err.Number & vbNewLine & _ Err.Description, , TITLE

End Sub

Page 14: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Custom Document Properties

Sub CDPsMaken(strNaamDocument1 As String, strNaamCDP As String, varWaardeCDP As Variant, _Optional varTypeCDP As Variant = msoPropertyTypeString)

Documents(strNaamDocument1).CustomDocumentProperties.Add _ Name:=strNaamCDP, LinkToContent:=False, Value:=varWaardeCDP, _ Type:=varTypeCDPEnd Sub

CDPsMaken DocumentNaam, "Datum", "1-1-2000", msoPropertyTypeDateDocuments(DocumentNaam).CustomDocumentProperties("Datum") = Date

CDPsMaken DocumentNaam, "Kenmerk", strKenmerkAchternaamDocuments(DocumentNaam).CustomDocumentProperties("Kenmerk") = rsTeksten("Omschrijving")

Page 15: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Ranges

Dim rngDocument As Range

Set rngDocument = objDoc.ContentrngDocument.SetRange Start:=objDoc.Tables(objDoc.Tables.Count).Range.End, _ End:=rngDocument.End

objSjabloon.Paragraphs(objSjabloon.Paragraphs.Count).Range.InsertAfter _ vbNewLine & vbNewLine & Trim$(rngDocument.Text)

Page 16: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Documentnaam voorstellen

Als een document nog niet is opgeslagen, kun je eensuggestie geven voor de file naam in Bestand, Eigenschappen.

ActiveDocument.BuiltInDocumentProperties(wdPropertyTitle) = “DocNaam"

With Dialogs(wdDialogFileSummaryInfo)    .Title = “Titel Document"    .ExecuteEnd With

‘om het pad te bepalen

With Dialogs(wdDialogFileSaveAs)     .Name = "c:\windows\temp\"     .Show End With

Page 17: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Documentbeheer

With Application.FileSearch .NewSearch .LookIn = strMapnaam .SearchSubFolders = False .FileName = "*.doc" If .Execute > 0 Then For intI = 1 To .FoundFiles.Count msgbox .FoundFiles(intI) Next intI Else MsgBox "Geen documenten gevonden." End If

End with

Page 18: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Waarschuwingen uitzetten

With Application

.DisplayAlerts = wdAlertsNone

‘code

.DisplayAlerts = wdAlertsAll

End With

Page 19: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Wordopdrachten ondervangen

Sub FileClose()

MsgBox "wordt afgesloten"

End Sub

Page 20: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Pad vastleggen

Sub FileSave()

Dim UserSaveDialog As Dialog Set UserSaveDialog = Dialogs(wdDialogFileSaveAs)

‘als het document al ooit is opgeslagen, wordt het opgeslagen If ActiveDocument.Path <> "" Then     ActiveDocument.Save     Exit Sub End If

With UserSaveDialog     .Name = "C:\My Documents"     If .Display Then        If LCase$(Left$(CurDir, 15)) <> "c:\my documents" Then            MsgBox “U kunt het document hier niet opslaan.“, vbCritical, TITEL            Exit Sub        End If        ‘opslaan van het document        UserSaveDialog.Execute    End IfEnd With

End Sub

Page 21: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Klasse declareren

In Klasse Module: (GebeurtenisKlasse2)

Public WithEvents appWord as Word.Application

Page 22: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Event declareren

In een module:

Dim X As New GebeurtenisKlasse2

Sub Register_Event_Handler() Set X.App = Word.ApplicationEnd Sub

Sub Autoexec() Register_Event_HandlerEnd Sub

Page 23: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

DocumentBeforeClose

Private Sub App_DocumentBeforeClose(ByVal Doc As _

Document, Cancel As Boolean)

Select Case MsgBox("Het document " & Doc.Name & " wordt gesloten" & vbNewLine & "wilt u dat? ", vbYesNo, “VBA Word")

Case Is = vbNo

Cancel = True

Case Is = vbYes

ActiveDocument.Saved = True

‘SendKeys "N"

End Select

End Sub

Page 24: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

Lettertype instellen

Private Sub App_NewDocument(ByVal Doc As Document)

Private Sub App_DocumentChange()

ActiveDocument.Styles(wdStyleNormal).Font.Name = "Comic Sans MS"

End Sub

Page 25: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

AutoClose

Public Sub AutoClose()

‘geef aan dat het document nog opgeslagen dient te worden ‘er verschijnt dan een venster om het document op te slaan

   ActiveDocument.Saved = False

   ‘het document wordt niet opgeslagen of gesloten als er op annuleren wordt gedrukt...   SendKeys "{ESC}"

End Sub

Page 26: Nationale Officedag6 maart 2008 VBA voor Word door Lenny Hoeks.

Nationale Officedag

6 maart 2008

VBA Word

De Rooie Zebra

Red Zebra denkt met u mee als het gaat over het vereenvoudigen en stroomlijnen van uw administratieve handelingen.

Red Zebra realiseert dit middels de MS Office omgeving. Dat varieert van het professioneel opmaken van Word documenten en PowerPoint presentaties, tot het maken van sjablonen en huisstijl systemen in Word, bedrijfsspecifieke tools in Excel, documentbeheersing middels VBA.

Het maken van databases en de koppeling hier naartoe kan ook gerealiseerd worden.

Tevens verzorgt Red Zebra onderwijs in bovengenoemde disciplines.