Hi!
I'm think I'm having trouble closing my word documents properly.
I say 'think' because I don't really know what's causing my problem.
There is a folder called 'PDF' which contains a variable amount of PDF files;
each one of these PDF files contain a single unique e-mail adress.
Desired outcome:
- I open my word document
- It lists all the E-mail adresses from the PDF files in the 'PDF' folder.
Current outcome:
- When it works (about 60% of the time):
it leaves about 5 active word processes which I can see in taskmanager.
- When it doesn't work:
It freezes and I see numerous active word processes in taskmanager
(Way more than the amount of PDF files in the folder)
All help is greatly appreciated!
This is my code:
(Option Explicit)
Private Sub Document_Open() 'Upon opening the document
'Update the Bookmark
RangeMyBookmark "output", "Please wait..."
'Define the locations
Dim PDFLocation As String
Dim TempLocation As String
PDFLocation = "C:\Users\Wim\Downloads\PDF\"
TempLocation = CStr(CreateObject("WScript.Shell").specialfolders("Desktop") & "\")
'-------------CONVERT PDF FILES TO WORD DOCUMENTS---------------------------------------
Dim file As Variant 'Setup file path variable
file = Dir(PDFLocation & "*.pdf") 'Set the path to the first PDF in the folder
Do While (file <> "") 'Open PDF, Save as DOCX, Close Document, Go to Next PDF
ChangeFileOpenDirectory PDFLocation 'Set the Directory
'Open the PDF file
Documents.Open FileName:=file, ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto, XMLTransform:=""
ChangeFileOpenDirectory TempLocation 'Set the Directory
'Save as DOCX file
ActiveDocument.SaveAs2 FileName:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, CompatibilityMode:=15
'Close Document, set Path to next PDF
ActiveDocument.Close
file = Dir
Loop
'-------------ADD DOCX FILES TO ARRAY: files()-------------------------------------------
'Set the path to the first DOCX in the temp folder
file = ""
file = Dir(TempLocation & "*.docx")
'CREATE THE ARRAY
Dim files() As String
'SETUP 'i' VARIABLE FOR LOOPING
Dim i As Integer
i = 0
Do While (file <> "") 'LOOP TO FIND THE NUMBER OF TEXT FILES
i = i + 1
file = Dir
Loop
'DEFINE SIZE OF THE ARRAY ACCORDINGLY
ReDim files((i - 1))
'Set the path to the first DOCX in the temp folder
file = ""
file = Dir(TempLocation & "*.docx")
i = 0 'RESET LOOPING VAR
Do While (file <> "") 'LOOP TO POPULATE THE ARRAY
files(i) = CStr(file)
file = Dir
i = i + 1
Loop
'-------------SCRAPE MAIL ADRESSESS FROM: files()---------------------------------------------------
'ADJUST 'i' FOR CORRECT NUMBER OF LOOPS
i = CInt(UBound(files))
'EMPTY 'file', IT WILL NOW BECOME THE MAIL ADRESS
file = ""
'CREATE OBJECTS NEEDED
Dim WApp As Object, WDoc As Object
'CREATE VARIABLE FOR THE FINAL OUTPUT
Dim mails As String
mails = "E-Mail adressess:" & vbNewLine
'LET'S LOOP!
Dim x As Integer
For x = 0 To i
'Open the first Docx File
Set WApp = CreateObject("Word.Application")
WApp.Visible = False
Set WDoc = WApp.Documents.Open(TempLocation & CStr(files(x)))
'Scrape it for the address
WApp.Selection.HomeKey Unit:=6
WApp.Selection.Find.ClearFormatting
WApp.Selection.Find.Execute "E-mailadres"
WApp.Selection.MoveRight Unit:=wdCell, Count:=1, Extend:=wdMove
file = CStr(WApp.Selection)
'Quit the document
WDoc.Close
WApp.Quit
'Empty the objects
Set WDoc = Nothing
Set WApp = Nothing
'Add the address to the final output
mails = mails & file & vbNewLine
Next x
'-------------CLEANUP! DELETE THE DOCX FILES FROM THE TEMP FOLDER!----------------------------------
For x = 0 To i
SetAttr TempLocation & CStr(files(x)), vbNormal
Kill TempLocation & CStr(files(x))
Next x
'-------------CHANGE BOOKMARK TO THE FINAL OUTPUT! :) ---------------------------------------------------
RangeMyBookmark "output", mails
End Sub
Sub to update the Word Bookmark:
Private Sub RangeMyBookmark(sBm As String, sCtl As String)
Dim oRange As Word.Range
With ActiveDocument
Set oRange = .Bookmarks(sBm).Range
oRange.Text = sCtl
.Bookmarks.Add sBm, oRange
Set oRange = Nothing
End With
End Sub
- Jeremy