Importing Word Document Properties from Excel File with UDFs

rhivert

New Member
Joined
Dec 9, 2010
Messages
11
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,660
Office Version
365
Platform
Windows
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,660
Office Version
365
Platform
Windows
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
 

Forum statistics

Threads
1,081,969
Messages
5,362,484
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top