Excel에서 명명 된 셀을 찾고 Word에있는 식별자를 기반으로 Word에 붙여 넣는 VBA 스크립트가 있습니다. 나는 식별자를 찾기 위해 RegEx
을 사용한다.VBA Selection.PasteAndFormat 줄 바꿈을 추가하십시오.
내가 겪고있는 문제는 값을 (정확하게) 붙여 넣을 때마다 다음 줄로 이동하도록 "Enter 키를 누릅니다"입니다. 그렇게해서는 안됩니다. 여기
스크립트입니다 : 언제나처럼Dim objWord, objWordDoc, RegEx, objExcel, objWorkbook, content, texts, text, Text_Name
Set RegEx = CreateObject("VBScript.RegExp")
Set objWord = CreateObject("Word.Application")
Set objExcel = CreateObject("Excel.Application")
Set objWordDoc = objWord.Documents.Open("D:\Performance Review Template Rev1.docx", False, True)
Set objWorkbook = objExcel.Workbooks.Open("D:\Template Rev1.xlsm", False, True)
'The entire content of the Word Document
Set content = objWord.ActiveDocument.Content
'The Regular Expression in terms of finding the short code within the document
'Explanation
'-----------
'\[# == Escaped [# characters to indicate that the start of the search needs to be an [#
'(.*?) == The forward seach in a non greedy way that is also the return group
'\] == Escaped ] character that signals the end of the search term
RegEx.Pattern = "\[#(.*?)\]"
RegEx.Global = True
Set texts = RegEx.Execute(content)
Dim Found
For Each text In texts
Set content = objWord.ActiveDocument.Content
'Find the TextName that is in the short code. The Submatches property returns
'the value of the inner return group, whereas the .Value property only returns
'the value of the short code with the [!xxx] added
Text_Name = text.submatches(0)
Dim xName, xText
Found = False
'Search for the text through all the Named Cells in the Excel file
objExcel.Range(Text_Name).Copy
With content.Find
.MatchWholeWord = True
.Text = text.Value
.Execute
If .Found = True Then
Found = True
content.PasteAndFormat 20
End If
End With
If Found = False Then
MsgBox "Did not find Named Cell!"
End If
With content.Find
.Text = text.Value
.Execute
If .Found = True Then
objWord.Selection.Range.Delete
End If
End With
Next
MsgBox "Completed named cells"
objWord.ActiveDocument.Close
objWord.Application.Quit
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
, 어떤 도움 항상 감사합니다.
빠른 수정 - 나는 시도 붙여 넣기 작업 – Absinthe
후 Selection.TypeBackspace. 그것은 작동하지 않습니다. 'content.PasteAndFormat 20' – Rijnhardt
두 번 시도해보십시오. 거기에 레이아웃 마커가있을 수 있습니다. Literally Selection.TypeBackspace를 누른 다음 Selection.TypeBackspace를 다시 누릅니다. – Absinthe