none
Word 2007 .activedocument.saved RRS feed

  • Question

  • Hi,

    I have a problem with a migration to Word 2007. The application started off life in 1997 as a VBA macro in a template in Word 97. Since then it has progressively moved through being a VSTO addin in Word 2003 and then as a stand alone application that has Word embedded inside it. Word is being displayed inside a window in a form with Word's parent being the application window. All this works very well with Word 2003. It was done this way as we could see that Word 2003 was going to be unsupported in the future and we found that the change to ribbons in Word 2007 was not suitable for our needs. The application has about 300 users who create about 3000 documents a day and can create as many as 500 documents an hour on times.

    The time has now come to make the transition from Word 2003 to 2007. I have replaced the Office 11 interop with Office 12 interop and have now been able to generate docx files that don't make Word run in compatibility mode and the application does what it is supposed to do with regard the workflow that the application manages. Documents are created on top of templates that have been around for a very long time and are now in .dotx fomat having been converted from .dot files.  However I have 2 small problems.

    1. For some reason when I open a saved file or I create a new document even though the last thing I do to the document in code is to tell it that it has been saved (.activedocument.saved = true) Word is ignoring this instruction and won't allow me to update this flag. I can step through the code and try changing the flag in the immediate window (VS 2008, VB) but this has no effect. the flag does not change but there is no error generated. This may not sound like a big deal but this causes a "do you want to continue and lose changes" dialog box to come up if the user has not edited the document and wishes to close it. This is annoying and needs to be fixed or got around.

    2. The documents have a watermark to show which stage in the workflow they are at. This watermark is created when the document is first created and is updated as the document progresses through the workflow. In Word 2003 this works. In 2007 it will not update. I have stepped through the code and it all appears to be working (no errors). The code finds the existing watermark and removes it and replaces it with the desired text. Apart from identifying where the document is in the workflow it is used to identify printed documents that have not yet been signed off as complete. Once the document is complete the watermark is removed, the document is printed and archived by converting the Word document into an MHT file so that it can be displayed in another application that is web based. 

    Sorry for the long explanation. I hope that somebody can help with this problem.

    Mike


    • Edited by Welsh Nomad Wednesday, September 11, 2013 11:21 AM corrected some spelling errors
    Wednesday, September 11, 2013 7:52 AM

Answers

  • Thanks for that Cindy.

    Here is how we get the handle. After creating the word object by calling Activator.createInstance(Type.GetTypeFromProgID("Word.Application"))

    we run the following

                        Dim myProcess() As Process
                        myProcess = Process.GetProcessesByName("Winword")
                        If myProcess.Count > 0 Then
                            For Each singleProcess As Process In myProcess
                                If CInt(singleProcess.MainWindowHandle) > 0 Then
                                    SetParent(singleProcess.MainWindowHandle, _pnlWordHandle)
                                    _WordHandle = singleProcess.MainWindowHandle
                                    Exit For
                                End If
                            Next
                        End If
    Probably not the best piece of code that you have ever seen but it has worked for us and still does work albeit with a few wrinkles now with Word 2007 and interop 12.

    Thursday, September 12, 2013 10:13 AM

All replies

  • Just to confuse matters if the user closes the active document using the x in the top right hand side of the screen I can trap the DocumentBeforeClose event generated by Word and I can set the activeDocument.saved property to true and close the document without the dialog box. Of course if the user has made any edits then these would be lost as the user would not be given the opportunity to save the document correctly.
    Wednesday, September 11, 2013 11:19 AM
  • It's not clear whether you still have Word embedded in some other kind of application? That can affect how Word works, especially since version 2007, at which time Microsoft explicitly no longer supports "embedding" Word in other software.

    If this still is running "embedded" your first step would be to test whether the behavior persists in Word running as a standalone desktop application.

    It's really not clear what the problem is in point (2). You mention "will not update" but the rest of the text doesn't describe in what manner the watermark won't update - it makes it sound as if it is working.

    Assuming the problems persist in the application outside an embedded environment, I'd suspect the files, if they're as old as you describe. Even if you've saved them in the new file format, the documents could have accumulated a lot of "gunk" that is preventing them from performing as expected. So the next test would be whether you can repro the problems in new documents, created in Word 2007 from a new Normal.dotm template generated by Word 2007.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, September 11, 2013 12:35 PM
    Moderator
  • Hi Cindy,

    Thanks for the reply. I have managed to work out a fix for part 2. Basically what I had to do was after removing the existing watermark I had to save the document. After that it was a case of putting in the new watermark and saving again.

    Word is being controlled by another application. If I start Word up as a standalone it does not exhibit this behaviour. Our application creates an instance of Word and captures the mainWindowHandle. It then uses the SetParent API to tell Word that it has a new parent. This does still work and as I said earlier all the rest of the functionality is still there. The only thing that is not working is that when the application asks Word to open an existing document Word insists that the document needs saving even though nothing has been edited. The only reason I need this to work is to prevent the users being unnecessarily bothered with dilaog boxes that are not necessary.

    The template files contain a load of document variables which have not changed very much over the years. There are no macros in them. The Normal.dotm on my development machine is new.

    Wednesday, September 11, 2013 2:12 PM
  • Right, then Word is having problems resolving where it's running.

    I'm not too conversant with Windows API, but I was doing a bit of research a couple of years ago and stumbled across the fact that Word actually has two Window handles for a document window: the application handle and the document handle. It makes a difference which you use. You might investigate that and try the other handle...

    Below is some sample code from the notes I made at the time. The hwnd3 variable is the "good" one.

        Dim applicationCaption As String
        Dim wordClassName As String
        Dim hWnd1 As Long, hWnd2 As Long, hWnd3 As Long
        applicationCaption = Application.Caption ' "Microsoft Word"
        wordClassName = "OpusApp"
        hWnd1 = FindWindow(vbNullString, applicationCaption)
        hWnd2 = FindWindow(wordClassName, applicationCaption)
        hWnd3 = FindWindow(wordClassName, vbNullString)
        Debug.Print hWnd1, hWnd2, hWnd3
    


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, September 12, 2013 9:36 AM
    Moderator
  • Thanks for that Cindy.

    Here is how we get the handle. After creating the word object by calling Activator.createInstance(Type.GetTypeFromProgID("Word.Application"))

    we run the following

                        Dim myProcess() As Process
                        myProcess = Process.GetProcessesByName("Winword")
                        If myProcess.Count > 0 Then
                            For Each singleProcess As Process In myProcess
                                If CInt(singleProcess.MainWindowHandle) > 0 Then
                                    SetParent(singleProcess.MainWindowHandle, _pnlWordHandle)
                                    _WordHandle = singleProcess.MainWindowHandle
                                    Exit For
                                End If
                            Next
                        End If
    Probably not the best piece of code that you have ever seen but it has worked for us and still does work albeit with a few wrinkles now with Word 2007 and interop 12.

    Thursday, September 12, 2013 10:13 AM