Word VBA Macro Select Range of text including Range

Kal

Joined
Aug 17, 2015
Messages
1
Reaction score
0
I am new to VBA Macro programming. I need help to accomplish requirement.I have created a .Dotm file and deployed at startup folder of word.

My requirement is to copy the text between the two text range including range.

Because i don't have enough reputation i can't upload image. Example is given in below URL.

2rr9aw4.jpg

So i want to select the text between |{1A4F7D93-AD10-E411-9403-00155D101201}| highlighted red in image.

I have written below macro code to achieve the requirement. it is working fine upto certain extent but it adds extra page and sometime ignores Guid at the end. I think below code causes issue. I have specified count to 12. Selection.MoveRight Unit:=wdWord, Count:=12, Extend:=wdExtend

Dim lGuid As String lGuid = "{1A4F7D93-AD10-E411-9403-00155D101201}"

Dim tmpDoc As Document
Set tmpDoc =New DocumentSet tmpDoc = Documents.Add(Visible:=False)
tmpDoc.Paragraphs(1).Range.Delete

ActiveDocument.Select
Selection.WholeStory
Selection.Copy
Selection.HomeKey
tmpDoc.Select
Selection.WholeStory
Selection.PasteAndFormat wdFormatOriginalFormatting

Set myRange = tmpDoc.Range
firstTerm ="|"& lGuid &"|"
secondTerm ="|"& lGuid &"|"With myRange.Find
.Text = firstTerm.MatchWholeWord =True.Execute
myRange.Collapse direction:=wdCollapseEnd

Set selRange = tmpDoc.Range
selRange.Start = myRange.End.Text = secondTerm
.MatchWholeWord =True.Execute
myRange.Collapse direction:=wdCollapseStart
selRange.End= myRange.Start
selectedText = selRange.Select
Selection.MoveStart Unit:=wdLine, Count:=-1
Selection.MoveRight Unit:=wdWord, Count:=12, Extend:=wdExtend
Selection.Copy
EndWith

Dim workingDocument As DocumentSet workingDocument =New DocumentSet workingDocument = Documents.Add(Visible:=False)
workingDocument.Paragraphs(1).Range.Delete

workingDocument.Range.PasteAndFormat wdFormatOriginalFormatting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top