By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

Copy Word Doc Line by Line

P: 6
I need to open a word doc and copy each line into a variable lets say
dim Sline as string
May 8 '07 #1
Share this Question
Share on Google+
11 Replies


SammyB
Expert 100+
P: 807
I need to open a word doc and copy each line into a variable lets say
dim Sline as string
Ae you sure you mean lines and not sentences? Here's for lines:
Expand|Select|Wrap|Line Numbers
  1.     Selection.HomeKey Unit:=wdStory     ' Move to the begining of the document
  2.     Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend     ' Select a line
  3.     Dim line1 As String
  4.     line1 = Selection.Text ' Save the line
  5.     Selection.MoveRight Unit:=wdCharacter, Count:=1     ' Move to the next line
  6.     Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
  7.     Dim line2 As String
  8.     line2 = Selection.Text
May 8 '07 #2

Expert 5K+
P: 8,434
I'd guess that anj755 is referring to "paragraphs".
May 9 '07 #3

P: 6
I was actually wondering if we could do a search for CRLF and then recognize it as a line. I am database person, not much of a .net programmer, so any help will really make my work easier.
May 9 '07 #4

SammyB
Expert 100+
P: 807
I was actually wondering if we could do a search for CRLF and then recognize it as a line. I am database person, not much of a .net programmer, so any help will really make my work easier.
Ah, that is simple: Word has already done all of the work. Word already separates your document with the CRLF into what it calls paragraphs and keeps them in a collection, so if you want the text from paragraph i, it is:
ActiveDocument.Paragraphs(i).Range.Text
May 9 '07 #5

P: 6
How do I get wdStory or wdLine
Seems like I need Word.dll
but all I can find in my machine is microsoft.office.interop.word.dll
I found word.dll in the web but it is not strong named.
Is there a way arround it?
May 9 '07 #6

P: 6
Okay I found a way to get arround it. But now I can't seem to figure how to do
while not EOF for the word doc?
May 9 '07 #7

SammyB
Expert 100+
P: 807
How do I get wdStory or wdLine
Seems like I need Word.dll
but all I can find in my machine is microsoft.office.interop.word.dll
I found word.dll in the web but it is not strong named.
Is there a way arround it?
You need to add a reference to microsoft.office.interop.word.dll and then use http://www.thescripts.com/forum/thread348663.html as an example for the open, but look at http://support.microsoft.com/?kbid=315981 to see how to quit Word.

While in Word, use objDoc.Paragraphs(i).Range.Text to get paragraph i.

But, is this a Windows App or a Web App? I think there are more problems with a Web App.
May 9 '07 #8

Expert 5K+
P: 8,434
Okay I found a way to get arround it. But now I can't seem to figure how to do while not EOF for the word doc?
If the paragraphs are in a collection, then I expect you can use a For Each loop to go through them, plus there should be a .Count property to tell you how many there are (if you need to know).
May 9 '07 #9

SammyB
Expert 100+
P: 807
Okay I found a way to get arround it. But now I can't seem to figure how to do
while not EOF for the word doc?
Killer is spot on.
Expand|Select|Wrap|Line Numbers
  1. Sub Macro4()
  2.     Dim p As Paragraph
  3.     For Each p In ActiveDocument.Paragraphs
  4.         MsgBox p.Range.Text
  5.     Next p
  6. End Sub
  7. Sub Macro5()
  8.     Dim i As Integer
  9.     For i = 1 To ActiveDocument.Paragraphs.Count
  10.         MsgBox ActiveDocument.Paragraphs(i).Range.Text
  11.     Next i
  12. End Sub
May 10 '07 #10

P: 6
Thank you
All these replies were very helpful. I finished coding and my SSIS package works great. Saved me from copy and pasting from word doc to excel.
May 10 '07 #11

Expert 5K+
P: 8,434
Congratulations!

Glad we could help. :)
May 10 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.