PDA

View Full Version : Import data from a word document into excel userform textbox



Tornado1981
10-08-2014, 01:49 PM
I'm trying to insert a value from a word document into an excel userform textbox that is already loaded ("Userform1") using VBA.

This is my code:


Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook

Set xlApp = GetObject(, "Excel.application")
Set xlWorkbook = xlApp.ActiveWorkbook

Dim nPages As Long

With ThisDocument
.Repaginate
nPages = .BuiltInDocumentProperties(wdPropertyPages)
End With

==> Here I want to insert the number of pages of my document into the excel's userform
textbox by implementing something like that

(xlWorkbook.Userform's Name.Textbox1.Text = nPages)

Set xlApp = Nothing
Set xlWorkbook = Nothing
Set xlWorksheet = Nothing
Set xlRange = Nothing

Any suggestions plz?

SamT
10-08-2014, 04:47 PM
Tornado, I deleted your other thread as it was just a duplicate of this one.

Have patience, someone will help you.

macropod
10-08-2014, 11:00 PM
If the Excel userform is already loaded, why not automate Word from Excel to get the page count that way? Pulling the data into an Excel userform would be a whole lot easier than trying to push it there from Word. To push it there, Word would have to go through a load more hoops.

snb
10-09-2014, 02:41 AM
I have to admit it isn't a very obvious way to fill a textbox in an Excel userform.
You can do it with a macro in Word & a macro in the Excel Workbook.

In Excel, add the macro M_snb in the codemodule of 'ThisWorkbook'


Sub M_snb(y)
on error resume next

UserForm1.TextBox1.Text = y
UserForm1.Show
End Sub

In Word use:


Sub M_snb()
GetObject(, "Excel.application").Run "Thisworkbook.M_snb", ThisDocument.BuiltInDocumentProperties(14)
End Sub


If the Userform1 in the Excel Workbook hasn't been loaded yet, it will; if it has been loaded, only the TextBox value will be adapted.

Tornado1981
10-09-2014, 08:08 AM
Thank you so much macropod & snb for replying.
I would like to explain what I'm trying to do here.
My excel userform is already loaded and contains a command button that when clicked, opens a blank word document.
After filling this document, and when the user click the save icon on the toolbar, a textbox in the excel's userform is to show the number of pages of the document , then the document is saved and closed.
So, is it necessary to add a macro to excel ?
Thank you in advance.

snb
10-09-2014, 08:39 AM
Did you test my suggestion ?

Tornado1981
10-09-2014, 10:27 AM
Did you test my suggestion ?

Yes I did snb .. and it works perfectly. Thank you so much!
I was just asking if there is another way to achieve that without any codes in excel.

snb
10-09-2014, 11:55 AM
If I would know one I had posted it. ;)