Ty Anderson

Working with Word document content objects

Microsoft Word is about the authoring of documents. Documents contain pages, paragraphs, sentences and more. Today, I want to wade into the waters of manipulating Word document content. The plan is to get your feet wet by providing an overview of the key objects along with code samples.

Continuing the fine tradition of all Office applications (OneNote being the exception), Word has an extensive and mature object model. My goal is to show “a way” to accomplish some tasks in Microsoft Word. These samples are not necessarily, “the way” to do it. There is more than one way to achieve the same results.

Today, we are concerned with the structure of a Word document… not its stylings and presentation (I’ll concern ourselves with that topic next).

Document content objects

Let’s look at the objects that combine together to construct a Word document.

  • Document :: The document represents a single, solitary Word document. This is the parent object for all content within a Word document file. It lives in an appropriately named collection called Documents. I covered the document object in a previous article – Word application and base objects.
  • Section :: Documents can contain multiple sections. This is how you apply different formatting, layout, and header/footer options to a Word document… by creating sections and formatting them differently.
  • Page :: Represents a single page within a document. A page can reside within a Range.
  • Paragraph :: A single paragraph of content. A paragraph can reside within a Selection, Document, or Range and is accessible via the Paragraphs collection object. Paragraphs also contain the formatting for content residing above it. Don’t ponder over that last sentence too much, I will explain it further in my next article.
  • Sentence :: This object does not exist… strange as that might seem. Instead, there is the Paragraphs collection object. This collection contains a collection of Range objects, which in turn, contain all the sentences for the Range. Confused? The code samples will help bring focus to the picture.
  • Selection :: This object contains the content selected either by the user or via code. It also is the insertion point if no selection exists. The insertion point is where the cursor blinks in the Word UI to inform the user of their location within the document.
  • Range :: Think of this object like real-estate. It contains a contiguous section of a document. Perhaps its two most popular properties are Start and End. They contain ranges starting and ending character positions within the Word document.

There are more “content” objects than these but the list above are the major objects that provide access to everything else.

Accessing Word document content objects with code

I want to give you a lot of VB.NET code samples today. I’ll start with the Range object and keep going as long as I have paper. By the end of the samples, my hope is you will have a general idea of how to access and edit content within a Word document.

The Range object

The Word object model is extensive and more than a little complex. The best way to figure out how to make things happen via code is to learn to think like Word thinks. The quickest way to achieve this goal is to master the Range and the Selection objects. I covered the selection object in this article so I will focus on the Range object. Just know that together, these two objects allow you to select and find Word content.

Enumerate paragraphs in a range

This method loops through all the paragraphs in the active document and reverses their order in a new document.

Private Sub EnumerateParagraphs()
    Dim curDoc As Word.Document = WordApp.ActiveDocument
    Dim newDoc As Word.Document
    Dim rng As Word.Range
    Dim str As String = ""
    Dim i As Integer
 
    rng = curDoc.Content
    i = rng.Paragraphs.Count()
 
    Do Until i = 0
        str = str & rng.Paragraphs.Item(i).Range.Text & vbCrLf
        i = i - 1
    Loop
 
    newDoc = WordApp.Documents.Add
    newDoc.Range().Text = str
 
    Marshal.ReleaseComObject(rng)
    Marshal.ReleaseComObject(curDoc)
    Marshal.ReleaseComObject(newDoc)

The key is the use of the Range object and all the document content. The code first grabs all the document content and assigns it to a Range variable (rng). Then the code finds out how many paragraphs exist and loops backwards to create a string. This string is then inserted into a new document. It’s a good bar trick that only works in nerd bars.

Select the current page range

In this sample, we use a Range object that’s buried deeper in the Word object model. The reason for this sample is that I want to point out that the Range object is darn near everywhere within Word.

Private Sub SelectedPageRange()
    Dim curDoc As Word.Document
    curDoc = WordApp.ActiveDocument
 
    curDoc.Bookmarks("\page").Range.Select()
 
    Marshal.ReleaseComObject(curDoc)
 
End Sub

In this procedure, I employ the predefined “page” bookmark. This is a standard Word bookmark that always exists and allows easy selection of the page containing the current focus (or selection).

You can learn more about predefined bookmarks at MSDN.

Enumerate sentences in a range

This one is similar to paragraphs but we switch sentences for paragraphs. The idea is the same… create a new document that contains the sentences in reverse order.

