Sunday, August 16, 2026 : Word That Has Not Changed
© 安岡喜晴 (JoyShine)

I was going through old data on an external hard drive, and found source code I wrote more than twenty years ago.

Back then, a subsidiary in the Fujitsu group asked me to build something that keeps saving a Word document in the background while it is being written.

    Public Sub WordRealTimeSaver()

        Dim W1 As Word.Application
        Dim W2 As Word.Document
        Dim sx, sy As String

        Dim SaveDirectory As String = GetAppPath() & "\Save"

        If System.IO.Directory.Exists(SaveDirectory) = False Then
            System.IO.Directory.CreateDirectory(SaveDirectory)
        End If

        Do
            Try

                W1 = GetObject(, "Word.Application")

                sx = W1.ActiveDocument.FullName
                sy = GetAppPath() & "\Save\" & W1.ActiveDocument.Name

                W2 = W1.ActiveDocument
                W2.SaveAs(sy, Word.WdSaveFormat.wdFormatDocument)

            Catch ex As Exception

            End Try
        Loop


    End Sub

SaveAs changes the file the open document is attached to.
After the first pass, the document being edited is the copy in the save folder.
The document's true path is in sx, and no line ever uses it.

The next day, that line is gone.

                W2 = W1.ActiveDocument.Content.Document
                W12 = CreateObject("Word.Application")
                W12.Visible = False
                W12.Documents.Add(W2)
                W12.Documents.Save(True, Word.WdSaveFormat.wdFormatDocument)

It starts a second Word and keeps it invisible.
No one can see the status bar on that one.
Going from Content back to Document returns the original document.
Documents.Add returns a new, empty document.

Three months later, the second Word is gone.

            Do

                y = GetAsyncKeyState(13)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H1)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H2)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H4)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H25)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H26)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H27)

                If y <> 0 Then
                    Exit Do
                End If

                y = GetAsyncKeyState(&H28)

                If y <> 0 Then
                    Exit Do
                End If

                Sleep(100)

            Loop

The Enter key, mouse buttons, and four arrow keys.
If any of them is pressed, the loop ends and the save follows.
Instead of asking Word whether the document changed, it watches the person.
The place could not be hidden, so the moment was chosen instead.

In the end, it was not taken up.
Probably, a progress bar running in the status bar on every save attempt did not feel right.

If it were me now, I would not ask Word anything.

When the client is a company, anything that has to sit inside Word tends to be turned down at review.

The OS keeps an eye on the folder, and when the file changes, the contents at that moment go into a dated copy.
Word is not called, so nothing runs in the status bar.

void WatchAndKeepCopies(const wchar_t* folder, const wchar_t* backupDir)
{
    HANDLE dir = CreateFileW(folder, FILE_LIST_DIRECTORY,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
    if (dir == INVALID_HANDLE_VALUE)
        return;

    alignas(DWORD) BYTE buf[32 * 1024];
    DWORD n = 0;

    while (ReadDirectoryChangesW(dir, buf, sizeof(buf), FALSE,
               FILE_NOTIFY_CHANGE_LAST_WRITE, &n, nullptr, nullptr))
    {
        if (n == 0)
        {
            KeepCopyOfEveryDocument(folder, backupDir);
            continue;
        }

        const BYTE* p = buf;
        for (;;)
        {
            const FILE_NOTIFY_INFORMATION* fni =
                reinterpret_cast<const FILE_NOTIFY_INFORMATION*>(p);

            std::wstring name(fni->FileName,
                              fni->FileNameLength / sizeof(WCHAR));
            KeepCopy(folder, name.c_str(), backupDir);

            if (fni->NextEntryOffset == 0)
                break;
            p += fni->NextEntryOffset;
        }
    }

    CloseHandle(dir);
}

When the notification buffer overflows, everything in it is thrown away and only a count of zero is returned.
When that happens, it is not the moment to give up quietly, and the whole folder is read again.

Excel and PowerPoint each have a method that saves a copy of the open document.
Word does not.

So from outside, the last saved version is as far as it goes.
Twenty years ago and today, the character just typed is only inside Word.

If this had been what I handed over back then, it would probably have been taken up.

Home  |  Profile  |  Software  |  Medicine  |  Diary  |  Photos  |  Contact