Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Word Automation using .Net and Bookmarks

Posted on 2014-04-08
11
Medium Priority
?
356 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 53

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 53

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
Independent Software Vendors: 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 53

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 53

Accepted Solution

by:
Rgonzo1971 earned 2000 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 53

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

Technology Partners: 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!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

885 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