Solved

Word Automation using .Net and Bookmarks

Posted on 2014-04-08
11
346 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 49

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 49

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 49

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 49

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 49

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 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