VBA-马车returns
VBA-carriage returns
这里是新的,一般来说 VBA。我创建了一个宏,它复制 excel 中单元格的内容并粘贴到 word 文档中的特定位置。仅供参考,我在 word 中使用书签 select 粘贴的确切位置。问题是复制的所有内容都会插入一行 and/or paragraph/carriage return。我已经找到了很多可能的解决方案,但其中 none 可行,可能是因为我在 VBA 方面缺乏经验。请帮忙!
Sub OpenWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim R1 As Object
Dim R2 As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\Users\KG\Desktop\VBA WIP\FAfile.docx")
Set R1 = WordDoc.Bookmarks("b1")
Set R2 = WordDoc.Bookmarks("b2")
WordApp.Visible = True
WordApp.Activate
Sheets("Details INPUT").Range("H4").copy
R1.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Sheets("Details INPUT").Range("H7").copy
R2.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Set WordDoc = Nothing
Set WordApp = Nothing
Set R1 = Nothing
Set R2 = Nothing
End Sub
试试下面的方法。至于 Excel,在使用 Word 的对象模型时,最好使用底层对象,而不是选择。除非绝对需要,否则最好避免使用剪贴板。 Word 还有一个 Range
对象,这是一个很有用的 "target".
请注意,此方法将丢失 Excel sheet 中的所有格式。
如果你想引入格式,使用问题中的代码,那么你将同时引入 worksheet 结构:你将粘贴一个 table 单元格。那可能就是你认为的新line/paragraph。我包含的变体(参见三个“”)仅粘贴字体格式,没有 Excel 结构(相当于 UI 中的 PasteSpecial as RTF)。
Sub OpenWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim R1 As Object
Dim R2 As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\Users\KG\Desktop\VBA WIP\FAfile.docx")
Set R1 = WordDoc.Bookmarks("b1").Range
Set R2 = WordDoc.Bookmarks("b2").Range
WordApp.Visible = True
'Put it at the end, before "clean up" if you want to do this
'WordApp.Activate
R1.Text = Sheets("Details INPUT").Range("H4").Text
R2.Text = Sheets("Details INPUT").Range("H7").Text
'''Sheets("Details INPUT").Range("H7").copy
'''R2.PasteExcelTable False, False, True
'CutCopyMode is NOT boolean, pass it either 1 or 0 or the xl-constant value!
'''Application.CutCopyMode = xlCopy
Set R1 = Nothing
Set R2 = Nothing
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
这里有多个问题。
首先,因为您使用的是后期绑定 CreateObject("Word.Application")
,您可能没有包含对 Microsoft Word ... Object Library
的引用。但是常量 wdFormatSurroundingFormattingWithEmphasis 将不会被设置并且为 0。使用后期绑定不能使用常量名称。必须改用适当的值。
并使用 Selection.PasteAndFormat 粘贴整个 table 单元格,而不仅仅是值。根据您的描述,您只想粘贴值。
要仅粘贴值,请尝试 Selection.PasteSpecial:
...
Sheets("Details INPUT").Range("H4").Copy
R1.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
Sheets("Details INPUT").Range("H7").Copy
R2.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
...
其中 2 是 wdPasteText 的值。
如果需要来自 Excel 的格式化内容,请改用 wdPasteRTF,即 1 而不是 2。
这里是新的,一般来说 VBA。我创建了一个宏,它复制 excel 中单元格的内容并粘贴到 word 文档中的特定位置。仅供参考,我在 word 中使用书签 select 粘贴的确切位置。问题是复制的所有内容都会插入一行 and/or paragraph/carriage return。我已经找到了很多可能的解决方案,但其中 none 可行,可能是因为我在 VBA 方面缺乏经验。请帮忙!
Sub OpenWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim R1 As Object
Dim R2 As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\Users\KG\Desktop\VBA WIP\FAfile.docx")
Set R1 = WordDoc.Bookmarks("b1")
Set R2 = WordDoc.Bookmarks("b2")
WordApp.Visible = True
WordApp.Activate
Sheets("Details INPUT").Range("H4").copy
R1.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Sheets("Details INPUT").Range("H7").copy
R2.Select
WordApp.Selection.PasteAndFormat Type:=wdFormatSurroundingFormattingWithEmphasis
Application.CutCopyMode = True
Set WordDoc = Nothing
Set WordApp = Nothing
Set R1 = Nothing
Set R2 = Nothing
End Sub
试试下面的方法。至于 Excel,在使用 Word 的对象模型时,最好使用底层对象,而不是选择。除非绝对需要,否则最好避免使用剪贴板。 Word 还有一个 Range
对象,这是一个很有用的 "target".
请注意,此方法将丢失 Excel sheet 中的所有格式。
如果你想引入格式,使用问题中的代码,那么你将同时引入 worksheet 结构:你将粘贴一个 table 单元格。那可能就是你认为的新line/paragraph。我包含的变体(参见三个“”)仅粘贴字体格式,没有 Excel 结构(相当于 UI 中的 PasteSpecial as RTF)。
Sub OpenWord()
Dim WordApp As Object
Dim WordDoc As Object
Dim R1 As Object
Dim R2 As Object
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="C:\Users\KG\Desktop\VBA WIP\FAfile.docx")
Set R1 = WordDoc.Bookmarks("b1").Range
Set R2 = WordDoc.Bookmarks("b2").Range
WordApp.Visible = True
'Put it at the end, before "clean up" if you want to do this
'WordApp.Activate
R1.Text = Sheets("Details INPUT").Range("H4").Text
R2.Text = Sheets("Details INPUT").Range("H7").Text
'''Sheets("Details INPUT").Range("H7").copy
'''R2.PasteExcelTable False, False, True
'CutCopyMode is NOT boolean, pass it either 1 or 0 or the xl-constant value!
'''Application.CutCopyMode = xlCopy
Set R1 = Nothing
Set R2 = Nothing
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
这里有多个问题。
首先,因为您使用的是后期绑定 CreateObject("Word.Application")
,您可能没有包含对 Microsoft Word ... Object Library
的引用。但是常量 wdFormatSurroundingFormattingWithEmphasis 将不会被设置并且为 0。使用后期绑定不能使用常量名称。必须改用适当的值。
并使用 Selection.PasteAndFormat 粘贴整个 table 单元格,而不仅仅是值。根据您的描述,您只想粘贴值。
要仅粘贴值,请尝试 Selection.PasteSpecial:
...
Sheets("Details INPUT").Range("H4").Copy
R1.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
Sheets("Details INPUT").Range("H7").Copy
R2.Select
'WordApp.Selection.PasteAndFormat Type:= 20
WordApp.Selection.PasteSpecial DataType:=2
Application.CutCopyMode = False
...
其中 2 是 wdPasteText 的值。
如果需要来自 Excel 的格式化内容,请改用 wdPasteRTF,即 1 而不是 2。