如何使用Word 2007 VBA复制所有文本并将其替换为for rows csv文档

发布于 2024-11-18 16:48:38 字数 246 浏览 7 评论 0原文

如何使用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

How use Word 2007(or Word 2003) VBA to copy all text and paste it into a four rows csv document. For 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-11-25 16:48:38

以下代码应输出到 .csv 文件。

注意!首先,请添加对 Microsoft Scripting Runtime dll (scrrun.dll) 的引用

从 VBA 窗口“工具”->“引用”->“检查 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

您可以根据您的需要按摩它......

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文