Translate

Sunday, September 25, 2016

Word - Mail Merge a Document to Separate Files

I was asked to help mail merge a document and save the result to a file name based using values from the some merged fields. I found some code on Stack Overflow by Søren Francis and modified it to loop.
My steps:

Sub OutDoc()
'
' 1) Merges active record and saves the resulting document named by the datafield
' 2) Closes the resulting document, and advances to the next record in the datasource
'
' Based on code by Søren Francis 6/7-2013 at href="http://stackoverflow.com/questions/12594828/how-to-split-a-mail-merge-and-save-files-with-a-merge-field-as-the-name
   
    Dim DokName  As String   'ADDED CODE
    Dim strFolder As String
    Dim fDone As Boolean
    fDone = False
    ' Set the destination folder
    strFolder = "C:\Projects\ForAdam\FinalDocs\"
 ' Start at the first document
 ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord
  Do
    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
    ' Set the document name
           DokName = .DataFields("ResID").Value & .DataFields("First").Value & "_Contract"
        End With

    ' Merge the active record
        .Execute Pause:=False
    End With

    ' Save then resulting document. NOTICE MODIFIED filename
    ActiveDocument.SaveAs2 FileName:=strFolder + DokName + ".doc", FileFormat:= _
        wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False

    ' Close the resulting document
    ActiveWindow.Close

    ' Now, back in the template document, advance to next record if there is a next record
    If ActiveDocument.MailMerge.DataSource.ActiveRecord <> ActiveDocument.MailMerge.DataSource.RecordCount Then
        ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
    Else ' Flag that we are done
        fDone = True
    End If
  Loop Until fDone 
End Sub

Friday, April 15, 2016

Tomcat - Update SSL certificate

I spent the day updating the server certificate with new certificates from Godaddy to provide a more secure connection to the site. Adding the new cert to IIS was a snap but adding it to Tomcat just wasn't working. I was finally able to get it to work by following the information located here. Basically I just exported the certificate from the certificate store on the IIS server as a pfx file.

My connector setting in the Tomcat server.xml file:

<Connector port="8443" connectionLinger="20000" connectionTimeout="60000" protocol="HTTP/1.1" keystoreType="PKCS12" keystorePass="youwish" keystoreFile="webapps/iis_export.pfx" sslProtocol="TLS" clientAuth="false" disableUploadTimeout="true" enableLookups="true" secure="true" scheme="https" maxThreads="150" SSLEnabled="true"/>

Wednesday, November 4, 2015

MS SQL Error Log Files

Yes, there are backup application log files and they can be very helpful in figuring out the cause of a backup failure. In my case I was getting the "Backup BACKUP failed to complete the command BACKUP DATABASE database_name. Check the backup application log for detailed messages.", "Backup Error: 3041, Severity: 16, State: 1" error like the following:

Checking the log file, which on my server is located in the "SQL\MSSQL12.MSSQLSERVER\MSSQL\Log" folder, I get the following:

Thursday, October 8, 2015

Java - Search for AD Groups and List Group Attributes

Added the ability to search for AD groups and get a Group's Attributes to my LDAP class:

 /**
  * 
  * @param groupName The name of the AD group.
  * @return The AD attributes for the group or null if error.
  * @throws NamingException
  */
    public Attributes getADGroupAttributes(String groupName) throws NamingException {
     this.userName = null;
        String searchFilter = "(&(objectClass=group)(cn=" + groupName + "))";
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration  results = ctx.search("dc=MYDOMAIN,dc=LOCAL", searchFilter, searchControls);
        SearchResult searchResult = null;
        if(results.hasMoreElements()) {
             searchResult = (SearchResult) results.nextElement();
            //make sure there is not another item available, there should be only 1 match
            if(results.hasMoreElements()) {
                this.strRes = "Matched multiple groups for the group name: " + groupName;
                return null;
            }
        }
        else{
            this.strRes = "No groups found";
            return null;         
        }
        return searchResult.getAttributes();  
    } 
 
    /**
     * 
     * @param groupName The group name to search for. Can use wild cards.
     * @return A comma delimited list of the AD group names
     * @throws NamingException
     */
    public String searchforADGroup(String groupName) throws NamingException {
     this.userName = null;
        String searchFilter = "(&(objectClass=group)(cn=" + groupName + "))";
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        NamingEnumeration  results = ctx.search("dc=MYDOMAIN,dc=LOCAL", searchFilter, searchControls);
        SearchResult searchResult = null;
        String strGroups = "";
        if(results!= null) {
         try {
    while(results.hasMore()) {
     searchResult = (SearchResult) results.nextElement();
     strGroups = strGroups + searchResult.getAttributes().get("cn") + ",";
    }
   } catch (Exception e) {

   }
        }
        else{
            this.strRes = "No groups found";
            return null;         
        }
        return strGroups;  
    } 

Wednesday, October 7, 2015

JQUERY - Object doesn't support property or method 'addEventListener' Error

Working on the new site and it works on my laptop for IE but loading from the site in IE it would come up was frozen. Nothing worked. Using the developer tools in IE I saw that I was getting the "Object doesn't support property or method 'addEventListener'" error in jquery. Added the following in the header of the JSP page fixed the problem:

 <meta http-equiv="X-UA-Compatible" content="IE=edge;" />

Thanks To

Tuesday, October 6, 2015

Java - Secure LDAP - simple bind failed: internews.local:636 Error

This took me a while to fix because I had to get the right certificates to install in the Java certificate store on the new server. I finally found the "cer" files on one of the domain controllers, copied the files to the new server, and then used the Java keytool utility to import the certificates into the Java certificate store.
Some tips:
Use "keytool.exe" located the the Java bin folder to import the certificates.
Import the certificates into the "cacerts" file located in the security folder under "jre\lib\security".

Monday, October 5, 2015

Tomcat - CertificateFile must be defined when using SSL with APR error

Setting Tomcat to use SSL and I was getting the "Connector attribute SSLCertificateFile must be defined when using SSL with APR" error. Had to disable APR in server.xml as follows:


  <!--
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  -->