繁体   English   中英

如何使用excel VBA将文本复制到word中

[英]How to copy text into word using excel VBA

如何在 Excel 中使用 VBA 将特定文本粘贴到特定位置和标题的单词中。 到目前为止,我可以使用 excel 打开单词,并且使用 Excel VBA 对单词几乎没有什么了解。

  Sub Open_word()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim filepath As String

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True

    filepath = "https://sharepoint.lamrc.net/cft/edms/Documents/DDS2011.doc"
    Set wrdDoc = wrdApp.Documents.Open(filepath)
    '----------------------Downloads document----------------------


    ActiveDocument.Paragraphs(1).Range.Text = vbCrLf
    ActiveDocument.Paragraphs(1).Range.Text = vbCrLf
    ActiveDocument.Paragraphs(1).Range.Text = vbCrLf
    ActiveDocument.Paragraphs(1).Range.Text = vbCrLf

    wrdApp.ActiveDocument.Content.Delete



    '----------------------Deletes default content----------------------

    Dim strNewFolderName As String
    strNewFolderName = "New Folder " & (Day(Now())) & "_" & Month(Now()) & "_" & Year(Now)
    If Len(Dir("C:\Macro_test\" & strNewFolderName, vbDirectory)) = 0 Then
        MkDir ("C:\Macro_test\" & strNewFolderName)
    End If
    Dim PathName As String
    PathName = ("New Folder " & MonthName(Month(Now())) & " " & Year(Now))

    wrdApp.ActiveDocument.SaveAs "C:\macro_test\" & strNewFolderName & "\" & "test" + ".doc"

    '----------------------save as files----------------------


    Sheets("SCREW").Range("H1:J11").Copy
    wrdApp.Selection.Paste

    Application.CutCopyMode = False

    '------------- pastes the actual content--------------


    Set wrdApp = Nothing

    Set wrdDoc = Nothing

    MsgBox ("DONE")

    '----------------message box---------------

  End Sub'

我可以将内容复制到单词,但无法将其复制到特定位置。

请帮忙。

很抱歉将此作为答案发布:这是因为我没有足够的业力点数来发表评论。

如果您打算一直使用类似的文档,那么我实际上会创建一个带有书签的模板。然后根据该模板创建一个文档。

这将为您提供正确格式的文档,然后您所要做的就是输入一些代码,例如:

wrdDoc.Bookmarks("bmkScrewData").Range.Text = Sheets("SCREW").Range("H1:J11")

假设您有一个名为 bmkScrewData 的书签。 只需打开基于模板的文档,然后尝试将内容强制转换为空白文档,就容易了几英里。 这样您就可以完全控制您的环境。

这也意味着您可以避免在新的 Word 文档中删除所有默认的gubbins。

暂无
暂无

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

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