PDA

View Full Version : Deleting and inserting text based on users choice



SwatiB
09-20-2017, 06:40 AM
Hi,

I am extremely new to VBA coding, specially for Word. I have to write a code where based on the users selection, one para gets removed or remains in the document. My code works only once. I have used bookmarks and once the para is deleted if the user chooses option 1 again, i dont know how to reinsert the bookmark. I am alternatively also trying to build a userform with combobox, i dont know if I use Cases to define this maybe it will work. Please advise.

Here is my code till now:

Sub CheckBox1_Click()
Dim answer1 As Long




'This will return what happens when the CP Manager check box (first checkbox)is clicked.


If (CheckBox1.Value = True) Then
answer1 = MsgBox("Are you a CP Manager", vbYesNo + vbQuestion, "Please check your choice")

If answer1 = vbYes Then

MsgBox ("Please read through the document")

ActiveDocument.Bookmarks("Reg_comp").Range.Font.Hidden = True

With Selection
.GoTo What:=wdGoToBookmark, Name:="Start"
End With

Else

MsgBox ("Please select option 2")

End If

Else: CheckBox2_Click

End If

CheckBox1.Value = False

End Sub


Private Sub CheckBox2_Click()
Dim answer2 As Long


If CheckBox2.Value = True Then
answer2 = MsgBox("Are you a CoC Manager", vbYesNo + vbQuestion, "Please check your choice")
CoC

Else
MsgBox ("Please select option 1")

End If


End Sub


Sub CoC()


With Selection
.GoTo What:=wdGoToBookmark, Name:="Reg_comp"
Selection.Delete
.GoTo What:=wdGoToBookmark, Name:="Start"
End With

CheckBox2.Value = False


End Sub

gmaxey
09-20-2017, 03:33 PM
Assume you have a bookmark named bmDemo. Write to the bookmark range and redefine the bookmark:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/20/2017
Dim oBM As Bookmark
Dim oRng As Range
Set oBM = ActiveDocument.Bookmarks("bmDemo")
Set oRng = oBM.Range
oRng.Text = vbNullString
If MsgBox("Are you a whatever?", vbYesNo, "QUESTION") = vbYes Then
oRng.Text = "whatever, whatever, whatever"
ActiveDocument.Bookmarks.Add "bmDemo", oRng
End If
ActiveDocument.Bookmarks.Add "bmDemo", oRng
lbl_Exit:
Exit Sub
End Sub

SwatiB
09-21-2017, 03:19 AM
Hi Greg,

Thank you so much for your reply!

I am just having one small problem - The bookmark needs to store a large paragraph, and when i try to insert that large paragraph using oRng.Text = "whatever, whatever, whatever" it doesnt work when the sentence goes to another line. The " appear at the end of the first sentence and i lose all formatting. Is there any way to presave the para text as the Bookmark and then call it via oRng.Text?

It works when I try inserting a few words, just not working on the large para :(

gmaxey
09-21-2017, 06:09 AM
If you need formatted text you could insert a building block in the bookmark. Say your building block is stored in the AutoText gallery, General Category of the document attached template:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/20/2017
Dim oBM As Bookmark
Dim oRng As Range
Dim oBB As BuildingBlock
Dim oBBT As BuildingBlockType
Set oBM = ActiveDocument.Bookmarks("bmDemo")
Set oRng = oBM.Range
oRng.Text = vbNullString
If MsgBox("Are you a whatever?", vbYesNo, "QUESTION") = vbYes Then
Set oBBT = ThisDocument.AttachedTemplate.BuildingBlockTypes(wdTypeAutoText)
Set oBB = oBBT.Categories("General").BuildingBlocks("TextSnippet")
Set oRng = oBB.Insert(oRng, True)
End If
ActiveDocument.Bookmarks.Add "bmDemo", oRng
lbl_Exit:
Set oBBT = Nothing: Set oBB = Nothing: Set oRng = Nothing
Exit Sub
End Sub

SwatiB
09-21-2017, 07:23 AM
Thank you so much for your help Greg! I will implement this..