我有一個電子表格,我想打開兩個 Word 檔案,做一堆東西,然后將一個的所有內容添加到另一個的末尾。我已經設法弄清楚如何做一堆事情,但對于我的生活,我似乎無法將一個內容復制到另一個結尾(我認為這是最簡單的部分)。
我的代碼:
Sub Redate_OUT()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = True
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Settings")
Dim pdf_path As String
Dim word_path As String
Dim Updated_path As String
pdf_path = sh.Range("E4").Value
Updated_path = sh.Range("E5").Value
word_path = "\\filestorage\cie\Operations\Results Team\Enquiries About Results\1.Series Folders\June 2022\4. Letters\OUT\Redater\Temporary Folder (Word)"
Dim fso As New FileSystemObject
Dim fo As Folder
Dim f As File
Dim MonthNo As Long
Dim MonthType As String
Dim Check As Boolean
Set fo = fso.GetFolder(pdf_path)
Dim wa As Object
Dim doc As Object
Dim FinalWording As Object
Set wa = CreateObject("word.application")
wa.Visible = True
Dim file_Count As Integer
Set FinalWording = wa.Documents.Open("\\filestorage\cie\Operations\Results Team\Enquiries About Results\1.Series Folders\June 2022\4. Letters\OUT\Redater\Final Wording.docx")
For Each f In fo.Files
Application.StatusBar = "Converting - " & file_Count 1 & "/" & fo.Files.Count
Set doc = wa.Documents.Open(f.Path)
doc.SaveAs2 (word_path & "\" & Replace(f.Name, ".pdf", ".docx"))
doc.Close False
Set doc = wa.Documents.Open(word_path & "\" & Replace(f.Name, ".pdf", ".docx"))
For MonthNo = 12 To 1 Step -1
MonthType = MonthName(MonthNo)
With doc.Content.Find
.Text = "?? " & MonthType & " 2022"
.Replacement.Text = Format(Date, "dd") & " " & MonthName(Format(Date, "mm")) & " " & Format(Date, "yyyy")
.MatchWildcards = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
If .Found = True Then
Check = True
GoTo Done
End If
End With
Next
Done:
If Check = True Then
With doc.Content.Find
.Text = "If you believe we have not arrived at this outcome properly, * Enquiry About Results Team"
.Replacement.Text = ""
.MatchWildcards = True
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
FinalWording.Content.WholeStory 'Select whole document
Selection.Copy 'Copy your selection
Documents(doc.Name).Activate 'Activate the other document
Selection.EndKey wdStory 'Move to end of document
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
doc.ExportAsFixedFormat OutputFileName:=Updated_path & "/" & f.Name, ExportFormat:=wdExportFormatPDF, Range:=2
doc.Close
End If
file_Count = file_Count 1
Next
FinalWording.Close False
wa.Quit
Kill word_path & "\" & "*.docx"
MsgBox "All OUT Letters have been updated", vbInformation
Application.StatusBar = ""
End Sub
我的主要困難是:
FinalWording.Content.WholeStory 'Select whole document
Selection.Copy 'Copy your selection
Documents(doc.Name).Activate 'Activate the other document
Selection.EndKey wdStory 'Move to end of document
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
我收到一個錯誤(物件不支持此屬性或方法):
Selection.EndKey wdStory 'Move to end of document
我也不相信 FinalWording 檔案的內容實際上是被復制的,因為當我嘗試在該行代碼運行后手動粘貼它時,沒有任何反應。
附帶說明一下,在將 PDF 保存為 word 檔案后,我一直在關閉它并再次打開以使用變數(doc)。由于我不需要保存 word 檔案,如果有更簡單的方法可以做到這一點而無需關閉和打開它,我將不勝感激。
非常感謝。
uj5u.com熱心網友回復:
您可以替換整個塊:
FinalWording.Content.WholeStory 'Select whole document
Selection.Copy 'Copy your selection
Documents(doc.Name).Activate 'Activate the other document
Selection.EndKey wdStory 'Move to end of document
Selection.PasteAndFormat wdPasteDefault 'Pastes in the content
和:
doc.Characters.Last.FormattedText = FinalWording.Content.FormattedText
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/503719.html