Avatar billede jvt Nybegynder
09. august 2007 - 12:01 Der er 9 kommentarer og
1 løsning

manglende www og email links ved oprettelse af signatur i Word

Jeg har lavet et script, som indsamler oplysninger fra AD og sammensætter dem til en signatur med den rette formatering m.m.

Eneste problem jeg har er, at word ikke automatisk formatere www adressen og email adressen i signaturen til links.

============ Det mest relevante af koden ==========

'**************************************
'* Setting up MS Office Word          *
'**************************************
Set oWord = CreateObject("Word.Application")
oWord.Visible = true
Set oDoc = oWord.Documents.Add()
Set oSelection = oWord.Selection
oSelection.WholeStory
oSelection.Delete


'**************************************
'* Adding info to signature          *
'**************************************
oSelection.Font.Name = "Verdana"
oSelection.Font.Size = "10"
oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )

oSelection.TypeText Chr(11) & Chr(11)

oSelection.Font.Size = "8"

oSelection.TypeText sName & Chr(11)
oSelection.TypeText sTitle & Chr(11) & Chr(11)

oSelection.Font.Color = RGB(iFC_CompanyNameRed, iFC_CompanyNameGreen, iFC_CompanyNameBlue)
oSelection.Font.Bold = True

oSelection.TypeText sCompany

oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )
oSelection.Font.Bold = False

oSelection.TypeText Chr(11)
oSelection.TypeText sAddress & Chr(11)
oSelection.TypeText sZipCode & Chr(11)
oSelection.TypeText sCountry & Chr(11)

oSelection.TypeText "T:  " & sCompTlf& Chr(11)
oSelection.TypeText "F:  " & sCompFax & Chr(11)
oSelection.TypeText "M: "  & sMobile & Chr(11)
oSelection.TypeText sEmail & Chr(11)
oSelection.TypeText sCompWeb

'**************************************
'* Creating new signature          *
'**************************************
Set oEmailOptions = oWord.EmailOptions
Set oEmailSignature = oEmailOptions.EmailSignature
Set oEmailSignatureEntries = oEmailSignature.EmailSignatureEntries

Set oSelection = oDoc.Range()

oEmailSignatureEntries.Add "AD Signature", oSelection
oSignatureObject.NewMessageSignature = "AD Signature"
oSignatureObject.ReplyMessageSignature = "AD Signature"


'**************************************
'* Cleaning up                        *
'**************************************
oDoc.Saved = True
oWord.Quit

============= Relevant kode slut =====================

Når signaturen så vises i outlook, er www adressen og email adressen ren tekst, ikke links :-(
Avatar billede morten_leth Nybegynder
09. august 2007 - 14:08 #1
oSelection.Hyperlinks.Add oSelection.range, "http://" & "test", ,"Klik her for at besøge vores site" , "test"

dette her laver et hylerlink...


oSelection.Hyperlinks.Add oSelection.range, "mailto:" & strEmail, ,"Klik her for at skrive til mig" , strEmail

dette her laver et mail link... :D
Avatar billede jvt Nybegynder
09. august 2007 - 15:12 #2
mange tak for hjælpen.

Nu har jeg bare fået et nyt problem:

formateringen af hyperlinkene skal ændres så den ligner resten af teksten (verdana, størrelse 8, sort).

Det definere jeg jo højere oppe i koden, men det slår ikke igennem når word automatisk formatere hyperlinkene...
Avatar billede morten_leth Nybegynder
09. august 2007 - 15:26 #3
oSelection.Hyperlinks.Add oSelection.range, "mailto:" & "test", ,"Klik her for at skrive til mig" , "test"
oSelection.MoveLeft 3, 1, 2
oSelection.Font.Name = "Verdana"
oSelection.Font.Size = "10"
oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )
oSelection.EndKey

oSelection.Hyperlinks.Add oSelection.range, "http://" & "test", ,"Klik her for at besøge vores site" , "test"
oSelection.MoveLeft 3, 1, 2
oSelection.Font.Name = "Verdana"
oSelection.Font.Size = "10"
oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )
oSelection.EndKey

