Solved

word automation

Posted on 2001-07-16
20
340 Views
Last Modified: 2007-11-27
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
0
Comment
Question by:vincent38
  • 10
  • 9
20 Comments
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6288777
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
 

Author Comment

by:vincent38
ID: 6288796
Dear nigelrowe,

Need more explaination form you, if possible simple code?
Thanks
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6288878
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
 
LVL 17

Expert Comment

by:inthedark
ID: 6288910
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
 

Author Comment

by:vincent38
ID: 6288930
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
 

Author Comment

by:vincent38
ID: 6288938
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
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6289022
Can't you address the controls directly? Something like...

Sub SetText()
    UserForm1.TextBox1.Text = "text1"
    UserForm1.TextBox2.Text = "text2"
    UserForm1.Show
End Sub
0
 

Author Comment

by:vincent38
ID: 6289070
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
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6289256
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
 

Author Comment

by:vincent38
ID: 6291895
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:vincent38
ID: 6292122
dear nigelrowe,

If you could give me your email,  i will email you my sample code.
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6292869
e-mail is nrowe@sword.fr
0
 

Author Comment

by:vincent38
ID: 6292911
project emailed to you
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6293386
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
 

Author Comment

by:vincent38
ID: 6296728
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
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6297329
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
 
LVL 3

Accepted Solution

by:
nigelrowe earned 150 total points
ID: 6297337
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
 

Author Comment

by:vincent38
ID: 6297404
Thanks for the help and he's your rewards
0
 

Author Comment

by:vincent38
ID: 6297408
Thanks for the help and have a nice day
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6297543
You're welcome. Have fun with Word.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now