Solved

Word Automation using .Net and Bookmarks

Posted on 2014-04-08
11
347 Views
Last Modified: 2014-04-10
Dim objWord as new Word.Application
Dim objDoc as Word.Document = objWord.Documents.Add

Here i need to add about 5 bookmarks. I dont know how to move the cursor.
If i just add bookmarks they are all on top of each other.
I need to add a bookmark and then add another bookmark on the following line and so on.

Now based the outcome of my processing i may need to delete a bookmark and then delete the line that it was on so that all bookmarks below it shift upwards one line.

Can you please tell me how to accomplish these things?
0
Comment
Question by:Basicfarmer
  • 6
  • 5
11 Comments
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39988065
Hi,

pls try

Sub MacroAddBM()

Set objDoc = ActiveDocument
Selection.GoTo What:=wdGoToBookmark, Name:="\EndOfDoc"
Selection.TypeText Text:="YourVariable"
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
With objDoc.Bookmarks
        .Add Range:=Selection.Range, Name:="BM4"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
End With
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
End Sub

Sub MacroDelBM()
Set objDoc = ActiveDocument
Selection.GoTo What:=wdGoToBookmark, Name:="BM4"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1

End Sub

Open in new window

Regards
0
 

Author Comment

by:Basicfarmer
ID: 39988697
Visual Basic 2010 does not like this. Is this VBA? I need it in Visual Basic.
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39988939
Hi,

let's try

Sub MacroAddBM()
Dim objWord as new Word.Application
Set objDoc = objWord.ActiveDocument
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="\EndOfDoc"
objWord.Selection.TypeText Text:="YourVariable"
objWord.Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
With objDoc.Bookmarks
        .Add Range:=Selection.Range, Name:="BM4"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
End With
    objWord.Selection.EndKey Unit:=wdLine
    objWord.Selection.TypeParagraph
End Sub

Sub MacroDelBM()
Dim objWord as new Word.Application
Set objDoc = objWord.ActiveDocument
objWord.Selection.GoTo What:=wdGoToBookmark, Name:="BM4"
objWord.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
objWord.Selection.Delete Unit:=wdCharacter, Count:=1

End Sub

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:Basicfarmer
ID: 39990145
Im sorry, but i cannot get this to work in .NET. I recorded a macro in word. I added a bookmark, then entered a carriage return, then added another bookmark and entered a carriage return, then added another bookmark and entered a carriage return. I cannot figure out how to translate this to .Net.
Sub Macro1()
'
' Macro1 Macro
'
'
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Mellow"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    Selection.TypeParagraph
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Start"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    Selection.TypeParagraph
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Test"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    Selection.TypeParagraph
End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39990761
And now?
Sub Macro1()
'
' Macro1 Macro
'
'
Dim objWord as new Word.Application
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Mellow"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Start"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="Test"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
End Sub

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 39991626
No, it will not accept the VBA formatting. Please see the screen shot. The word "selection" is not present as an option.
Screen Shot
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39991909
Of course

Sub Macro1()
'
' Macro1 Macro
'
'
Dim objWord as new Word.Application
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=objWord.Selection.Range, Name:="Mellow"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=objWord.Selection.Range, Name:="Start"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
    With objWord.ActiveDocument.Bookmarks
        .Add Range:=objWord.Selection.Range, Name:="Test"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    objWord.Selection.TypeParagraph
End Sub

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 39991955
That worked!!!
        objDoc.Bookmarks.Add(Range:=objWord.Selection.Range, Name:="Mellow")
        objWord.Selection.TypeParagraph()
        objDoc.Bookmarks.Add(Range:=objWord.Selection.Range, Name:="Start")
        objWord.Selection.TypeParagraph()
        objDoc.Bookmarks.Add(Range:=objWord.Selection.Range, Name:="Test")
        objWord.Selection.TypeParagraph()

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 39991962
I still really dont understand the "Seletion" and how it works with the document. Can you elaborate?
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 39991972
Selection can be where the cursor is or the selected text or a selected object like a shape
0
 

Author Comment

by:Basicfarmer
ID: 39991998
I will ask a new question on EE so you can get the points here in the next few minutes. I want to move on to a couple more questions I have.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question