nu skal det jo så siges de gør det ikke nemt...

oSelection.MoveLeft 3, 1, 2
gør at den markere selectionen (mener jeg) og end key gør den afmarkere det igen, det er mig bekendt den eneste måde at komme udenom den lille hurdle på.. :D
Avatar billede morten_leth Nybegynder
09. august 2007 - 15:28 #4
oSelection.TypeParagraph()

btw så laver denne her en ekstra linje, kunne godt være du havde lyst til at bruge det.
Avatar billede jvt Nybegynder
09. august 2007 - 16:32 #5
Så lykkedes det at få mit script til at fungere efter hensigten. Jeg havde tidligere rodet med oSelection.MoveLeft, dog uden held.
Da du kun har lavet kommentare kan jeg ikke give dig point... Kan du ikke lave et svar?
Avatar billede morten_leth Nybegynder
09. august 2007 - 20:37 #6
He he jow det kan jeg da.. :D jeg laver som regel ikke svar før end det er nødvendigt.. deraf grunden til jeg ikke har gjort det endnu.. :D

Håber det spiller max for dig...

Hygge /Leth
Avatar billede bpraem Nybegynder
29. februar 2008 - 10:29 #7
Hej Jvt, Er det ikke et script du vil dele ? Det er lige præcis hvad jeg godt kunne bruge..
Avatar billede morten_leth Nybegynder
29. februar 2008 - 10:43 #8
Du kan tage det som han har deroppe og kopiere det, derudover så kan du smide det ind som jeg har lavet.. så tror jeg du er godt kørende.. :D
Avatar billede jvt Nybegynder
29. februar 2008 - 12:01 #9
Jeg kan godt dele mit script. Jeg har fået det til at virke for længe siden (tak morten).

Der er dog tilføjet en hel del, bla en log. En mere erfaren scripter vil nok kunne optimere min kode en hel del, men sådan er det jo altid. Der er så også en hulens masse referencer der skal ændres for at du kan benytte dig af det. God fornøjelse:

'==================================================================
'========================== Declarations ==========================
'==================================================================
Const sRM_NormalDot = "\\HERA\Documents\Marketing\02 Corporate Identity\04 Templates\01 Word\00 Released\Normal.dotm"
Const sNormalDotmFolder = "\Microsoft\Templates\"
Const sCompany = "Resultmaker A/S"
Const sAddress = "Vester Farimagsgade 3"
Const sZipCode = "1606 Copenhagen"
Const sCountry = "Denmark"
Const sCompTlf = "+45 70 20 10 21"
Const sCompFax = "+45 70 20 08 21"
Const sCompWeb = "www.resultmaker.com"
Const iFC_CompanyNameRed = 169
Const iFC_CompanyNameGreen = 188
Const iFC_CompanyNameBlue = 192
Const iFC_TextRed = 0
Const iFC_TextGreen = 0
Const iFC_TextBlue = 0
Const sLogFile = "AutoSignature.log"
Const sLogFolder = "c:\Logon_Script_logs"
Const wdLineSpaceSingle = 0
Const ForAppending = 8

Dim sAppPath, sResult, sPCRole, sWordVersion, sUserName, sName, sTitle, sDepartment, sMobile, sEmail, sLog, sFile
Dim oFSO, oShell, oWord, oSelection, oLogFile, oSysInfo, oUser, oEmailOptions, oEmailSignature, oEmailSignatureEntries
Dim bWarnings
Dim i


'******************
'** SCRIPT START **
'******************
bWarnings = False
'logfolder creation and logfile deletion
Set oFSO = CreateObject("Scripting.FileSystemObject")
sLog = sLogFolder & "\" & sLogFile

If oFSO.FolderExists(sLogFolder) = False Then
    oFSO.CreateFolder sLogFolder
End If
If oFSO.FileExists(sLog) Then
    oFSO.DeleteFile sLog
End If


ReportProgress "======================================"
ReportProgress "= Script started " & Date() & " " & time() & " ="
ReportProgress "======================================" & vbCrLf

CheckForRequirements
CollectUserInfo
WriteSignatures

