word automation

dear experts,

How to assigned text into my word document with
active-x
text boxes, i got around 10 of them and
combo boxes, around 5 of them

thanks
vincent38Asked:
Who is Participating?
 
nigelroweCommented:
Incidentally, you could put the code into the user doc itself and protect the document code with a password. So even if somebody opened the doc and tried to read the code, they would have to know the password. I can't remember where this is done, somewhere in the options I think. But be careful, if you forget the password, you might be in trouble. I believe there is a way of unprotecting a document, but, for the life of me I can't remember, consequently, I don't use this feature.
0
 
nigelroweCommented:
Hi Vincent,
You could try passing the text to document variables from VB, then populating the text boxes with this data.
Or, you could open the word document from VB and populate directly the controls. Need more?
0
 
vincent38Author Commented:
Dear nigelrowe,

Need more explaination form you, if possible simple code?
Thanks
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
nigelroweCommented:
Well, document variables...

In the VBA editor of the document, select the this document editor pane and write a sub something like....


' This sub adds a new doc variable (trying to add an already existing variable will raise error
Private Sub AddDocVariable()
    Documents("pubcode.doc").Variables.Add "new_docvariable", "Some Value"
End Sub

This sub updates the values of existing doc variables
Private Sub InitDocVarValues()
    Documents("pubcode.doc").Variables("idpublication").Value = 0
    Documents("pubcode.doc").Variables("typublication").Value = 2
    Documents("pubcode.doc").Variables("generatehtml").Value = False
    Documents("pubcode.doc").Variables("docsuccess").Value = True
    Documents("pubcode.doc").Variables("display").Value = True
    Documents("pubcode.doc").Variables("savefilename").Value = "c:\ptolemy\publication\generated\generated.doc"
    Documents("pubcode.doc").Variables("sourcepath").Value = "c:\ptolemy\publication\msword\"
    Documents("pubcode.doc").Variables("targetpath").Value = "c:\ptolemy\publication\generated\"
    Documents("pubcode.doc").Variables("journalnr").Value = 0
    Documents("pubcode.doc").Variables("novolume").Value = 0
    Documents("pubcode.doc").Variables("idbasemodel").Value = 0
    Documents("pubcode.doc").Variables("dtpublication").Value = "1/1/2000"
    Documents("pubcode.doc").Variables("ConnectionString").Value = "*"
    Documents("pubcode.doc").Variables("nrpages").Value = 1
    Documents("pubcode.doc").Variables("mode").Value = 1
    Documents("pubcode.doc").Variables("idproc").Value = "0"
    Documents("pubcode.doc").Variables("idworkitem").Value = "0"
    Documents("pubcode.doc").Variables("ftpfilelist").Value = "0"
    'Documents("pubcode.doc").Variables("desclist").Value = "0"
    Documents("pubcode.doc").Variables("produceps").Value = "False"
    Documents("pubcode.doc").Variables("psftpfilelist").Value = "0"
    Selection.WholeStory
    Application.Run MacroName:="UpdateFields"
   
End Sub

when you save the document, the doc variables are saved with it, so they are persistent. You can change the values by opening word from VB and setting them before the user gets to see the document, then, in your forms page you can access these variables and fill the controls with the data contained in them...
Text1.Text = Documents("mydoc.doc").Variables("new_docvariable").Value

Need more?
0
 
inthedarkCommented:
Here is an example of how to Create a document amd print using Word.  In fact the example performs a mailmerge with a text file where the fields are delimited by pipes "|".

You need to set a project reference (in you project menu) to MS Word Library.

You can create tables and do a whole bunch of stuff.  You can find out how to do things by going into word, start recording a macro, then type/create the items you need. Stop recording the macro, then edit the macro, you can then see the code that is required to do the job.  You then have to adapt the code a little to work in VB but its not hard.

I have more comments to add if your app. is to be distributed to use different versions of Word:

Is this is the case you need to use late binding instead of using:
Dim WordApp As Word.Application
and
Set WordApp = New Word.Application

You need:
Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")

But get your app working first using ealry binding like the example below.  You only need to use late binding i your are distributing your App. to unknown users.


Untested Example:

Dim WordApp As Word.Application
Dim Doc As Word.Document
Dim DSel As Word.Selection
Dim Range As Word.Range
Dim datafile As Long
Dim fc As Integer ' field count
Dim wrd
ReDim flds(0) as string
dim Delimiter as string

' create an instance to word
Set WordApp = New Word.Application

