Rajkumar_h
New Member
- Joined
- Oct 4, 2013
- Messages
- 20
Hello,
I have around 100 word documents stored in my folder, I need to open each document and find the last page and insert a new blank page and paste the excel data to it. I tried using the below code but getting stuck in Sub ProcessDocument(objWordDoc As Document). Please help..
Thanks in advance
Rajkumar
I have around 100 word documents stored in my folder, I need to open each document and find the last page and insert a new blank page and paste the excel data to it. I tried using the below code but getting stuck in Sub ProcessDocument(objWordDoc As Document). Please help..
Code:
[/COLOR][COLOR=#333333]Private mobjWordApp As Word.Application[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">'https://stackoverflow.com/questions/831458/excel-vba-to-open-multiple-word-files-in-a-loop
Sub Test()
ProcessDirectory "\\na1.ofc.loc\dfsusa\homedir\Y921737\home\Desktop\WorkMacro"
End Sub
Property Get WordApp() As Word.Application
If mobjWordApp Is Nothing Then
Set mobjWordApp = CreateObject("Word.Application")
mobjWordApp.Visible = True
End If
Set WordApp = mobjWordApp
End Property
Sub CloseWordApp()
If Not (mobjWordApp Is Nothing) Then
On Error Resume Next
mobjWordApp.Quit
Set mobjWordApp = Nothing
End If
End Sub
Function GetWordDocument(FileName As String) As Word.Document
On Error Resume Next
Set GetWordDocument = WordApp.Documents.Open(FileName)
If Err.Number = &H80010105 Then
CloseWordApp
On Error GoTo 0
Set GetWordDocument = WordApp.Documents.Open(FileName)
End If
End Function
Sub ProcessDirectory(PathName As String)
Dim fso As New FileSystemObject
Dim objFile As File
Dim objFolder As Folder
Dim objWordDoc As Object
On Error GoTo Err_Handler
Set objFolder = fso.GetFolder(PathName)
For Each objFile In objFolder.Files
If StrComp(Right(objFile.Name, 4), ".doc", vbTextCompare) = 0 Then
Set objWordDoc = GetWordDocument(objFile.Path)
' objWordDoc.Unprotect Password:="testcode" ' Need to check if it has Password?
ProcessDocument objWordDoc
objWordDoc.Close 0, 1
Set objWordDoc = Nothing
ElseIf StrComp(Right(objFile.Name, 4), ".docx", vbTextCompare) = 1 Then
Set objWordDoc = GetWordDocument(objFile.Path)
' objWordDoc.Unprotect Password:="testcode" ' Need to check if it has Password?
ProcessDocument objWordDoc
objWordDoc.Close 0, 1
Set objWordDoc = Nothing
End If
Next
Exit_Handler:
CloseWordApp
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
'Resume Next ' or as above
End Sub
Sub ProcessDocument(objWordDoc As Document)
'Dim pag As Range
'Dim docMultiple As Document
'Set docMultiple = ActiveDocument
lPageCount = ActiveDocument.Content.ComputeStatistics(wdStatisticPages) ' Getting stuck here
lPageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)
ActiveDocument.Goto What:=wdGoToPage, Which:=wdGoToNext, Name:=lPageCount
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.InsertBreak Type:=wdPageBreak
</code>[COLOR=#333333]End Sub[/COLOR][COLOR=#333333]
Thanks in advance
Rajkumar