|
|
|
date: Fri, 3 Oct 2008 08:49:02 -0700,
group: microsoft.public.word.vba.general
back
Re: Word count from word document into a cell in excel
The following code will let you select a folder containing the documents and
then it will create a table in a Word document that contains the document
names in one column and the numbe of words in each document in a second
column.
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long
Dim Target As Document
Dim TargetTable As Table
Dim drow As Row
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
If .Show = -1 Then
PathToUse = .SelectedItems(1) & "\"
Else
End If
End With
Set fd = Nothing
MsgBox PathToUse
If Len(PathToUse) = 0 Then
Exit Sub
End If
myFile = Dir$(PathToUse & "*.doc")
Set Target = Documents.Add
Set TargetTable = Target.Tables.Add(Target.Range, 1, 2)
With TargetTable
.Cell(1, 1).Range.Text = "Document"
.Cell(1, 2).Range.Text = "Word Count"
End With
While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
Set drow = TargetTable.Rows.Add
drow.Cells(1).Range.Text = myDoc.Name
drow.Cells(2).Range.Text = myDoc.Words.Count
myDoc.Close SaveChanges:=wdSaveChanges
myFile = Dir$()
Wend
You can copy and paste the table into Excel if that is where you really need
the information.
If you do not know what to do with the above code, see the article "What do
I do with macros sent to me by other newsgroup readers to help me out?" at:
http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
"Justin" wrote in message
news:4F48BB2B-0A78-473B-B7F2-BB4354C778AA@microsoft.com...
> Doug,
>
> I am trying to accomplish the same thing as Laebrye. Essentially, I am
> hoping to produce an Excel spreadsheet which contains word counts for a
> list
> of documents, in order to help with layout for a publication. I have no
> familiarity with macros or VBA, and the link you had pointed out was a bit
> beyond my understanding. Any suggestions?
>
> -J
>
> "Doug Robbins - Word MVP" wrote:
>
>> See the article "Control Excel from Word" at:
>>
>> http://www.word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm
>>
>>
>> --
>> Hope this helps.
>>
>> Please reply to the newsgroup unless you wish to avail yourself of my
>> services on a paid consulting basis.
>>
>> Doug Robbins - Word MVP
>>
>> "Laebrye" wrote in message
>> news:EA543315-985D-41B3-A1B6-AE1AB3600CD0@microsoft.com...
>> > Hi everybody,
>> >
>> > I'm trying to do something very specific with absolutely no luck. All I
>> > want
>> > to do is find a way of putting the word count from a word file into a
>> > cell
>> > in
>> > an excel file. Ideally, I'd like a way of updating the the wordcount to
>> > a
>> > different cell in the same worksheet each time I perform the macro (I'm
>> > guessing it will have to be a macro, I can't find another way fo doing
>> > it.
>> >
>> > Any help I can get would be appreciated.
>> >
>> > Laebrye
>>
>>
>>
date: Wed, 19 Nov 2008 20:07:48 +1000
author: Doug Robbins - Word MVP
Re: Word count from word document into a cell in excel
Thanks, Doug! That works really well. I added a second column to
incorporate character count, as word count alone for layouts can often be
misleading.
One problem that I ran into, however, was that this iteration of the macro
does not take into account the presence of endnotes or footnotes. Any
suggestions on how to tweak it?
Again, thanks so much for your help!
"Doug Robbins - Word MVP" wrote:
> The following code will let you select a folder containing the documents and
> then it will create a table in a Word document that contains the document
> names in one column and the numbe of words in each document in a second
> column.
>
> Dim myFile As String
> Dim PathToUse As String
> Dim myDoc As Document
> Dim Response As Long
> Dim Target As Document
> Dim TargetTable As Table
> Dim drow As Row
> Dim fd As FileDialog
> Set fd = Application.FileDialog(msoFileDialogFolderPicker)
> With fd
> If .Show = -1 Then
> PathToUse = .SelectedItems(1) & "\"
> Else
> End If
> End With
> Set fd = Nothing
> MsgBox PathToUse
> If Len(PathToUse) = 0 Then
> Exit Sub
> End If
> myFile = Dir$(PathToUse & "*.doc")
> Set Target = Documents.Add
> Set TargetTable = Target.Tables.Add(Target.Range, 1, 2)
> With TargetTable
> .Cell(1, 1).Range.Text = "Document"
> .Cell(1, 2).Range.Text = "Word Count"
> End With
> While myFile <> ""
> Set myDoc = Documents.Open(PathToUse & myFile)
> Set drow = TargetTable.Rows.Add
> drow.Cells(1).Range.Text = myDoc.Name
> drow.Cells(2).Range.Text = myDoc.Words.Count
> myDoc.Close SaveChanges:=wdSaveChanges
> myFile = Dir$()
> Wend
>
> You can copy and paste the table into Excel if that is where you really need
> the information.
>
> If you do not know what to do with the above code, see the article "What do
> I do with macros sent to me by other newsgroup readers to help me out?" at:
>
> http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
>
>
> --
> Hope this helps.
>
> Please reply to the newsgroup unless you wish to avail yourself of my
> services on a paid consulting basis.
>
> Doug Robbins - Word MVP
>
> "Justin" wrote in message
> news:4F48BB2B-0A78-473B-B7F2-BB4354C778AA@microsoft.com...
> > Doug,
> >
> > I am trying to accomplish the same thing as Laebrye. Essentially, I am
> > hoping to produce an Excel spreadsheet which contains word counts for a
> > list
> > of documents, in order to help with layout for a publication. I have no
> > familiarity with macros or VBA, and the link you had pointed out was a bit
> > beyond my understanding. Any suggestions?
> >
> > -J
> >
> > "Doug Robbins - Word MVP" wrote:
> >
> >> See the article "Control Excel from Word" at:
> >>
> >> http://www.word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm
> >>
> >>
> >> --
> >> Hope this helps.
> >>
> >> Please reply to the newsgroup unless you wish to avail yourself of my
> >> services on a paid consulting basis.
> >>
> >> Doug Robbins - Word MVP
> >>
> >> "Laebrye" wrote in message
> >> news:EA543315-985D-41B3-A1B6-AE1AB3600CD0@microsoft.com...
> >> > Hi everybody,
> >> >
> >> > I'm trying to do something very specific with absolutely no luck. All I
> >> > want
> >> > to do is find a way of putting the word count from a word file into a
> >> > cell
> >> > in
> >> > an excel file. Ideally, I'd like a way of updating the the wordcount to
> >> > a
> >> > different cell in the same worksheet each time I perform the macro (I'm
> >> > guessing it will have to be a macro, I can't find another way fo doing
> >> > it.
> >> >
> >> > Any help I can get would be appreciated.
> >> >
> >> > Laebrye
> >>
> >>
> >>
>
>
>
date: Wed, 19 Nov 2008 14:49:02 -0800
author: Justin
Re: Word count from word document into a cell in excel
You would need to iterate through the StoryRanges in the document and add
the totals from each. For some information on iterating through the
StoryRanges, see the article "Using a macro to replace text where ever it
appears in a document including Headers, Footers, Textboxes, etc." at:
http://www.word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
"Justin" wrote in message
news:B353F718-AE61-4EAF-A1D8-C5F928CD410C@microsoft.com...
> Thanks, Doug! That works really well. I added a second column to
> incorporate character count, as word count alone for layouts can often be
> misleading.
>
> One problem that I ran into, however, was that this iteration of the macro
> does not take into account the presence of endnotes or footnotes. Any
> suggestions on how to tweak it?
>
> Again, thanks so much for your help!
>
> "Doug Robbins - Word MVP" wrote:
>
>> The following code will let you select a folder containing the documents
>> and
>> then it will create a table in a Word document that contains the document
>> names in one column and the numbe of words in each document in a second
>> column.
>>
>> Dim myFile As String
>> Dim PathToUse As String
>> Dim myDoc As Document
>> Dim Response As Long
>> Dim Target As Document
>> Dim TargetTable As Table
>> Dim drow As Row
>> Dim fd As FileDialog
>> Set fd = Application.FileDialog(msoFileDialogFolderPicker)
>> With fd
>> If .Show = -1 Then
>> PathToUse = .SelectedItems(1) & "\"
>> Else
>> End If
>> End With
>> Set fd = Nothing
>> MsgBox PathToUse
>> If Len(PathToUse) = 0 Then
>> Exit Sub
>> End If
>> myFile = Dir$(PathToUse & "*.doc")
>> Set Target = Documents.Add
>> Set TargetTable = Target.Tables.Add(Target.Range, 1, 2)
>> With TargetTable
>> .Cell(1, 1).Range.Text = "Document"
>> .Cell(1, 2).Range.Text = "Word Count"
>> End With
>> While myFile <> ""
>> Set myDoc = Documents.Open(PathToUse & myFile)
>> Set drow = TargetTable.Rows.Add
>> drow.Cells(1).Range.Text = myDoc.Name
>> drow.Cells(2).Range.Text = myDoc.Words.Count
>> myDoc.Close SaveChanges:=wdSaveChanges
>> myFile = Dir$()
>> Wend
>>
>> You can copy and paste the table into Excel if that is where you really
>> need
>> the information.
>>
>> If you do not know what to do with the above code, see the article "What
>> do
>> I do with macros sent to me by other newsgroup readers to help me out?"
>> at:
>>
>> http://www.word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
>>
>>
>> --
>> Hope this helps.
>>
>> Please reply to the newsgroup unless you wish to avail yourself of my
>> services on a paid consulting basis.
>>
>> Doug Robbins - Word MVP
>>
>> "Justin" wrote in message
>> news:4F48BB2B-0A78-473B-B7F2-BB4354C778AA@microsoft.com...
>> > Doug,
>> >
>> > I am trying to accomplish the same thing as Laebrye. Essentially, I am
>> > hoping to produce an Excel spreadsheet which contains word counts for a
>> > list
>> > of documents, in order to help with layout for a publication. I have
>> > no
>> > familiarity with macros or VBA, and the link you had pointed out was a
>> > bit
>> > beyond my understanding. Any suggestions?
>> >
>> > -J
>> >
>> > "Doug Robbins - Word MVP" wrote:
>> >
>> >> See the article "Control Excel from Word" at:
>> >>
>> >> http://www.word.mvps.org/FAQs/InterDev/ControlXLFromWord.htm
>> >>
>> >>
>> >> --
>> >> Hope this helps.
>> >>
>> >> Please reply to the newsgroup unless you wish to avail yourself of my
>> >> services on a paid consulting basis.
>> >>
>> >> Doug Robbins - Word MVP
>> >>
>> >> "Laebrye" wrote in message
>> >> news:EA543315-985D-41B3-A1B6-AE1AB3600CD0@microsoft.com...
>> >> > Hi everybody,
>> >> >
>> >> > I'm trying to do something very specific with absolutely no luck.
>> >> > All I
>> >> > want
>> >> > to do is find a way of putting the word count from a word file into
>> >> > a
>> >> > cell
>> >> > in
>> >> > an excel file. Ideally, I'd like a way of updating the the wordcount
>> >> > to
>> >> > a
>> >> > different cell in the same worksheet each time I perform the macro
>> >> > (I'm
>> >> > guessing it will have to be a macro, I can't find another way fo
>> >> > doing
>> >> > it.
>> >> >
>> >> > Any help I can get would be appreciated.
>> >> >
>> >> > Laebrye
>> >>
>> >>
>> >>
>>
>>
>>
date: Thu, 20 Nov 2008 16:13:44 +1000
author: Doug Robbins - Word MVP
|
|