Cleanup
'****************
'** SCRIPT END **
'****************

'===============================================================
'========================== Functions ==========================
'===============================================================

Function CheckForRequirements
    ReportProgress "*******************************"
    ReportProgress "** Checking for requirements **"
    ReportProgress "*******************************"
    sPCRole = CheckComputerRole
'PCROLE
    If sPCRole <> "Member Workstation" Then
        ReportProgress "FAILURE - The computer is not a Workstation, but a " & sPCRole & " - Quiting script"
        'there's no reason for a message box when the script exits because the user logs on to a server
        WScript.Quit
    Else
        ReportProgress "SUCCESS - The computer is a Workstation"
    End If
    sWordVersion = CheckWordVersion
'WORD VERSION
    If sWordVersion = "" Then
        ReportProgress "FAILURE - Word is not installed. Quiting Autosignatur script"
        EarlyQuit
    Else
        ReportProgress "SUCCESS - Word is installed ( " & sWordVersion & " )"
    End If
'RM_NORMALDOTM
    If oFSO.FileExists(sRM_NormalDot) = 0 Then
        ReportProgress "FAILURE - The Resultmaker version of 'Normal.dotm' is _NOT_ found on the lokationen " & sRM_NormalDot & " - Quiting Autosignatur script"
        EarlyQuit
    Else
        ReportProgress "SUCCESS - The Resultmaker version of 'Normal.dotm' is found on the lokation " & sRM_NormalDot
    End If
'DELETE LOCAL NORMALDOTM
    Set oShell = CreateObject("Wscript.Shell")
    sAppPath = oShell.ExpandEnvironmentStrings("%appdata%")
    If oFSO.FileExists(sAppPath & sNormalDotmFolder & "Normal.dotm") Then
        wscript.sleep 1000
        On Error Resume Next
        Err.Clear
        i = 0
        Do
            oFSO.DeleteFile sAppPath & sNormalDotmFolder & "Normal.dotm"
            If Err.Number = 0 Or i = 10 Then
                Exit Do
            Else
                i = i + 1
                WScript.Sleep 1000
            End If
        Loop
        If Err.Number <> 0 Or i = 10 Then
            ReportProgress "FAILURE - Error happend while trying to delete " & sAppPath& sNormalDotmFolder & "Normal.dotm * " & Err.Description & "* - Quiting Autosignatur script"
            EarlyQuit
        Else
            ReportProgress "SUCCESS - " & sAppPath & sNormalDotmFolder & "Normal.dotm deleted after waiting for " & i & " seconds. Word will now start with a default normal.dotm"
        End If
    Else
        ReportProgress "WARNING - The file " & sAppPath & sNormalDotmFolder & "Normal.dotm not found. The script continues..."
        bWarnings = True
    End If
End Function 'CheckForRequirements


Function CheckComputerRole
    Dim sComputer
    Dim oWMIService, oComputer
    Dim colComputers
   
    sComputer = "."
   
    Set oWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
    Set colComputers = oWMIService.ExecQuery("Select DomainRole from Win32_ComputerSystem")
    For Each oComputer in colComputers
        Select Case oComputer.DomainRole
            Case 0
                CheckComputerRole = "Standalone Workstation"
            Case 1       
                CheckComputerRole = "Member Workstation"
            Case 2
                CheckComputerRole = "Standalone Server"
            Case 3
                CheckComputerRole = "Member Server"
            Case 4
                CheckComputerRole = "Backup Domain Controller"
            Case 5
                CheckComputerRole = "Primary Domain Controller"
        End Select
    Next
    Set oWMIService = Nothing
End Function 'CheckComputerRole


Function CheckWordVersion
    On Error Resume Next
    Err.Clear
    Set oWord = CreateObject("Word.Application")
    On Error Goto 0
    If Err.Number = 0 Then
        Select Case oWord.Version
            Case "9.0"    CheckWordVersion = "Word 2000"
            Case "10.0" CheckWordVersion = "Word XP"
            Case "11.0" CheckWordVersion = "Word 2003"
            Case "12.0" CheckWordVersion = "Word 2007"
            Case Else    CheckWordVersion = "Unknown version (" & oWord.Version & ")"
        End Select
    Else
        CheckWordVersion = ""
        'Word could not be found
    End If
    oWord.Quit
    Set oWord = Nothing
