We help IT Professionals succeed at work.

Problem converting Word VBA to VB10

578 Views
Last Modified: 2012-05-09
I have this VBA code in a Word 2007 file that moved one character at a time through a document
and at each change in a Style it would insert a string before the letter of the changed style text.
The sting is just the name o the style. I have been tying to convert this code to an app using VB 2010 Express.
I have no problems opening the word file, but I can't find how to move one character at a time through the whole document, identity the end o the document, and return the name of the selected text's style.
Any help will be greatly appreciated!
Public Sub PlaceTags()
'This procedure will scan a document for Word Styles and 
'   place a layout process tag before each change in text 
'   styles.

Dim cStyle As Style

'Place the curse at the top of the document
Selection.HomeKey Unit:=wdStory

'Place the first tag at the top of the doc.
Selection.InsertBefore "<" & Selection.Style & ">"

'This var will contain the last Style of the previous Char
Set cStyle = Selection.Style

'Loop until you reach the bottom of the document
Do Until ActiveDocument.Bookmarks("\Sel").Start = _
         ActiveDocument.Bookmarks("\EndOfDoc").Start

    'Select the next Char
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
                        Extend:=wdExtend
    
    'Check to see if the selection has no Style - Like images
    If Selection.Style <> "Normal" Then
    
        'Assign the right Tag
        If Selection.Style <> cStyle Then
        
          'Add the Tag
          Selection.InsertBefore "<" & Selection.Style & ">"
        
          'Move the selections to the next Char
          Selection.Move Unit:=wdCharacter, Count:=1
        
        End If
    End If
    
    'Set the new current Style
    Set cStyle = Selection.Style
           
    'Move to the next Char
    Selection.Move Unit:=wdCharacter, Count:=1
    
Loop

End Sub

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Great help. I had a death in the family and I totally forgot that I even posted this question.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.