Solved

Word Automation using .Net and Bookmarks

Posted on 2014-04-08
11
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 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