' give word some time
DoEvents
DoEvents
DoEvents
DoEvents

' show word (optional)
WordApp.Visible = True
DoEvents

' create a new document
Set Doc = WordApp.Documents.Add ' can set a template ("C:\templates\yourtemplate.dot", False)

' but its best to setup a template and open like:
'Set Doc = WordApp.Documents.Add("C:\templates\yourtemplate.dot", False)

' create a link to the current typing area (known as the selection)
Set DSel = WordApp.Selection

' open your data file
' theformatof your data is another subject but here is an example using a pipe delimter:

delimiter="|"


datafile = FreeFile
Open "C:\MyData.txt" For Input As #datafile

Dim DataLine$

Do While Not EOF(datafile)
  Line Input #datafile, DataLine$
 
  ' turn the single line into an array of fields
  flds = Split(DataLine, Delimiter)
     
  ' The name and address are in fields
  ' fld(0), fld(1), etc.

  GoSub CreatePage
Loop

Close datafile

' save the document
Doc.SaveAs "C:\windows\Temp\test.doc"
 
WordApp.Quit
Set DSel = Nothing
Set Range = Nothing

MsgBox "Done"

Exit Sub

CreatePage:


' setup a style
DSel.Style = Doc.Styles("Normal")

' put the addresses into the page

For fc = 0 To UBound(flds)
  DSel.TypeText flds(fc) + vbCrLf
Next fc

' movedown

DSel.TypeText vbCrLf
DSel.TypeText vbCrLf
DSel.TypeText vbCrLf

DSel.TypeText "Dear Chummy," ' or even "Dear " + fdl(25) +","

DSel.TypeText vbCrLf

' type some words
DSel.TypeText "Mary had a little lamb" + vbCrLf

' now close of the data this record
' now move to the next page

' Now go to end of document
DSel.Collapse Direction:=wdCollapseEnd '0

' insert a pagebreak
DSel.InsertBreak wdPageBreak

Return
0
 
vincent38Author Commented:
Is there a way to update direcly from VB rather then VBA?.
I planed to write a active-x dll to embedded the process of updating with-in.

I tried to used

Dim ict As InlineShape
Dim i As Integer
i = 1
For Each ict In ActiveDocument.InlineShapes
    If UCase(ActiveDocument.InlineShapes(i).OLEFormat.object.Name) = UCase(ControlName) Then
            Select Case ActiveDocument.InlineShapes(i).OLEFormat.ClassType
            Case "Forms.TextBox.1", "Forms.ComboBox.1"
                ActiveDocument.InlineShapes(i).OLEFormat.object.Text = VarValue
            Case "Forms.CheckBox.1"
                ActiveDocument.InlineShapes(i).OLEFormat.object.Value = VarValue
            Case Else
            '    need to cover rest of the control
            End Select
        End If
       i = i + 1
    Next


but this method required scanning whole control in word before update, i'm not sure why
ActiveDocument.InlineShapes(wordcontrolname).text = "test" didn't work

0
 
vincent38Author Commented:
Is there a way to update direcly from VB rather then VBA?.
I planed to write a active-x dll to embedded the process of updating with-in.

I tried to used

Dim ict As InlineShape
Dim i As Integer
i = 1
For Each ict In ActiveDocument.InlineShapes
    If UCase(ActiveDocument.InlineShapes(i).OLEFormat.object.Name) = UCase(ControlName) Then
            Select Case ActiveDocument.InlineShapes(i).OLEFormat.ClassType
            Case "Forms.TextBox.1", "Forms.ComboBox.1"
                ActiveDocument.InlineShapes(i).OLEFormat.object.Text = VarValue
            Case "Forms.CheckBox.1"
                ActiveDocument.InlineShapes(i).OLEFormat.object.Value = VarValue
            Case Else
            '    need to cover rest of the control
            End Select
        End If
       i = i + 1
    Next


but this method required scanning whole control in word before update, i'm not sure why
ActiveDocument.InlineShapes(wordcontrolname).text = "test" didn't work

0
 
nigelroweCommented:
Can't you address the controls directly? Something like...

Sub SetText()
    UserForm1.TextBox1.Text = "text1"
    UserForm1.TextBox2.Text = "text2"
    UserForm1.Show
End Sub
0
 
vincent38Author Commented:
dear nigelrowe,

User requirement is to populate word document from VB form after some key value been key-in.

No modification allowed in generated word document except their administrator.

