left-icon

Visual Studio Add-Ins Succinctly®
by Joseph D. Booth

Previous
Chapter

of
A
A
A

CHAPTER 11

Documents

Documents


Each source file being edited is represented in Visual Studio as a document object, which has the necessary properties to access the file content, save it, etc. In this chapter, we will look at how to use the document object.

Getting the document

The document object can be obtained from any source window or by asking Visual Studio for the active document. Any “Document” type window will include a reference to the document object associated with it. The following code sample illustrates a few ways to get the document object.

Document ActiveDoc = _applicationObject.ActiveDocument;

foreach (Document CurrentDoc in _applicationObject.Documents)

{

}

foreach(Window CurWindow in _applicationObject.Windows)

{

   if (CurWindow.Kind == "Document")

   {

       Document CurDoc = CurWindow.Document;

   }

}

Document object

The document object has some basic properties to allow you to determine the file and path name, which windows the document is loaded in, which project item it is associated with, etc.:

  • ActiveWindow: Window the document object is actively displayed in.
  • FullName: Path and file name of document.
  • Language: String containing language, e.g., CSHARP, CSS, XML, VB, etc.
  • Path: Folder document is located in.
  • ProjectItem: Associated project item the document is from.
  • Saved: Has the document been changed since last opened?
  • Selection: Selected text object associated with the document.
  • Windows: All windows the document is displayed in.

With these basic properties, you can navigate from the document to windows or to the project item within the solution. You could back up a copy of the document before you make any changes, etc.

In addition, there are a few methods you can use with the document object to save the file, activate its window, close the document, etc.:

  • Activate(): Move focus to this document.
  • Close(): Close with the option to save.
  • Redo(): Redo the last operation.
  • Save(): Save the document with an optional "Save As" file name.
  • Undo(): Undo the last operation.

You can use the basic properties and methods to perform operations on the document as a whole. In later chapters, we will explore getting the code and text from the document and manipulating it as well.

Text document object

Each document object has an object method which provides access to the text content of the document and allows you to do some basic edit operations in the document. You can get the associated text document with the following code sample.

Document theDoc = _applicationObject.ActiveDocument;

TextDocument theTextDoc = (TextDocument)theDoc.Object("TextDocument");

The text document object has two properties of interest to help editing the text in the document window. The start and end points are objects representing the first and last points in the file. You can create an edit point from either of these objects if you want to do some basic editing. We cover how to edit with edit points in the next chapter. An edit point is the programmatic equivalent of the user’s cursor location while editing. Edit operations take place from the edit point in the document.

The text document has some basic manipulation methods for the document. These include:

  • ClearBookMarks(): Clear any bookmarks from the document’s margin.
  • CreateEditPoint(): Position the “programmatic” cursor for editing.
  • MarkText(): Search for a pattern and mark lines containing the pattern.
  • ReplacePattern(): Search and replace text in the document.

These methods allow you to easily manipulate the text content.

Converting C# to VB

There is a great number of websites that will convert your C# code to VB.NET, but let’s assume we wanted to tackle such a beast ourselves (which is way beyond the scope of this book). The following code sample shows how we could use the MarkText and ReplacePattern methods to get started.

Document ActiveDoc = _applicationObject.ActiveDocument;

TextDocument TextDoc = (TextDocument)ActiveDoc.Object("TextDocument");

if (TextDoc != null)

 {

     TextDoc.MarkText("public void");    // Bookmark all the lines we are going to tweak.

     TextDoc.ReplacePattern("public void", "sub");

  }

In this simple example, we’ve searched for all C# void methods and converted them to sub calls (the VB equivalent). We’ve also marked the line we’ve changed so the user can navigate to the bookmarked lines to review the code changes.

Summary

The document and text document objects can perform some basic file manipulations and global text updates, but in the next two chapters we explore code modification in more detail, including the built-in Visual Studio code-parser class, the code model.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.