Private Sub EnumerateSentences()
    Dim curDoc As Word.Document = WordApp.ActiveDocument
    Dim newDoc As Word.Document
    Dim rng As Word.Range
    Dim str As String = ""
    Dim i As Integer
 
    rng = curDoc.Content
    i = rng.Sentences.Count()
 
    Do Until i = 0
        str = str & rng.Sentences.Item(i).Text
        i = i - 1
    Loop
 
    newDoc = WordApp.Documents.Add
    newDoc.Range().Text = str.ToUpper
 
    Marshal.ReleaseComObject(rng)
    Marshal.ReleaseComObject(curDoc)
    Marshal.ReleaseComObject(newDoc)
End Sub

To mix it up, the final document’s text is upper case. I know, this is dazzling trickery.

The takeaway is that the Range object contains a section of the Word document. Within it are major items like paragraphs and sentences.

The Section object

Documents can contain multiple sections. Sections allow you to define different page layouts and header/footer schemes.

Enumerate sections

You can loop through document sections by accessing the Sections collection. This collection resides directly beneath the Document object.

Private Sub EnumerateSections()
    Dim curDoc As Word.Document
    Dim newDoc As Word.Document
    Dim str As String = ""
    Dim i As Integer
 
    curDoc = WordApp.ActiveDocument
 
    For i = 1 To curDoc.Sections.Count
        str = str & "SECTION " & i & vbCr
        str = str & vbTab & "start = " & curDoc.Sections(i).Range.Start
        str = str & vbTab & "end = " & curDoc.Sections(i).Range.End & vbCrLf
    Next
 
    newDoc = WordApp.Documents.Add
    newDoc.Range().Text = str
 
    Marshal.ReleaseComObject(curDoc)
    Marshal.ReleaseComObject(newDoc)
End Sub

This procedure builds a string that 1) lists each document section and 2) contains the section’s Start and End character position. Notice the use of the Range object to read this information.

Create a new section

When building Word documents via code, you will likely need to create a new section. This procedure will do the trick.

Private Sub CreateSection()
    Dim curDoc As Word.Document
    curDoc = WordApp.ActiveDocument
 
    curDoc.Sections.Add(WordApp.Selection.Range)
 
    Marshal.ReleaseComObject(curDoc)
End Sub

This sample inserts a section break at the current selection (or cursor) location. Again, notice the use of the Range property. You can easily grab a different range within the document and pass it as the location of the page break.

The Page object

The page object resides in a “funny” location. Not “ha ha” funny… more like “why the hell is it here?” funny. You access document pages via the Document.ActiveWindow.Panes collection. Why? Because these are the rules.

Enumerate pages

Someday you might want to loop through all document pages and perform some very specific business logic on them. This code does exactly that.

Private Sub EnumeratePages()
    Dim curDoc As Word.Document
    Dim newDoc As Word.Document
    Dim pgs As Word.Pages
    Dim str As String = ""
    Dim i As Integer
 
    curDoc = WordApp.ActiveDocument
    pgs = curDoc.ActiveWindow.Panes(1).Pages
 
    For i = 1 To pgs.Count
        str = "PAGE " & i & vbCr
        str = str & vbTab & "height = " & pgs.Item(i).Height
        str = str & vbTab & "width = " & pgs.Item(i).Width & vbCrLf
    Next
 
    newDoc = WordApp.Documents.Add
    newDoc.Range().Text = str
 
    Marshal.ReleaseComObject(pgs)
    Marshal.ReleaseComObject(curDoc)
    Marshal.ReleaseComObject(newDoc)
End Sub

Here, the business logic is to create a string that contains the height and width of each page and then display it in a new document. Your business logic will probably be more complex than this. Consider this procedure a starter kit for processing document pages.

Insert a page break

To create a page, you create a page break.

Private Sub InsertPageBreak()
    Dim sel As Word.Selection
    sel = WordApp.Selection
 
    sel.InsertBreak(Type:=Word.WdBreakType.wdPageBreak)
 
    Marshal.ReleaseComObject(sel)
End Sub

I start by referencing the current insertion point via the Selection object. I then invoke the InsertBreak method and specify a page break. Walla! We have a new page.

Working with familiar (some) Word content objects

Now that you know the basics of working with the Range, Section, and Page objects, let’s look at working with typical Word content like tabless, comments, & text.

Insert a table

I use tables all the time. I can see how it would be useful to have a procedure that inserts a table exactly how I like it.

