Results 1 to 10 of 10
  1. #1
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442

    Retrieving Checkbox information within a text box on a word document


    Attached is an example file.

    I am parsing the text box at the top of the document to retrieve information for a document control system. I am able to parse everything I need except for the values within the checkboxes. I've attempted a number of things but all of them seem to rely on the checkboxes being in the body of the file and NOT within a text box. There's got to be a way to do this but I am striking out so far, if anyone has done this or has a suggestion/solution I'd appreciate a shove in the right direction.

    rp
    TEST_Draft.zip

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    For a starting point, can you provide code that parses the textbox?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Here it is.

    I don't believe I can make use of the 'strings' as they are in the array, I'm thinking I somehow have to read that text box a different way.

    Code:
    Dim objwa
    Dim objwd
    Dim currpath
    Dim sfile
    Dim sText, sLine, sERR, sfilename
    Dim aLineArray
    Dim sCreatedby
    Dim dCreatedDate, dReviewDate
    Dim sREVIEWLIST, sFINALREVIEWLIST
    Dim iRemindDays
    Dim ctl As Control
    Dim ffTemp As FormField
    
    
    Set objwa = CreateObject("Word.application")
    objwa.Visible = False
    currpath = CurrentProject.Path & "\documents\current\"
    currpath = Replace(currpath, "\\", "\")
    sfilename = "Test_Draft.docx"
    sfile = currpath & sfilename
    
    
    Set objwd = objwa.Documents.Open(sfile)
    
    
    For Each Shape In ActiveDocument.Shapes
        sText = Shape.TextFrame.TextRange.Text
        aLineArray = Split(sText, vbCr)
        For i = 0 To UBound(aLineArray)
            sLine = aLineArray(i)
            If InStr(sLine, "Created") Then
                sLine = Replace(sLine, vbTab, "")
                sCreatedby = Mid(sLine, InStr(sLine, ":") + 1, InStrRev(sLine, "Date") - (InStr(sLine, ":") + 1))
                dCreatedDate = Trim(Right(sLine, Len(sLine) - InStrRev(sLine, ":")))
                If Not IsDate(dCreatedDate) Then sERR = sERR & "The Created Date is not a valid date" & vbCrLf
            ElseIf InStr(sLine, "Reviewed by") Then
                sREVIEWLIST = sLine
            ElseIf InStr(sLine, "Final") Then
                sFINALREVIEWLIST = sLine
            ElseIf InStr(sLine, "remind") Then
                sLine = Replace(Replace(sLine, vbTab, ""), " ", "")
                dReviewDate = Mid(sLine, InStr(sLine, ":") + 1, InStr(sLine, "remind") - (InStr(sLine, ":") + 1))
                If Not IsDate(dReviewDate) Then sERR = sERR & "The Review date is not a valid date" & vbCrLf
                iRemindDays = Mid(sLine, InStr(sLine, "every") + 5, InStr(sLine, "day") - (InStr(sLine, "every") + 5))
                If Not IsNumeric(iRemindDays) Then sERR = sERR & "The Number of days between reminders is not valid" & vbCrLf
                If Len(sERR) = 0 Then
                    If DCount("*", "tblDocument", "[D_Name] = '" & sfilename & "' AND [D_Date] = #" & dCreatedDate & "#") > 0 Then
                        MsgBox "A file with the name: " & sfilename & vbCrLf & vbCrLf & "And a created date of: " & dCreatedDate & vbCrLf & vbCrLf & "Already exists", vbOKOnly, "ERROR Adding New File"
                        Exit Function
                    End If
                    sSQL = "INSERT INTO tblDocument (D_Name, D_Path, D_Createdby, D_Date, D_Date_Review, D_Reminder) VALUES ("
                    sSQL = sSQL & "'" & sfilename & "',"
                    sSQL = sSQL & "'" & currpath & "',"
                    sSQL = sSQL & "'" & sCreatedby & "',"
                    sSQL = sSQL & "#" & dCreatedDate & "#,"
                    sSQL = sSQL & "#" & dReviewDate & "#,"
                    sSQL = sSQL & iRemindDays & ")"
                    CurrentDb.Execute sSQL
                    sREVIEWLIST = Trim(Replace(sREVIEWLIST, vbTab, ""))
                Else
                    MsgBox "The following errors were encountered attempting to import the information in this file" & vbCrLf & vbCrLf & sERR & vbCrLf & "Please review the following document and correct any problems" & vbCrLf & vbCrLf & sfile, vbOKOnly, "ERROR Importing File Information"
                    Exit Function
                End If
            End If
        Next i
        Set objwd = Nothing
        Set objwa = Nothing
        Exit Function
    Next Shape
    
    
    End Function

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Access complained about Shape variable so I declared it. Now it chokes on TextRange.

    Got rid of Shape and this much works so far:

    MsgBox objwd.Shapes(1).TextFrame.TextRange.Text

    As you already know, the checkbox is not intact in that output.

    Did find where should be able to reference other entities within the textbox:

    objwd.Shapes(1).TextFrame.TextRange.Tables(1)

    However, checkbox doesn't show as available to TextRange. Checkbox appears to be in FormFields collection. And you're right, that doesn't seem accessible from within textbox.

    Tried adding a checkbox to main body of document and not able to reference that yet.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You overlapped some of your efforts with mine so at least I'm not missing something I shouldn't have. Just as a reference I couldn't get formfields to recognize a checkbox in the main body of a word document either so I may just tell them they've got to use a table at the top of their document instead of this text box. I haven't given up yet but I'm close to doing so.

    thanks for your effort june

    rp

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I copied your checkbox control to the body and that one did not work.

    In playing with that control properties and the checkbox from the 'legacy' controls, must have replaced it because it looks different. Now this code works to reference the checkbox on main body.

    Debug.Print objwd.FormFields("Check1").Checkbox

    However, it returns True even though the checkbox is not checked.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    AFAICT, the object type is 17 (text box) and belongs to the Shapes collection. It's not an msoOLEControlObject, and I have no idea at this point what the other shape types are save to say they're not inline shapes either.
    https://msdn.microsoft.com/en-us/VBA...eration-office
    If your erred on Shapes, try adding a reference to Word.
    Confess that I'm out of my league with Word vba; just playing around with the document and what I could find on the Word object model. plus playing with and adapting some pirated code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I already had references to Word and Excel and still did not like Shape. But finally solved by declaring it like:

    Dim Shape As Word.Shape

    And replacing ActiveDocument with objwd.

    Okay, the following compiles but triggers runtime error:

    Debug.Print Shape.TextFrame.TextRange.FormFields(1).Checkbox

    I tried to copy/paste the 'legacy' checkbox control within the textbox and that gets an error 'Cannot paste form fields into comment, ... or text boxes."

    I can set properties of the 'legacy' checkbox whereas see no way to do that with the other thing.

    So what exactly is that thing called a checkbox that is embedded into the textbox?


    Same here. Pirating everything I can find and still not handling checkbox.
    Last edited by June7; 02-13-2018 at 12:33 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    I already had references to Word and Excel and still did not like Shape.
    Maybe it's a version issue. I recently "upgraded"?? to 2016 and this works:

    Code:
    Set objwd = objwa.Documents.Open(sfile)
    
    Dim ilsh As InlineShape
     Dim sh As Shape
    Dim ob As Object
    For Each ilsh In ActiveDocument.InlineShapes
    ...
    Not sure I get the idea of putting check boxes inside of a text box. If it's to be used as a form field, I'm used to seeing them in tables (if for no other reason than to maintain some control over their placement).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I didn't even see the 2 legacy versions of checkboxes. I've tried them both (ActiveX and Form Field but everything I've read indicates the activex control is not a great idea) and now and I am experiencing the same problem june is (compiles just fine but throws an error at run time). I have suggested they move to a table at the top of their document precisely for the reasons you mentioned Micron and I think I can push them there, but I am still unable to read the contents of any checkbox I've tried. When I use the 'form field' version of the checkbox I can't even get my code to recognize it using

    Code:
    dim ff as formfield
    
    For Each ff In ActiveDocument.FormFields
        Debug.Print "HEREx"
    Next ff

    I never get anything in my debug window though there is at least one 'form field' control on the source document.

    The funny part of this is the entire header was put in a text box to make it easy to delete when the document(s) had their final revision so I'm trying to find a solution strictly for the document development process.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2017, 10:25 AM
  2. Replies: 4
    Last Post: 08-24-2015, 12:57 PM
  3. VBA to find/replace text in word document
    By Goodge12 in forum Programming
    Replies: 1
    Last Post: 07-16-2014, 01:23 PM
  4. Extract text from each word document in a file
    By mercapto in forum Programming
    Replies: 11
    Last Post: 03-12-2013, 10:29 PM
  5. Replies: 3
    Last Post: 02-22-2013, 06:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums