Tabs, Ribbons, and Menus not responsive after VBA (Word 2007) generation of labels from IBM Notes

StackOverflow https://stackoverflow.com/questions/21089339

Question

I am generating a set of labels using Word 2007, controlled via VBA from IBM Notes.

The document generates fine but after the generation the Tabs, ribbons and Menus are not responsive. I can click in the generated document but clicking on any of the top controls has no effect (icons do not press in, etc). As a workaround I can click on another window, fiddle there, and then the ribbon for that particular document reacts. I am assuming that it has something to do with a focus problem.

After the code is finished, I am setting the Word Application.Visible to True, ScreenUpdating to True. I am calling a 'Close' to close all open files, but still, it's to no avail.

The code is being called by an IBM Notes Database in Lotusscript.

Have you encountered this before? It's very puzzling, and a no-go for my customers.

Andrew

Main Function:

Dim p As New LabelSourceFile

Call p.GenerateFileForSelectedDocuments()
Call p.ExtractWordTemplateFromConfig()

Dim w As New WordExport
w.setSelectedTemplateFullFile(p.FilePathToTemplate)
Print "file path to template: " + p.FilePathToTemplate

Call w.InitializeWordDocument()

Dim finaldoc As Variant
set finaldoc =  w.MailMergeWithThisFile(p.getDataFileStreamFileName())  

Call w.ReplaceCRWithCarriageReturns(finaldoc)
finaldoc.Activate 'gives focus back to document I've just generated

Set finaldoc = Nothing
'p.DeleteVorlage
'Kill p.FilePathToDataFile

Call w.ReleaseToUser() 

This function initializes my document:

Public Function InitializeWordDocument  As Integer
    'Initialize a Word Document Object
    ' If m_strSelectedTemplateFullFile = "" an empty document is created
    InitializeWordDocument = False
    'On Error Goto ErrorHdl
    m_objWordApplication.DisplayAlerts = wdAlertsnone

    If Not m_objWordApplication Is Nothing Then         
        If m_strSelectedTemplateFullFile <> "" Then
            Set m_objWordDoc = m_objWordApplication.Documents.Add( m_strSelectedTemplateFullFile )
            InitializeWordDocument = True
        Else
            Set m_objWordDoc = m_objWordApplication.Documents.Add()
        End If

        Set m_objCurrentRange = m_objWordDoc.Range(0,0)

    End If

    m_objWordApplication.DisplayAlerts = wdAlertsAll
    Exit Function       
ErrorHdl:
    InitializeWordDocument = False

    Exit Function
End Function

This function is actually doing the merging:

'/*************************************************************************************
' * Function MailMergeWithThisFile
' * @param datafilename the data file
' * @return the final merged word document
' * @author Andrew Magerman/Magerman/NotesNet
' * @version Dec 18, 2013
' *************************************************************************************/
Function MailMergeWithThisFile (datafilename As String) As variant
    On Error GoTo ErrorHandler

    'If I don't block the popups, there is an annoying pop-up that appears.
    m_objWordApplication.DisplayAlerts = wdAlertsnone

    With me.m_objWordDoc.MailMerge          
        .MainDocumentType = wdFormLetters
        Call .OpenDataSource(datafilename, wdOpenFormatText, false)  
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = wdDefaultFirstRecord
            .LastRecord = wdDefaultLastRecord
        End With
        .Execute(False)
    End With


    'I need to set this here, immediately after the generation of the destination, merged document because the later
    'actions of closing change the active document.
    Set MailMergeWithThisFile = m_objWordApplication.ActiveDocument

    Call m_objWordDoc.Close(False)

    m_objWordApplication.DisplayAlerts = wdAlertsAll


    Exit Function
ErrorHandler:
    Call logError
    Exit Function
End Function
Was it helpful?

Solution

By brainlessly commenting out lines until I got an answer, here is my workaround:

Always create a new instance (CreateObject) and forget about the GetObject.

If anyone has a good explanation for this, I'd be happy to hear it. For the moment I am just going to use the workaround.

    'Initialize the Word Object
    Set m_objWordApplication = Nothing


    'I create always a new Word instance because that avoids me having focus issues.                
'   Set m_objWordApplication = GetObject("","word.application") 

'   If m_objWordApplication Is Nothing Then
        Set m_objWordApplication = CreateObject("word.application")
'   End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top