How to check word document file is empty or not from excel macro?

I want to check user provided MS word file is empty or not from excel macro.
Although i test as following example, its not work.
 Word VBA: How do I determine if the current Word document is ...
Aj PdlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You can check the characters count of the document like this to see if the document is empty...

Dim doc As Document
Set doc = ActiveDocument
If doc.Range.Characters.Count = 1 Then
    MsgBox "The document is empty.", vbInformation
End If

Open in new window

0
Aj PdlAuthor Commented:
Thanks for your answer Mr. Neeeraj.

But, its work only if document include text and picture, not working document only include shape object (eg. following image).
This document include only shape.
Thanks for helping me.
0
Rgonzo1971Commented:
Hi,

pls try
Sub macro()
    strFileToOpen = Application.GetOpenFilename(Title:="Please choose a file to open", _
    FileFilter:="Word Files *.doc* (*.doc*),")
    If strFileToOpen = False Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        Exit Sub
    Else
        Set objWord = CreateObject("Word.Application")
        Set objDoc = objWord.Documents.Open(strFileToOpen)
        objWord.Visible = True
        IsDocEmpty = True
        For Each astory In objDoc.StoryRanges
            If Len(astory.Text) > 1 Then
               IsDocEmpty = False
            End If
            On Error Resume Next
            If astory.ShapeRange.Count > 0 Then
                If Err = 0 Then
                    IsDocEmpty = False
                Else
                    On Error GoTo 0
                End If
            End If
            If IsDocEmpty = False Then Exit For
        Next
        If IsDocEmpty Then
            MsgBox "The document is empty.", vbInformation
        Else
            MsgBox "The document is not empty.", vbInformation
        End If
    End If
    objDoc.Close
    objWord.Quit
    Set objDoc = Nothing
    Set objWord = Nothing
End Sub

Open in new window

EDIT corrected code

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

MacroShadowCommented:
Test the file size:

Function IsFileEmpty (strFileName As String) As Boolean
    IsFileEmpty = FileLen(strFileName) = 0
End Function

Open in new window

0
Aj PdlAuthor Commented:
Thanks for everyone, Thanks @Rgonzo1971. Its work now. :)
0
MacroShadowCommented:
Just out of curiosity, did you try my proposed solution?
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
That's my fault. I should have asked you about the criteria for considering a document empty. Never mind, glad your issue has been resolved. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.