Private Sub InsertTable(rowCount As Integer, columnCount As Integer)
    Dim curDoc As Word.Document = WordApp.ActiveDocument
    Dim table As Word.Table
 
    table = curDoc.Tables.Add(WordApp.Selection.Range, rowCount, columnCount)
    table.Cell(1, 1).Range.Text = "Hello Table"
 
    Marshal.ReleaseComObject(table)
    Marshal.ReleaseComObject(curDoc)
End Sub

In this case, I like a table with 1 column and 7 rows and 0 formatting. The Tables collection resides under the Document object. To add a new table, you call the collection’s Add method and specify its location (via a Range object), number of rows, and number of columns.

Enumerate comments

Authoring a quality document of any type (blog, article, report, proposal, etc.) is a collaborative effort. Comments are key to the collaborative process. If you receive your document after this process and it is littered with helpful comments for improving it… the following code will come in handy.

Private Sub EnumerateComments()
    Dim curDoc As Word.Document = WordApp.ActiveDocument
 
    For i = 1 To curDoc.Comments.Count
        curDoc.Comments.Item(i).Range.Text = _
            curDoc.Comments.Item(i).Range.Text & vbCrLf & _
            "Corrected. It's all good now!"
    Next
 
    Marshal.ReleaseComObject(curDoc)
End Sub

Here, I loop through the Comments collection. This collection also resides directly under the Document object. For each comment, I insert a comment below the existing comment text.

Create a comment

Creating a comment is straight-forward. The approach below utilizes the current selection’s range as the insertion point.

Private Sub CreateComment(commentText As String)
    WordApp.Selection.Comments.Add(WordApp.Selection.Range, commentText)
End Sub

The text for the comment needs to be passed as the procedure’s parameter.

You can also create comments by calling Document.Comments.Add. If you do that, you need to pass a Range to specify where to insert the comment.

Delete all comments

Deleting all comments is delightfully easy. There is a method that takes care of them.

Private Sub DeleteComments()
    Dim curDoc As Word.Document = WordApp.ActiveDocument
 
    curDoc.DeleteAllComments()
 
    Marshal.ReleaseComObject(curDoc)
End Sub

There is no need to loop through the Comments collection.

Insert text

To insert text, you can utilize the Selection and Range objects.

Private Sub InsertText(textToInsert As String)
    WordApp.Selection.InsertAfter(textToInsert)
    'WordApp.Selection.InsertBefore(textToInsert)
    'WordApp.ActiveDocument.Range.InsertAfter(textToInsert)
End Sub

In this sample, I utilize the current selection to insert the passed string after the current selection. I’ve include commented code to show how you can chose to InsertBefore. Also, I’ve shown how to do the same with the Range object.

Find text

Finding text is a core competency in Word solution development. This sample performs search and replace.

Private Sub FindText()
    Dim rng As Word.Range
    rng = WordApp.ActiveDocument.Content
 
    With rng.Find
        .ClearFormatting()
        .Execute(FindText:="Hello Table", _
                 ReplaceWith:="Found Table", _
                 Replace:=Word.WdReplace.wdReplaceAll)
    End With
 
    Marshal.ReleaseComObject(rng)
End Sub

The procedure sets a Range object that contains all document Content. It then executes a Find & Replace action to replace the text inserted in the InsertTable method from earlier.

Copy and paste text

If you have text in a Word document, you will need to move it around.

Private Sub CopyAndPasteText()
    Dim curDoc As Word.Document = WordApp.ActiveDocument
    Dim rng As Word.Range
    'Dim sel As Word.Selection
    rng = curDoc.Range(curDoc.Paragraphs(1).Range.Start, _
                       curDoc.Paragraphs(3).Range.End)
    rng.Copy()
    'sel = curDoc.Range(curDoc.Paragraphs(1).Range, _
    '                    curDoc.Paragraphs(3).Range.End)
    'sel.Copy()

    WordApp.Selection.GoTo(What:=Word.WdGoToItem.wdGoToBookmark, _
                           Name:="\EndOfDoc")
    WordApp.Selection.Paste()
 
    Marshal.ReleaseComObject(rng)
    Marshal.ReleaseComObject(curDoc)
End Sub

This method stores the first 3 paragraphs in a Range object, copies them, moves to the end of the document, and pastes the paragraphs. I’ve included commented code that shows how you could perform the copy using a Selection object.

***

We have now delved into the waters of Word document content manipulation. We’ll continue looking at scenarios in future articles!

