Importing Word Document Properties from Excel File with UDFs

rhivert

New Member
Joined
Dec 9, 2010
Messages
12
Hi all,

I would like to verify / identify the formatting and properties for objects, text, paragraphs, etc. within a Word document from an Excel file (using UDFs).

The example below shows a function (UDF) I have written to identify the font name for a specific cell in an Excel document:

Function FontName(MyRef)
Application.Volatile 'Forces recalculation
FontName = MyRef.Font.Name
End Function

Thank you for your help.

R
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
UDFs below assumes that the document is already open

This should get you started

1 Add a reference to the Word Object library (in VBA under Tools \ References \ then scroll down .. etc)
ReferenceToWordObjectLibrary.jpg


2 Example PDFs
input required for this is the name of the shape
VBA Code:
Function GetShapeFontName(shp As String) As String
    On Error Resume Next
    GetShapeFontName = ActiveDocument.Shapes(shp).TextFrame.TextRange.Font.Name
End Function

input required for this is the paragrapgh number
VBA Code:
Function GetParagraphFontName(p As Integer) As String
    On Error Resume Next
    GetParagraphFontName = ActiveDocument.Paragraphs(p).Range.Font.Name
End Function

3 The worksheet
 
Upvote 0
This may be a helpful starting point to get a handle on what the Word document contains
- amend to detail whatever interests you

VBA Code:
Sub LoopWord()
'reference required to Word Object library
    On Error Resume Next
    Dim shp As Word.Shape, aList As String
    Const V = vbCr

    With ActiveDocument

        aList = .Name & V
        aList = aList & V & "Table count: " & .Tables.Count
        aList = aList & V & V & "Paragraph count: " & .Paragraphs.Count
        aList = aList & V & V & "Shapes:"

        For Each shp In .Shapes
            aList = aList & V & shp.Name
        Next shp

    End With
   
'print results to immediate window
Debug.Print aList

End Sub

Look for the output in VBA immediate window (make visible when in VBA with {CTRL} G )
- it will look something like this

TheNameOfMyFile.docx

Table count: 5

Paragraph count: 59

Shapes:
Text Box 2
Rectangle 1
Text Box 2
Text Box 2


Shape names repeat - so you may need to use shp.ID instead of shp.Name to reference shapes
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top