I have a database with word docs. The filenames are like 000001, 000002......
I have successfully written a macro that opens these documents, makes a selection until a value is found, print this selection and then close the document again and move on to the next. (see code below )
There is one thing I want to do extra, but I can't figure it out. I want the word "End" printed under this selection (on the same page). But I want to do this without making any change in the documents.
I don't want to change the documents because the value that needs to be printed with the selection comes from an userform textbox and is different every time. But if someone can help me, you can give an example with the word end
Added a picture above off what im looking for
this is my code now:
Dim i As Long, wdApp As Object, wdDoc As Object, wdRng As Object
Set wdApp = CreateObject("Word.Application")
With wdApp
.Visible = True
For i = 1 To 9
Set wdDoc = .Documents.Open("\\path\" & Format(i, "000000") & ".NET", False, True, False)
With wdDoc
Set wdRng = .Range(0, 0)
With .Range
With .Find
.Text = "ENDLIST"
.Forward = True
.MatchWholeWord = True
.MatchCase = True
.Execute
End With
If .Find.found = True Then
wdRng.End = .Duplicate.Start
wdRng.Select
wdDoc.PrintOut , Range:=1
End If
End With
.Close False
End With
Next
.Quit
End With
Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Is this possible, and if it is can someone please help me going
Found it out myself. The code below will open the word file, will add the information at the beginning of the doc, make a selection from the beginning until a word is found, print this selection and then close the word file again.
Couldn't figure out how to get it after the selection, like shown in the picture of the question. But the place didn't really matter as long as the needed information was on the paper and it looked neat.
Sub test()
Dim i As Long, wdApp As Object, wdDoc As Object, wdRng As Object, wRng As Object
Dim BEGINNING_OF_STORY
Dim MOVE_SELECTION
BEGINNING_OF_STORY = 6
MOVE_SELECTION = 0
Set wdApp = CreateObject("Word.Application")
With wdApp
.Visible = True
For i = 1 To 2
Set wdDoc = .Documents.Open("M:\" & Format(i, "000") & ".docx")
With .Selection
.Font.Bold = True
.HomeKey BEGINNING_OF_STORY, MOVE_SELECTION
.Font.Size = "25"
.TypeText ("information" & vbCrLf)
End With
With wdDoc
Set wdRng = .Range(0, 0)
With .Range
With .Find
.Text = "DEF"
.Forward = True
.MatchWholeWord = True
.MatchCase = True
.Execute
End With
If .Find.found = True Then
wdRng.End = .Duplicate.Start
wdRng.Select
wdDoc.PrintOut , Range:=1
End If
End With
.Close False
End With
Next
.Quit
End With
Set wdRng = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Fitted my own needs, hope someone else can work with this sometimes too
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.