【问题标题】:VBA-carriage returnsVBA-回车
【发布时间】:2018-08-02 09:35:17
【问题描述】:

这里是新的,一般是在 VBA 中。我创建了一个宏,它可以复制 excel 中单元格的内容并粘贴到 word 文档中的特定位置。仅供参考,我使用 word 中的书签来选择粘贴的确切位置。问题是复制的所有内容都会插入一行和/或段落/回车符。我找到了很多可能的解决方案,但没有一个可行,因为我在 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

【问题讨论】:

    标签: vba excel line-breaks carriage-return


    【解决方案1】:

    请尝试以下方法。对于 Excel,在使用 Word 的对象模型时,最好使用底层对象,而不是选择。除非绝对需要,否则最好避免使用剪贴板。 Word 还有一个Range 对象,这是一个有用的“目标”。

    请注意,这种方法会丢失 Excel 工作表中的所有格式。

    如果您想使用问题中的代码进行格式设置,那么您将同时引入工作表结构:您将粘贴一个表格单元格。这可能是您认为的新行/段落。我包含的变体(参见三个“”)仅粘贴字体格式,没有 Excel 结构(相当于 UI 中的 PasteSpecial 作为 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
    

    【讨论】:

    • 仅将 Excel 中的值设置为书签范围的好方法。
    【解决方案2】:

    这里有多个问题。

    起初,由于您使用的是后期绑定CreateObject("Word.Application"),因此您可能没有包含对Microsoft Word ... Object Library 的引用。但是常量wdFormatSurroundingFormattingWithEmphasis 不会被设置为0。使用后期绑定不能使用常量名。必须改为使用适当的值。

    使用Selection.PasteAndFormat,您将粘贴整个表格单元格,而不仅仅是值。根据您的描述,您只想粘贴该值。

    要粘贴值,请尝试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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      • 2019-02-09
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      相关资源
      最近更新 更多