Thanks
0
 
nigelroweCommented:
I just want to get one thing clear, what are these objects you want to use? Are they the bookmark type or the VB controls type?

I ask this because there is a fundamental difference in the way they are addressed.
0
 
vincent38Author Commented:
dear nigelrowe,

sorry for late reply, too tired...

Actually the idea is to use word as a reporting format for generating form (print/archive), And the form is read only to the user and all modification should be done through VB exe. Only administrator who know the password could unlock word form and modified direcly.

1)Word form design included word vb control like textbox, combobox and etc...
2) VB project - form similar to word form with standard vb control.

the object i'm using is "word object"






0
 
vincent38Author Commented:
dear nigelrowe,

If you could give me your email,  i will email you my sample code.
0
 
nigelroweCommented:
e-mail is nrowe@sword.fr
0
 
vincent38Author Commented:
project emailed to you
0
 
nigelroweCommented:
Vincent, I think I've found what you're looking for. Your'e looking to directly address a vb control, placed on a Word document, from within a VB class, without intervention on the users part. Please correct me if I'm mistaken. This is how you do it...

Option Explicit
Private objWord As Word.Application
Private Sub Command1_Click()
    Set objWord = New Word.Application
    objWord.Visible = True
    objWord.Documents.Add "c:\doc1.doc"
    objWord.ActiveDocument.text1.Text = "added from VB"
End Sub

This is an extremely simplified version. Create a Word doc 'Doc1.doc' and place a textbox control named "text1" on it, save and close. Then open a new project in VB, place a command button on the form and paste the code above in the code pane. You'll see that it works.

Incidentally, although piloting word from a dll is perfectly possible, I should warn you that performance time can be seriously affected if you have a completed process to carry out. I carried out comparison tests on the two methods (1. All piloting code contained in classes in the document itself, 2. All piloting code in classes in a dll). The application consisted of creating a gazette based on database records (including images). The difference in the execution time for the two methods was astounding. Too produce a document of ~ 15 pages took about 15 seconds with method 1 and 2 hours with method 2. So be warned.
0
 
vincent38Author Commented:
dear nigelrowe,

I get your point, but if i implemented code contained in class in documents - i loose the security, means on top of the above coding i do logged user who did changes.

How you think, to create userform in words or dll?

2 HOURS!!! for me is not accepted, if i used word graphics textboxes the speed should be better? --- help me with some demo code i will reward you with additional 50 points


0
 
nigelroweCommented:
I said 2 hours, but it was for my project which is very complicated. It involves pulling documents out of a database, opening them in word, then pulling maybe hundreds of records out of the db, looping through them and inserting the data in the documents. I repeat, if your project is not too complicated, you can stick with the dll. If you notice performance problems, think about putting the code inside a separate document, open word as invisible, add the user document from the code doc, does all the processing, closes the code document, make word visible showing the user document only. The processing is invisible to the user. Something like...


From VB

Set objWord = New Word.Application
 objWord.Visible = False
 objWord.Documents.Add "c:\path\codedoc.doc"
 ' set the doc variables to the VB values
 objWord.ActiveDocument.Variables("text1").Value = text1.Text
objWord.ActiveDocument.Variables("text2").Value = text2.Text
 objWord.Run "codedoc.InitialiseProcess"
 ' log the user who carried out the changes


From Word
in the code doc (which you name codedoc, not the file name, the VBA object name) you have an entry sub (called from VB), and the subs/functions which carry out the processing, ...

Public Sub InitialiseProcess()
 Application.Documents.Add "c:\path\userdoc.doc"
 ProcessDoc
 Application.Documents("codedoc.doc").Close
 'If you want the user to see the document then
 Application.Visible = True
 'Else save the doc and quit
 ActiveDocument.SaveAs "somename"
 ActiveDocument.Close
End Sub

Private Sub ProcessDoc()
 ActiveDocument.Text1.Text = Application.Documents("codedoc.doc").Variables("text1").Value
ActiveDocument.Text2.Text = Application.Documents("codedoc.doc").Variables("text2").Value
...
End Sub

Please note, this is an untested example. It is a scenario intended to illustrate only.

But as I said earlier, if your dll version gives satisfactory execution times, stick with it.

Have a nice day
0
 
vincent38Author Commented:
Thanks for the help and he's your rewards
0
 
vincent38Author Commented:
Thanks for the help and have a nice day
0
 
nigelroweCommented:
You're welcome. Have fun with Word.
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.