Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fill in Word Document form fields - VBA in QM
#1
Hi All,
I came across some sample VBA code for updating a Word form field:
http://www.devhut.net/2010/09/17/vba-wor...rm-fields/

I was trying to do it in QM but unsuccessfully:

Code:
Copy      Help
VARIANT* filepath  =  "C:\SampleDoc.docx"
typelib Word {00020905-0000-0000-C000-000000000046} 8.5
;_s.expandpath(filepath)
Word.Application oApp._create
Word.Document oDoc = oApp.documents.open(filepath)
oApp.Visible = "true"
oDoc.FormFields("PtNameField").Result = "John Doe"
oDoc.Close
;



I had first defined filepath as a string str but I received this error from QM

Quote:Error in Macro: expected VARIANT*
tip: maybe you can use operator & (address) or + (type cast) or @ (string to UTF-16).

So I changed str to VARIANT* which threw an exception but seemed to work when I changed it to just VARIANT without the asterisk.

It then tried to open the document successfully but the next line didn't seem to be the right syntax

Code:
Copy      Help
oDoc.FormFields("PtNameField").Result = "John Doe"

FormFields seems to be a read only property and doesn't take the .Result member.

Any thoughts on how to proceed further with setting the text of that field?

Thanks!!!!,
S
#2
I have gotten a little farther:


Code:
Copy      Help
VARIANT  filepath  =  "C:\Pick the date.docx"
VARIANT s
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application oApp._create
Word.Document oDoc = oApp.documents.open(filepath s s)
oApp.Visible = "true"
out oDoc.FormFields.Count
oDoc.Close
oApp.Quit
;


this will open the doc in non-locked mode but doesn't show any form field count even when I am using a document made from a template with lots of fields.
Until some fields are recongized then the actions on FormFields index/bookmark can't occur.


When I try this:

Code:
Copy      Help
VARIANT  filepath  =  "C:\Pick the date2.docx"
VARIANT s
typelib Word {00020905-0000-0000-C000-000000000046} 8.0
Word.Application oApp._create
Word.Document oDoc = oApp.documents.open(filepath s s)
oApp.Visible = "true"
out oDoc.FormFields.Count
VARIANT t = "TestField"
oDoc.FormFields.Item(t).Result = "John Doe"

I get the result

Error (RT) in Macro: 0x800A1735,
The requested member of the collection does not exist



I think I am getting closer but can't figure out why the VB doesn't recognize the fields.

Thanks for any thoughts,
S
#3
Don't know, same in vbscript, shows 0.
Macro Macro2247
Code:
Copy      Help
;/exe 1
str s=
;Set oApp=GetObject(,"Word.Application")
;Set oDoc = oApp.ActiveDocument
;MsgBox oDoc.FormFields.Count
VbsExec s
#4
I am still a big confused about the differences between vbscript and vba (and not very good at either!).
BUT, I did get this script working as a VBA macro enabled within the document template itself:

Code:
Copy      Help
Sub FieldInserter()
    Dim cc As ContentControl
    Open "C:\Name.txt" For Input As #1
    Input #1, ShortText
    Close #1
    NameString = ShortText

    Open "C:\Date.txt" For Input As #2
    Input #2, ShortText
    Close #2
    DateString = ShortText

    For Each cc In ActiveDocument.ContentControls
        If cc.Title = "NameField" Then
            cc.Range.Text = NameString
        End If
        If cc.Title = "DateField" Then
            cc.Range.Text = DateString
        End If
    Next cc
End Sub

I would rather drive this from QM so I don't have to do a screen GUI-driving multi-sep process e.g. run doc filepath, wait for doc, activate doc, send key 'AF8, Select Macro in Word macro combo box, key 'Y to select, print doc, close doc, etc.
Much easier and safer if I can just use QM to drive steps through VB (even better with doc set to visible = false).

If I try to run the above script using VBsExec, I get errors like this:

Quote:Error (RT) in Macro44: Expected end of statement
VBScript(2,8): Dim cc As ContentControl.

which I think reflects the difference between vba and vb?

Thanks for any additional thoughts,
S


Forum Jump:


Users browsing this thread: 1 Guest(s)