Available downloads:

This sample Word add-in was developed using Add-in Express for Office and .net:

VB.NET Word Document Content add-in

Word add-in development in Visual Studio for beginners:

35 Comments

  • https://secure.gravatar.com/avatar/feea9ebdcb1ba2eae513ddfd18a05c2c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ron says:

    Good as far as it goes, but I create:

    word_doc.sections(1).headers.text = “dog”
    then
    word_doc.sections(2).headers.text = “cat”

    all headers in the document are “cat”

    Cannot figure out why

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hello Ron,

    According to MSDN, the HeadersFooters collection does not have the Text property:
    http://msdn.microsoft.com/en-us/library/office/ff197925.aspx

    So, I cannot figure out how your code works.

  • https://secure.gravatar.com/avatar/e3b254c666b8f8dc3070a709408db7c1?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Shai says:

    Thanks for the post and really been helpful. I wonder how can I delete a page from a word doc. for example I want to delete page #3 from a 5 page document. I am populating data on a word template. Every page has a picture and some statements and putting data from different database tables. But in case there is no data in a particular page because the underlying database table has no record of it, want to delete that page. I know which page to delete but do not know how to delete. Please help.

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hello Shai!

    You want to use the Range object.
    First navigate to the page you want to delete using the GoTo command.
    Then set a Range to reference the entire page.
    Last, delete it by calling Range.Delete.

    Here is some code that can get you started.

    With WordApp.ActiveDocument
    Set Rng = .GoTo(What:=wdGoToPage, Name:=iPage)
    Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:=”\page”)
    Rng.Delete
    End With

    You can check if the page is blank by checking the range’s Word count.

    If Rng.Words.Count = 0 then Rng.Delete

    Good luck.

    Ty

  • https://secure.gravatar.com/avatar/d1a135090c0f9dee6ae1c71e864b587f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dustin C. says:

    Quick question: I consistently have reports that are actually aggregates of individual reports. This is outside of my control (right now), but I would like to break them up into their individual documents through some of the methods you listed above. One nice part is that the individual reports consistently have “END OF REPORT” near the end of the document (two lines above the end of the page, to be specific). My thought was to try to key off of that phrase, somehow drop the cursor down, and split the documents off that way. As I am somewhat of a novice to these objects, I was wondering if you could maybe shed some light?

    Thanks,

    Dustin

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Dustin,

    The best way is to record a VBA macro while performing the actions in the Word UI that you need your add-in to perform. Start with searching the “END OF REPORT” string, then select the report, cut it, create a new document, paste the contents, etc. The point is: the recorded macro shows the members of the Word object model involved in the process. Then you’ll be able to convert the macro to the programming language that you use. While working on this, you may find useful to look into the Reference and Concepts sections at http://msdn.microsoft.com/en-us/library/office/ee861527.aspx.

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hi Dustin,

    I did a little bit of research and found this article:
    How to programmatically save each page or section of a document as a separate file

    I think the samples it provides will serve as a good start.
    You have two options here
    1 – Prepare the document by manually inserting sections after the END OF REPORT line.
    2 – Use the Word object model to scan each page to locate END OF REPORT. When you find it, use the Selection object to include all content above END OF REPORT, select it, and save it.

    Good luck,

    Ty

  • https://secure.gravatar.com/avatar/01e38aa302b1a3fc7883fff844fda13c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Victor says:

    Hi Anderson,
    Thank you for porting this ,it’s really use-full to me. And i have a problem to binding data using Xml Mapping custom xml nodes .

    I have template in that i added custom xml nodes, and i bind the data based on Xml nodes using vb.net . like below

    For Each XMlnode As Word.ContentControl In wordDoc.ContentControls

    If XMlnode.XMLMapping.CustomXMLNode.BaseName = “Desc” Then OperList.Add(XMlnode.XMLMapping.CustomXMLNode.BaseName)

    Next

    wordDoc.ActiveWindow.Selection.InsertRowsBelow()

    when i try to insert new row in a table using above line command ,it is insert but xml-nodes not carried to new row …

    How can i approach to print data in multiple row using xml nodes .
    please help me ..

    Thank you .

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hi Victor,

    Thanks for raising this question.
    Please allow me a couple of days to respond.

    Ty

  • https://secure.gravatar.com/avatar/01e38aa302b1a3fc7883fff844fda13c?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Victor says:

    Hi Anderson ,
    Thank you for giving response to my problem.
    i will wait for your answer …

    Thank you .

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Victor,

    What version of Word are you targeting?
    Also, can you send your code and document to me to look at further?

    What you are doing makes sense but I need more context.
    Are you trying to create repeating content controls in a pre Word 2013 version of Word? See below:

    Repeating section content controls represented in the object model

  • https://secure.gravatar.com/avatar/b5c0a82f6b96e79071b2fde3a7a2f9a4?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Kavitha says:

    Hi Ty Anderson,

    I am struct at repeating data in word 2013.

    can you please find my word document at below link:

    https://onedrive.live.com/redir?resid=D935E20D58C2FDAF%21107

    and XML file : https://onedrive.live.com/redir?resid=D935E20D58C2FDAF!112&authkey=!ACL29rLRh6Yg0lA&ithint=file%2c.XML

    In word document CARRIER INFORMATION section i have to loop the data. But first row only it is printing.in second row i don’t have the xml nodes….so it is not printing.

    But i need to print all lines in CARRIER INFORMATION.

    Let me know how can i print all lines.

  • https://secure.gravatar.com/avatar/b5c0a82f6b96e79071b2fde3a7a2f9a4?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Kavitha says:

    Hi Ty Anderson,

    Can i have samples of using Repeating section content controls in VB.Net.

    i have find related code in VBA http://gregmaxey.mvps.org/word_tip_pages/contentcontrol_enhancements_word_2013.html

    I have one more doubt. I have word document template with office 2013.If i give this to customer who is using office 2010. how then cam manage custom xml nodes.

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hi Kavitha

    Thank you for the sample code. I will take a look.

    Ty

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hi Kavitha,

    I reviewed your document and I think you need to look at how you setup the repeating content controls in the document. I don’t see a repeating section content control in the Bill of Lading document.
    Please review these articles:
    http://www.techrepublic.com/article/pro-tip-nest-content-controls-using-word-2013s-new-repeating-section-content-control/#.
    http://msdn.microsoft.com/en-us/library/office/jj889465(v=office.15).aspx

    For a code sample, this might help:

    http://social.msdn.microsoft.com/Forums/en-US/4e9fd33f-b13a-4a7d-90b9-48a96a33a71e/how-can-i-loop-the-repeating-section-content-controls-using-vbnet?forum=worddev

    Ty

  • https://secure.gravatar.com/avatar/e219f800b7891a462aa76c033412494f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Charles Hurst says:

    Hi Ty,

    Thanks for the post it was an insightful read on my current project.

    I’m having one issue where by my code is currently stripping out any Header content and replacing with whatever the user selects from my Pop up.

    Is there anyway to insert text in to Word Header with a merge (preferably on top) without deleting all current content?

    Thanks in advance,

    Charles

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Charles,

    Yes, it is possible. Please have a look at the following MSDN article:
    http://msdn.microsoft.com/en-us/library/ms178795.aspx

    It demonstrates how to get a Range object of the main Header. Using the Range object you will be able to get and set its Text.

  • https://secure.gravatar.com/avatar/4e2ee2ecdd51a2841a993d53187efc09?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ajith. S says:

    Hi Anderson

    Your article on ‘Working with Word document content objects’ is very informative.
    My concern is programmatically using Inter.Word library how can i enable a search.
    The search results should be displayed in the navigation pane, showing all the details of the matches found.
    Kindly help.

    Thanks in advance

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hello Ajith

    Unfortunately, Word does not provide an API object for the Navigation pane.
    It is possible however to build your own pane that mimics the search behavior you want to automate.

    You can execute searches using the Find object:
    http://msdn.microsoft.com/en-us/library/office/ff839118(v=office.15).aspx

    Ty

  • https://secure.gravatar.com/avatar/c91245e076cf27130ad58c7b69708acc?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Vijay Nanda says:

    Hi Anderson,

    I want to write a small programme using VB 6.0 and MS Word 2007 wherein I want to create a document (My Report.doc) which can be edited, reformatted, displayed and printed. The full details are as under:

    There are 12 conditions. if condition No. 1 is met some pre-bookmarked portion from the already existed MS Word File (C:\My File.doc) is to be copied, a New Word Document is opened with the name “My Report.doc” and the copied portion is pasted on it.

    For example,

    If K = 1 Then (read pre bookmarked portion from the World File (C:\My File.doc) Open a New Word Document and copy paste the selected portion in this New Word Document (My Report.doc).

    Elseif K = 2

    Then (read selected portion from the World File (C:\My File.doc) Open a New Word Document and copy paste the selected portion in this New Word Document (My Report.doc).

    ……and so on till K = 12 is tested. (Only one option will be true hence only one MS Word File (My Report.doc) will be Opened.

    Similarly L, M, N, O, P, Q, R,S & T are to be tested (12 Times each) and the entire information is to be deposited in one single MS Word File (My Report.doc).

    Note :
    (Text in the C:\My File is simple text divided into paragraphs. There are no Tables/Fields etc.)

    I shall be grateful, if you could help me in making this programme using VB 6.0 and MS Word 2007 for my personal use.

    With Kind Regards

    Vijay Nanda

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hello Vijay,

    I can’t help you write the app with VB6 but I can point you to some useful links:
    http://word.mvps.org/faqs/macrosvba/WorkWithBookmarks.htm
    http://www.vbexplorer.com/VBExplorer/office_vb.asp

    Is VB6 a strict requirement? If so, why?

    Ty

  • https://secure.gravatar.com/avatar/3be0e460194f534b3f72834c6be1462b?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ravikumar says:

    Hi,

    how to get the total count of characters from beginning of the document to the current cursor position. i want to get total characters from starting to cursor edit point, not the total characters in the document.

    Please help
    Thanks.

  • https://secure.gravatar.com/avatar/c68f03884efb33fd95bcfbd9f1078c07?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ty Anderson says:

    Hi Ravikumar,

    To count the characters is simple. You only need to check this:

    Dim currentPosition As Long
    currentPosition = Selection.Range.Start

    The Selection.Range.Start is the position of the cursor.
    The integer value that .Start returns, is the number of characters that precedes it within the document.

    I hope this helps.

    Ty

  • https://secure.gravatar.com/avatar/a8f64f4fa0ca034267056d187f92b61f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G manoj choudhary says:

    Hi,
    i want to delete the blank pages that have no text in between header and footer in word file.

    please suggest any workaround for it asap.

    thanks in advance.

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Manoj,

    In Word, click the “Show/Hide all paragraph marks” toggle button in the Paragraph group on the Home tab and study the pages the look empty. Find more useful details at http://word.mvps.org/faqs/formatting/NonPrintChars.htm.

  • https://secure.gravatar.com/avatar/a8f64f4fa0ca034267056d187f92b61f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G manoj choudhary says:

    Hi Andrei,

    i want to do this using vb language.

    there is a word document which contains some blank pages so i have to delete only blank pages from the document without changing the format of other pages.

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Manoj,

    There’s no blank pages in Word; there are pages that look blank. There several scenarios in which a page may look blank: page/section breaks, hidden text, changes displayed or not displayed, etc. My suggestion is to study scenarios in which the page looks blank and handle these scenarios in code. A way to study the content of a page is to use the “Show/Hide all paragraph marks” toggle button. I suggest that you visit the URL I provided in my previous post; I strongly believe this is a very useful page for your task.

  • https://secure.gravatar.com/avatar/a8f64f4fa0ca034267056d187f92b61f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G manoj choudhary says:

    Hi Andrei,

    i have to delete that list of pages from document where there is no text between header and footer.

  • https://secure.gravatar.com/avatar/14847d037592c32a8c8e036fe751a427?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Will says:

    Hi,

    Is there a way to tell if the current selection is in the main document or in a comment? I’m trying to add test to the current cursor position but have to format the text differently if it’s in a comment.

    Thanks,
    Will

  • https://secure.gravatar.com/avatar/14847d037592c32a8c8e036fe751a427?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Will says:

    I think I figured it out. If the selection.Comments.Count > 0, it seems like the selection is within a comment.

    Thanks!

  • https://secure.gravatar.com/avatar/340db94341d638315696c2251c92e902?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G julex says:

    man i need help do you have a codes of microsoft word via vb.net

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Julex,

    Please have a look at the following post, hopefully it will prove helpful:
    Customizing Microsoft Word ribbons and toolbars

  • https://secure.gravatar.com/avatar/965f9e997d578f86fcee62afcf0f5abc?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Rogerlopensio says:

    Is there a way to add a macro to a word document through vb,net?

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Rogerlopensio,

    Yes. You start with the Document.HasVBProject and Document.VBProject properties. The latter requires referencing Microsoft.Vbe.Interop.dll; find this .DLL on your system.

Post a comment

Have any questions? Ask us right now!