Solved

Word Automation using .Net and Bookmarks

Posted on 2014-04-08
11
343 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 48

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 48

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
 

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 48

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 48

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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now