[英]How to copy text into word using excel VBA
How to paste a particular text into word at a particular location and header as well using VBA in excel.如何在 Excel 中使用 VBA 将特定文本粘贴到特定位置和标题的单词中。 As of now, I can open the word using excel and I have got few things on to word using 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'
I'm able to copy the conten to word but not able to copy it to particular location.我可以将内容复制到单词,但无法将其复制到特定位置。
Please help.请帮忙。
Sorry for posting this as an answer: this is because I don't have enough Karma Points to make this a comment.很抱歉将此作为答案发布:这是因为我没有足够的业力点数来发表评论。
If you are going to use a similar document all of the time then I would actually create a template with bookmarks in. Then create a document based on that template.如果您打算一直使用类似的文档,那么我实际上会创建一个带有书签的模板。然后根据该模板创建一个文档。
This then gives you a document of the right format and then all you have to do is to shove in some code such as:这将为您提供正确格式的文档,然后您所要做的就是输入一些代码,例如:
wrdDoc.Bookmarks("bmkScrewData").Range.Text = Sheets("SCREW").Range("H1:J11")
Assuming that you have a bookmark called bmkScrewData.假设您有一个名为 bmkScrewData 的书签。 It's miles easier just to open a document based on a template then try to force stuff into a blank document.
只需打开基于模板的文档,然后尝试将内容强制转换为空白文档,就容易了几英里。 This way you can have your environment completely under your control.
这样您就可以完全控制您的环境。
It also means that you can avoid all the deletion of the default gubbins in the new Word document.这也意味着您可以避免在新的 Word 文档中删除所有默认的gubbins。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.