gpt4 book ai didi

vba - 使用VB宏将数据从Word表复制到Excel表时如何保留源格式?

转载 作者:行者123 更新时间:2023-12-03 00:32:16 26 4
gpt4 key购买 nike

我正在尝试使用 VB 宏将一些数据从 Word 表复制到 Excel 工作表。

它正在根据需要完美地复制文本。

现在我想保留 Word 文档中存在的源格式。

我想保留的东西是

  1. 删除线
  2. 颜色
  3. 项目符号
  4. 换行符

我正在使用以下代码进行复制 -

objTemplateSheetExcelSheet.Cells(1, 2) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)

请告诉我如何编辑它以保留源格式。

我使用的逻辑如下 -

wdFileName = Application.GetOpenFilename("Word files (*.*),*.*", , _
"Browse for file containing table to be imported") '(Browsing for a file)

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) '(open Word file)

With wdDoc
'enter code here`
TableNo = wdDoc.tables.Count '(Counting no of tables in the document)
If TableNo = 0 Then
MsgBox "This document contains no tables", _
vbExclamation, "Import Word Table"
End If
End With

我正在对单词文件运行表计数。然后,对于单词文档中存在的所有表格,使用上述代码访问表格的每一行和每一列。

好的,我也附上剩余的代码

'Creating TemplateSheet object
Set objTemplateSheetExcelApp = CreateObject("Excel.Application")
'Opening the template to be used
objTemplateSheetExcelApp.Workbooks.Open ("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx")
Set objTemplateSheetExcelWkBk = objTemplateSheetExcelApp.ActiveWorkbook.Worksheets(5)
Set objTemplateSheetExcelSheet = objTemplateSheetExcelApp.ActiveWorkbook.Worksheets(5) '(Selecting the desired tab)

tblcount = 1
For tblcount = 1 To TableNo
With .tables(tblcount)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
On Error Resume Next
strEach = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
For arrycnt = 0 To 15
YNdoc = InStr(strEach, myArray(arrycnt))
If (YNdoc > 0) Then
objTemplateSheetExcelSheet.Cells(2, yourArray(arrycnt)) = _
WorksheetFunction.Clean(.cell(iRow, iCol + 1).Range.Text)
If arrycnt = 3 Or arrycnt = 6 Then
objTemplateSheetExcelSheet.Cells(2, yourArray(arrycnt) + 1) = _
WorksheetFunction.Clean(.cell(iRow + 1, iCol + 1).Range.Text)
End If
End If
Next arrycnt
Next iCol
Next iRow
End With
Next tblcount
End With
intRow = 1

'To save the file
strFileName = "Newfile.xlsx"
objTemplateSheetExcelWkBk.SaveAs strFld & "\" & strFileName

objTemplateSheetExcelApp.Quit

Set objTemplateSheetExcelApp = Nothing
Set objTemplateSheetExcelWkBk = Nothing
Set objTemplateSheetExcelSheet = Nothing

Set wdDoc = Nothing

最佳答案

要从 Excel 与 Word 交互,您可以选择早期绑定(bind)或后期绑定(bind)。我正在使用后期绑定(bind),您不需要添加任何引用。

我将分 5 部分介绍代码

  1. 与 Word 实例绑定(bind)
  2. 打开 Word 文档
  3. 与 Word 表格交互
  4. 声明您的 Excel 对象
  5. 将单词表复制到 Excel
<小时/>

A.与 Word 实例绑定(bind)

<小时/>

声明您的 Word 对象,然后与现有的 Word 实例绑定(bind)或创建一个新实例。例如

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object

'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True
End Sub
<小时/>

B.打开Word文档

<小时/>

连接/创建 Word 实例后,只需打开 Word 文件即可。请参阅此示例

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String

FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
"Browse for file containing table to be imported")

'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

'~~> Open the Word document
Set oWordDoc = oWordApp.Documents.Open(FlName)
End Sub
<小时/>

C.与Word表格交互

<小时/>

现在您已打开文档,让我们连接一下 word 文档的 Table1。

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
Dim tbl As Object

FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
"Browse for file containing table to be imported")

'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

Set oWordDoc = oWordApp.Documents.Open(FlName)

Set tbl = oWordDoc.Tables(1)
End Sub
<小时/>

D.声明您的 Excel 对象

<小时/>

现在我们有了 Word 表的句柄。在复制之前,让我们设置 Excel 对象。

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
Dim tbl As Object

FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
"Browse for file containing table to be imported")

'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

Set oWordDoc = oWordApp.Documents.Open(FlName)

Set tbl = oWordDoc.Tables(1)

'~~> Excel Objects
Dim wb As Workbook, ws As Worksheet

Set wb = Workbooks.Open("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx")

Set ws = wb.Sheets(5)
End Sub
<小时/>

E.将Word表格复制到Excel

<小时/>

最后,当我们设置好目标位置后,只需将表格从 Word 复制到 Excel 即可。请参阅此。

Sub Sample()
Dim oWordApp As Object, oWordDoc As Object
Dim FlName As String
Dim tbl As Object

FlName = Application.GetOpenFilename("Word files (*.Doc*),*.Doc*", , _
"Browse for file containing table to be imported")

'~~> Establish an Word application object
On Error Resume Next
Set oWordApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set oWordApp = CreateObject("Word.Application")
End If
Err.Clear
On Error GoTo 0

oWordApp.Visible = True

Set oWordDoc = oWordApp.Documents.Open(FlName)

Set tbl = oWordDoc.Tables(1)

'~~> Excel Objects
Dim wb As Workbook, ws As Worksheet

Set wb = Workbooks.Open("C:\Temp\Documents Page XX_US-VC Combo Template.xlsx")

Set ws = wb.Sheets(1)

tbl.Range.Copy

ws.Range("A1").Activate

ws.Paste
End Sub

屏幕截图

Word文档

enter image description here

Excel(粘贴后)

enter image description here

希望这有帮助。

关于vba - 使用VB宏将数据从Word表复制到Excel表时如何保留源格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12245525/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com