【发布时间】:2020-07-07 01:05:12
【问题描述】:
如何在 Excel 中使用 VBA 将特定文本粘贴到特定位置和标题的 word 中。到目前为止,我可以使用 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'
我可以将内容复制到 word 但无法将其复制到特定位置。
请帮忙。
【问题讨论】:
-
四处看看。以前也有人问过类似的问题。喜欢here
-
请具体说明“特定位置”。您的要求不明确。
-
您可能还想查看您的代码在做什么。我在你的任何'ActiveDocument.Paragraphs(1).Range.Text = vbCrLf' 行中都看不到任何点,因为你跟随那些'wrdApp.ActiveDocument.Content.Delete',这也让我想知道你为什么'打开一个现有文档只是为了删除它的所有内容...