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?
 
Rgonzo1971Connect With a Mentor Commented:
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.