End Function 'CheckWordVersion


Sub CollectUserInfo
    ReportProgress "*********************************"
    ReportProgress "** Collecting User Information **"
    ReportProgress "*********************************"
    Set oSysInfo = CreateObject("ADSystemInfo")
    On Error Resume Next
    Err.Clear
        sUserName = oSysInfo.UserName
        Set oUser = GetObject("LDAP://" & sUserName)
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - User found in AD"
            Err.Clear
        Else
            ReportProgress "FAILURE - Error finding user in AD! * " & Err.Description & " *"
            EarlyQuit
        End If
        sName = oUser.FullName
        sTitle = oUser.Title
        sDepartment = oUser.Department
        sMobile = oUser.Mobile
        sEmail = oUser.mail
        ReportProgress "SUCCESS - Userinformation collected"
End Sub 'CollectUserInfo


Sub WriteSignatures
    ReportProgress "************************"
    ReportProgress "** Writing signatures **"
    ReportProgress "************************"
    On Error Resume Next
        Err.Clear
        'Setting up Word
        Set oWord = CreateObject("Word.Application")
        oWord.Visible = False   
        oWord.Documents.Add
        Set oSelection = oWord.Selection
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Starting Word"
        Else
            ReportProgress "FAILURE - Problem occured when trying to start Word * " & Err.Description & " *"
            EarlyQuit
        End If
   
        'Setting Email options
        Set oEmailOptions = oWord.EmailOptions
        Set oEmailSignature = oEmailOptions.EmailSignature
        Set oEmailSignatureEntries = oEmailSignature.EmailSignatureEntries
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Connecting to Outlook signature part"
        Else
            ReportProgress "FAILURE - Connecting to Outlook signature part * " & Err.Description & " *"
            EarlyQuit
        End If
       
        'Writing the "NEW MAIL" signature in Word
        oSelection.Font.Name = "Verdana"
        oSelection.Font.Size = "10"
        oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )
        oSelection.TypeText Chr(11) & Chr(11) & Chr(11)
       
        oSelection.Font.Size = "8"
       
        oSelection.TypeText sName & Chr(11)
        oSelection.TypeText sTitle
       
        If sDepartment <> "" Then
            oSelection.TypeText ", " & sDepartment
        End If
       
        oSelection.TypeText Chr(11) & Chr(11)
        oSelection.Font.Color = RGB(iFC_CompanyNameRed, iFC_CompanyNameGreen, iFC_CompanyNameBlue)
        oSelection.Font.Bold = True
       
        oSelection.TypeText sCompany
       
        oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue )
        oSelection.Font.Bold = False
       
        oSelection.TypeText Chr(11)
        oSelection.TypeText sAddress & Chr(11)
        oSelection.TypeText sZipCode & Chr(11)
        oSelection.TypeText sCountry & Chr(11)
       
        oSelection.TypeText "T:  " & sCompTlf& Chr(11)
        oSelection.TypeText "F:  " & sCompFax & Chr(11)
       
        If sMobile <> "" Then
            oSelection.TypeText "M: "  & sMobile & Chr(11)
        End If
       
        oSelection.Hyperlinks.Add oSelection.Range, "mailto:" & sEmail,,sEmail, sEmail & Chr(11)
        oSelection.Hyperlinks.Add oSelection.Range, "http://" & sCompWeb,, sCompWeb, sCompWeb
        oSelection.EndKey
       
        oSelection.MoveLeft 2, 2, 2
        oSelection.Font.Name= "Verdana"
        oSelection.Font.Size = "8"
        oSelection.Font.Color = RGB(iFC_TextRed, iFC_TextGreen, iFC_TextBlue)

        Set oSelection = oWord.ActiveDocument.Range
        oSelection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
       
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Writing the signature for a NEW MAIL in Word"
        Else
            ReportProgress "FAILURE - Writing the signature for a NEW MAIL in Word * " & Err.Description & " *"
            EarlyQuit
        End If
               
        'Adding the NEW MAIL signature to available signatures in Outlook
        oEmailSignatureEntries.Add "RM_Signature_new", oSelection
       
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Adding the NEW MAIL signature"
        Else
            ReportProgress "FAILURE - Adding the NEW MAIL signature * " & Err.Description & " *"
            EarlyQuit
        End If
       
        'Writing the "REPLY MAIL" signature in Word
        Set oSelection = oWord.Selection
        oSelection.WholeStory
        oSelection.Delete
        oSelection.Font.Name = "Verdana"
        oSelection.Font.Size = "8"
        oSelection.TypeText Chr(11)
        oSelection.TypeText "/" & sName
        Set oSelection = oWord.ActiveDocument.Range

        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Writing the signature for a REPLY MAIL in Word"
        Else
            ReportProgress "FAILURE - Writing the signature for a REPLY MAIL in Word * " & Err.Description & " *"
            EarlyQuit
        End If
       
        'Adding the REPLY MAIL signature to available signatures in Outlook
        oEmailSignatureEntries.Add "RM_Signature_reply", oSelection
       
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Adding the REPLY MAIL signature"
        Else
            ReportProgress "FAILURE - Adding the REPLY MAIL signature * " & Err.Description & " *"
            EarlyQuit
        End If
   
        'Setting "New mail" and "Reply mail" signatures in Outlook
        oEmailSignature.NewMessageSignature = "RM_Signature_new"
        oEmailSignature.ReplyMessageSignature = "RM_Signature_reply"
       
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - Setting the signatures in Outlook"
        Else
            ReportProgress "FAILURE - Setting the signatures in Outlook * " & Err.Description & " *"
            EarlyQuit
        End If
    On Error Goto 0
    oWord.ActiveDocument.Saved = True
    oWord.Quit
    Set oWord = Nothing
