Of course. This is an excellent question that gets to the heart of COM and Windows scripting history.
The short answer is: Yes, but Scripting.FileSystemObject is the best and most direct tool for the job. The primary COM "analogue" is ADODB.Stream, but it has significant drawbacks for your specific use case (appending from multiple programs).
Let's break down the options.
The Standard: Scripting.FileSystemObject (FSO)
First, let's confirm why FSO is the standard. It's part of the "Microsoft Scripting Runtime" (scrrun.dll), which has been a core component of Windows for decades. Its OpenTextFile method was designed specifically for this task.
' The standard, correct way
Const ForAppending = 8
Dim fso, txtFile
Set fso = CreateObject("Scripting.FileSystemObject")
' The 'ForAppending' mode is crucial. The OS handles file locking
' to prevent corruption when multiple processes do this.
Set txtFile = fso.OpenTextFile("C:\Logs\MyLog.txt", ForAppending, True)
txtFile.WriteLine "Log entry from Program A."
txtFile.Close
Why it's good for your use case: The ForAppending mode is atomic at the file system level. The OS ensures that if two programs try to write at the same time, one will wait for the other to finish. The writes are serialized, and your file will not get corrupted.
Alternative 1: ADODB.Stream (The Common but Flawed Analogue)
The most well-known COM alternative for file I/O is the ADODB.Stream object. It's part of ActiveX Data Objects (ADO), typically used for database work, but its stream object is a powerful tool for file manipulation.
However, it does not have a true append mode. You have to read the entire file, move to the end, write, and then save the entire file back. This creates a dangerous race condition in your multi-program scenario.
Example of how to use it (and why it's dangerous for you):
' An alternative, but DANGEROUS for multi-process appending
Const adTypeText = 2 ' 2 = text
Const adSaveCreateOverWrite = 2
Dim stream
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeText
stream.Charset = "utf-8" ' Or "ascii", etc.
stream.Open
' DANGER ZONE STARTS HERE
' If the file exists, load it all into memory
If CreateObject("Scripting.FileSystemObject").FileExists("C:\Logs\MyLog.txt") Then
stream.LoadFromFile "C:\Logs\MyLog.txt"
' Move the cursor to the end of the loaded content
stream.Position = stream.Size
End If
' Write the new text to the in-memory stream
stream.WriteText "Log entry from Program B." & vbCrLf
' Save the ENTIRE in-memory stream back to disk, overwriting the file
stream.SaveToFile "C:\Logs\MyLog.txt", adSaveCreateOverWrite
' DANGER ZONE ENDS HERE
stream.Close
The Race Condition Problem:
- Program A opens
MyLog.txt and loads its contents into memory.
- Program B opens
MyLog.txt and loads the same original contents into its memory.
- Program A adds its new line in memory and saves the entire file back to disk.
- Program B adds its new line in memory and saves the entire file back to disk, overwriting the changes made by Program A.
Conclusion: ADODB.Stream is a poor choice for appending to a shared file from multiple processes.
Alternative 2: The "Power User" Solution - A Custom .NET COM Object
If for some reason you cannot use Scripting.FileSystemObject (e.g., it's disabled by policy), the most robust solution is to write your own simple COM object in a modern language like C# and use its superior, built-in file I/O.
This gives you the best of both worlds: a simple COM interface for your legacy apps to call, and a modern, efficient, and safe implementation under the hood.
Step 1: Create the C# Class Library (.NET Framework)
- In Visual Studio, create a new "Class Library (.NET Framework)" project.
- In the project properties, under "Application", click "Assembly Information..." and check "Make assembly COM-Visible".
- Under the "Build" tab, check "Register for COM interop".
Step 2: Write the C# Code
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace FileHelper
{
// Define an interface for COM
[ComVisible(true)]
[Guid("A516A65B-1672-4D46-B42A-75394191563F")] // Generate your own GUID
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IFileAppender
{
void Append(string filePath, string textToAppend);
}
// Implement the interface in a class
[ComVisible(true)]
[Guid("D8B5555C-2A2B-4523-9A92-C05C33E51078")] // Generate your own GUID
[ClassInterface(ClassInterfaceType.None)]
public class FileAppender : IFileAppender
{
public void Append(string filePath, string textToAppend)
{
try
{
// File.AppendAllText is thread-safe and process-safe.
// It handles opening, writing to the end, and closing in one atomic operation.
File.AppendAllText(filePath, textToAppend + Environment.NewLine);
}
catch (Exception ex)
{
// Handle or re-throw exceptions so the COM client is aware of failures.
// For COM, it's often best to throw a COMException.
throw new COMException("Failed to append to file: " + ex.Message, ex);
}
}
}
}
Step 3: Build the Project
Build the project in Visual Studio (as Administrator, if necessary, for registration). It will create the DLL and register it as a COM object on your system.
Step 4: Use it from VBScript/VBA
The calling code is now beautifully simple and safe.
Dim appender
Set appender = CreateObject("FileHelper.FileAppender") ' ProgID is Namespace.ClassName
appender.Append "C:\Logs\MyLog.txt", "Log entry from Program C, using custom COM."
Set appender = Nothing
This method is highly recommended if FSO is not an option. File.AppendAllText is optimized for this exact scenario.
Summary
| Method | How it Works | Concurrency Safety (Your Use Case) | When to Use |
|---|
Scripting.FileSystemObject | Opens a file stream in a true "append" mode. The OS serializes writes. | Excellent. Designed for this. | Always, if possible. This is the standard, correct tool. |
ADODB.Stream | Loads entire file, modifies in memory, saves entire file back. | Very Poor. Prone to race conditions and data loss. | For reading/writing a whole file when you are sure only one process has access. Not for shared appending. |
| Custom .NET COM Object | Wraps modern System.IO.File.AppendAllText() in a COM-visible class. | Excellent. Leverages modern, safe, and efficient file I/O. | When Scripting.FileSystemObject is disabled or you need more advanced functionality not available in FSO. |
Final Recommendation: Stick with Scripting.FileSystemObject. It is the native, built-in, and safest COM solution for your stated goal. Only explore the custom COM object if you have a specific policy or technical constraint preventing the use of FSO.