如何使用 Word 2007(或 Word 2003)VBA 复制所有文本并将其粘贴到四行 csv 文档中。例如:“我爱这个世界”。这将是:
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
以下代码应输出到 .csv 文件。
笔记!首先,请添加对 Microsoft Scripting Runtime dll (scrrun.dll) 的引用:
从 VBA 窗口 Tools->References->Check Microsoft Scripting Runtime dll
这是有效的代码(您可以创建宏并将代码放入其中):
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
可以根据自己的需要按摩...
希望这可以帮助。