簡體   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