A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

VBA task: Copy selection in Word and paste in Excel



 
 
Thread Tools Display Modes
  #1  
Old July 9th 12, 11:17 PM posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA task: Copy selection in Word and paste in Excel

I have a Word document which contains several blocks of text enclosed in curly brackets (e.g., "{This is the text I want to copy.}"). I want to find and copy those blocks of text and paste them into cells in Excel.

It seems to me this must be a pretty common task... anybody know a simple way to do it?

Thanks.
Ads
  #2  
Old July 11th 12, 07:12 PM posted to microsoft.public.excel.programming
Peter T[_5_]
external usenet poster
 
Posts: 82
Default VBA task: Copy selection in Word and paste in Excel

Only lightly tested -

Sub test()
Dim pos1 As Long, pos2 As Long, i As Long
Dim objWord As Object

On Error Resume Next
Set objWord = GetObject(, "word.application")
If Not objWord Is Nothing Then
s = objWord.activedocument.Range.Text
End If
On Error GoTo 0
If Len(s) = 0 Then
' can't get active document with text
Exit Sub
End If
pos2 = 1
While pos2 > 0
pos1 = InStr(pos2, s, "{")
If pos1 Then
pos2 = InStr(pos1, s, "}")

End If
If pos2 >= pos1 + 2 And pos1 > 0 Then
i = i + 1
Cells(i, 1) = Mid$(s, pos1 + 1, pos2 - pos1 - 1)
Else: pos2 = 0
End If
Wend

End Sub


Could also probably Automate Word and use Word's Find method,

With objWord.Selection.Find
.ClearFormatting
.Text = "\{*\}"
.Replacement.Text = ""
.Forward = True
.Wrap = 2 ' wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

return found strings (trimmed without the brackets) to say an array or
collection or directly into cells in Excel

Regards,
Peter T

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy and paste into Excel and Word damonp3 Excel Discussion (Misc queries) 0 March 5th 09 06:55 PM
Paste Special MS Excel Worksheet range selection into Outlook Task Bugman Excel Programming 1 January 23rd 08 09:41 AM
Copy Selection - Paste Selection - Delete Selection Uninvisible Excel Programming 2 October 25th 07 01:31 PM
Won't let me copy and paste from Excel to Word SueB57 Excel Discussion (Misc queries) 0 November 14th 05 05:14 PM
change from copy & paste task pane (yuk) to c&p little box Christie Excel Discussion (Misc queries) 0 October 27th 05 01:39 PM


All times are GMT +1. The time now is 11:37 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.