How use Word 2007 VBA to copy all text and replace it into a for rows csv document
How use Word 2007(or Word 2003) VBA to copy all text and paste it into a four rows csv document. F开发者_如何学运维or example: "I love the world". It will be:
I - Line 1 - page 1 - Paragraph 1
love - Line 1 - page 1 - Paragraph 1
the - Line 1 - page 1 - Paragraph 1
word - Line 1 - page 1 - Paragraph 1
The following code should output to .csv file.
Note! First, please add reference to the Microsoft Scripting Runtime dll (scrrun.dll):
From the VBA window Tools->References->Check Microsoft Scripting Runtime dll
Here is the code that works (you can create macro and place the code inside it):
Dim wordsArray, arrayElement
Dim delimiter As String
Dim fileName As String
Dim fso As FileSystemObject
Dim outputFile As textStream
'select all document's content
ActiveDocument.Select
'provide delimiter
delimiter = InputBox("Please enter delimiter to use")
'split the selected content and place it inside the array
wordsArray = Split(Selection.Text, delimiter)
'generate output file name
fileName = "C:\Output.csv"
'create new FileSystem object and open text stream to write to
Set fs = New FileSystemObject
Set outputFile = fs.CreateTextFile(fileName, True) 'note file will be overwritten
'iterate through array and write to the file
For Each arrayElement In wordsArray
'Use the following code to place each word into separate COLUMN
'outputFile.Write (arrayElement) & ","
'Use the following code to place each word into separate ROW
outputFile.WriteLine (arrayElement)
Next
'close output stream
outputFile.Close
You can massage it based on your needs...
Hope this helps.
精彩评论