End Sub 'WriteSignatures


Sub ReportProgress(sMessage)
    Set oFile = oFSO.OpenTextFile(sLog, ForAppending, True)
    If sMessage = "" Then
        oFile.WriteLine
    Else
        oFile.WriteLine Time() & " - " & sMessage
    End If
    oFile.Close
End Sub ' ReportProgress


Sub Cleanup
    ReportProgress "*****************"
    ReportProgress "** Cleaning up **"
    ReportProgress "*****************"
    On Error Resume Next
        i = 0
        Do
            Err.Clear
            oFSO.CopyFile sRM_NormalDot, sAppPath & "\Microsoft\Templates\"
            If Err.Number <> 0 Then
                WScript.Sleep 1000
                i = i + 1
            End If
        Loop Until Err.Number = 0 Or i = 10
        If Err.Number = 0 Then
            ReportProgress "SUCCESS - The Resultmaker version of NORMAL.DOTM restored after sleeping for " & i & " seconds"
        End If
        If i = 10 Then
            ReportProgress "FAILURE - The Resultmaker version of NORMAL.DOTM could not be restored. * " & Err.Description & "*"
            EarlyQuit   
        End If
    On Error Goto 0
    If bWarnings = True Then
        ReportProgress ""
        ReportProgress "=============================="
        ReportProgress "= Script ended with Warnings ="
        ReportProgress "=============================="   
    Else
        ReportProgress ""
        ReportProgress "=============================="
        ReportProgress "= Script ended with Success ="
        ReportProgress "=============================="
    End If
    WScript.Quit
End Sub 'Cleanup


Sub EarlyQuit
    MsgBox "See " & sLog & " for more info",vbcritical, "The script 'AutoSignature' ended with an error!"
    On Error Resume Next
        oWord.quit
        Set oWord = Nothing
    On Error Goto 0
    ReportProgress ""
    ReportProgress "================="
    ReportProgress "= Script FAILED ="
    ReportProgress "================="
    WScript.Quit
End Sub 'EarlyQuit
Avatar billede bpraem Nybegynder
29. februar 2008 - 14:00 #10
lækkert ! Jeg takker mange gange !
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester