简体   繁体   English

Word VBA 解决复制粘贴到 excel 时的换行或段落标记问题

[英]Word VBA to solve linebreak or paragraph mark issue when copy & Paste into excel

I have managed to copy and paste a table in word using VBA into excel but the end result isn\t quite what I have expected.我已经设法使用 VBA 将 Word 中的表格复制并粘贴到 excel 中,但最终结果与我的预期不太一样。

There is this problem of a linebreak issue in some of my cells that I would like to solve.我想解决的一些单元格中存在换行问题。

For example,例如,

I have a text in a table in word with certain linebreak like我在 word 表格中有一个带有某些换行符的文本,例如

"This is a sunny day and we have the following options:" “这是一个阳光明媚的日子,我们有以下选择:”

   a) Go shopping

   b) Stay Indoor

The problem with this is that once it is imported into excel, point a and b is no longer in the same cell in excel.这样做的问题是,一旦它被导入到 excel 中,点 a 和 b 就不再在 excel 的同一个单元格中。 Excel inserted it as a new cell each instead of formatting the points into a single cell. Excel 将其作为一个新单元格插入每个单元格,而不是将这些点格式化为一个单元格。

I have tried to use find and replace to replace the linebreak ^p into blank but the letter a and b would be removed as space.我尝试使用查找和替换将换行符 ^p 替换为空白,但字母 a 和 b 将作为空格删除。 I don't want to replace my a and b as space.我不想将我的 a 和 b 替换为空格。 I still need them to be inserted into the excel cell.我仍然需要将它们插入 excel 单元。

Sub CreateExcel()

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim tableWord As Word.Table


Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False

Set xlWB = xlApp.Workbooks.Add ' create a new workbook
'declare the cell to put the text in
With xlWB.Worksheets(1)
     Set tableWord = ActiveDocument.Tables(1)
     tableWord.Range.Copy
    .Paste Destination:=xlWB.Worksheets(1).Range("A1")

    xlApp.Dialogs(xlDialogSaveAs).Show
End With

xlWB.Close False ' close the workbook without saving
xlApp.Quit ' close the Excel application
Set xlWB = Nothing
Set xlApp = Nothing

End Sub

You can try to search & replace with the Carriage Return character: Chr(10)您可以尝试使用回车符进行搜索和替换: Chr(10)

[EDIT] Well, i can't find a way to deal straight forward with your issue, maybe a better vba expert may find a solution. [编辑]好吧,我找不到直接处理您的问题的方法,也许更好的 vba 专家可能会找到解决方案。

The workaround i found is to concatenate the values of the cell after you pasted them because there is no other way to keep formatting either (i adapted my Excel code to make it usable in Word vba):我发现的解决方法是在粘贴单元格的值连接它们,因为没有其他方法可以保持格式化(我调整了我的 Excel 代码以使其在 Word vba 中可用):

Option Explicit

Sub CreateExcel()

Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim tableWord As Word.Table
Dim cell As Excel.Range, rUsed As Excel.Range, target As Excel.Range
Dim val As String

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
'xlApp.Visible = False

Set xlWB = xlApp.Workbooks.Add ' create a new workbook
'declare the cell to put the text in
With xlWB.Worksheets(1)
     Set tableWord = ActiveDocument.Tables(1)
     tableWord.Range.Copy
    .Paste Destination:=xlWB.Worksheets(1).Range("A1")

    'Select used range to get every cell that was pasted and has content
    Set rUsed = xlWB.Worksheets(1).UsedRange
    Set target = rUsed.Cells(1, 1)
    'Get the value of each cell and concatenate its value
    For Each cell In rUsed
        If cell.Value <> "" Then
            val = val + cell.Value + Chr(10)
            cell.ClearContents
        End If
    Next cell
    'Remove last carriage return
    target.Value = Left(val, Len(val) - 1)

    xlApp.Dialogs(xlDialogSaveAs).Show
End With

xlWB.Close False ' close the workbook without saving
xlApp.Quit ' close the Excel application
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

Regards,问候,
Max最大限度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM