First, let me say this is the first time I have attempted to execute code in a Word document from an Excel code module. I've gotten to this point with a good bit of searching/googling for solutions so thank you to all who post how to get things done!
The code posted below is intended to open a Word doc, find specific text, move to the next line and copy a picture from an Excel file. Since there will be multiple instances, my intent is to open the Word file, the first time the code is executed as defined by the 'chartno' variable, cycle through multiple times until all instances are copied and saved.
Everything seems to be working fine with one exception, when the code line ".Application.Selection.Paste" is executed, the picture does get pasted but the code simply stops at that point. If I manually execute the code and I step over that line of code the file does get saved as expected.
Please help and thanks in advance for your assistance.
The code posted below is intended to open a Word doc, find specific text, move to the next line and copy a picture from an Excel file. Since there will be multiple instances, my intent is to open the Word file, the first time the code is executed as defined by the 'chartno' variable, cycle through multiple times until all instances are copied and saved.
Everything seems to be working fine with one exception, when the code line ".Application.Selection.Paste" is executed, the picture does get pasted but the code simply stops at that point. If I manually execute the code and I step over that line of code the file does get saved as expected.
Please help and thanks in advance for your assistance.
VBA Code:
Option Explicit
Sub FindText()
Dim sNewFileName As String
Dim FNLngth As Integer
Dim WordFN As String
Dim iApp As Word.Application
Dim iDoc As Word.document
Dim chartno As Integer
Dim CurrCity As String
CurrCity = "New Holland" ' Manually setting this now in order to test code. Value will come from module that calls this module
' Capture Word Doc filename and trim off file extension and assign PDF file name
If chartno = 1 Then
Set iApp = CreateObject("Word.Application")
iApp.Visible = True
Set iDoc = iApp.Documents.Open("C:\Users\fbatu\Documents\aWork\Commission Program\Tyson Foods Letter Project\10-1-22 Company Letter.docx")
WordFN = iDoc.FullName
FNLngth = Len(WordFN) - 5
WordFN = Left(WordFN, FNLngth)
sNewFileName = WordFN & " - " & CurrCity & ".pdf"
' Find text in Word doc and copy Jpeg picture on next line
With iDoc
.Application.Selection.Find.Text = "The following pricing changes "
.Application.Selection.Find.Execute
.Application.Selection.EndKey Unit:=wdLine
.Application.Selection.MoveRight
.Application.Selection.Paste
.SaveAs2 Filename:=sNewFileName, FileFormat:=wdFormatPDF, AddtoRecentFiles:=False
End With
Else
With iDoc
.Application.Selection.MoveUp Unit:=wdLine, Count:=10
.Application.Selection.Find.Text = "The following pricing changes "
.Application.Selection.Find.Execute
.Application.Selection.EndKey Unit:=wdLine
.Application.Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
.Application.Selection.Delete Unit:=wdCharacter, Count:=1
.Application.Selection.Paste
.SaveAs2 Filename:=sNewFileName, FileFormat:=wdFormatPDF, AddtoRecentFiles:=False
End With
End If ' chartno = 1
End Sub