Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2019, 04:59 AM
Keybraker Keybraker is offline How can I replace FormFields with text Windows 7 64bit How can I replace FormFields with text Office 2013
Novice
How can I replace FormFields with text
 
Join Date: May 2019
Posts: 1
Keybraker is on a distinguished road
Default How can I replace FormFields with text

I want to replace every ActiveDocument.FormFields with text, but everything I try is not working .



I am currently looping though ActiveDocument.FormFields.Count and if it is a checkboox I want it to replace the checkbox with the value in text - "0" or "1".


Code:
Sub Test()

    Dim strCheckBoxSlct As String
    Dim strCheckBoxName As String
    Dim strCheckBoxValue As String
    
    For i = 1 To ActiveDocument.FormFields.Count
        If ActiveDocument.FormFields(i).CheckBox Then
        
            strCheckBoxName = ActiveDocument.FormFields(i).Name
            strCheckBoxValue = ActiveDocument.FormFields(i).CheckBox.Value
            strCheckBoxSlct = ActiveDocument.FormFields(i).Range
            
            'ActiveDocument.FormFields(i).Delete
               
            If strCheckBoxValue = True Then
                ' write 0
            Else
                ' write 1
            End If
            
        End If
    Next
    
End Sub
Reply With Quote
  #2  
Old 05-05-2019, 05:11 AM
eduzs eduzs is offline How can I replace FormFields with text Windows 10 How can I replace FormFields with text Office 2010 32bit
Expert
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Try this in a disposable copy of your document (backup the original file):


Code:
Sub Test()

    Dim strCheckBoxSlct As String
    Dim strCheckBoxName As String
    Dim strCheckBoxValue As String
    Dim i As Integer
    
    For i = 1 To ActiveDocument.FormFields.Count
        If ActiveDocument.FormFields(1).CheckBox Then
            strCheckBoxValue = ActiveDocument.FormFields(1).CheckBox.Value
            ActiveDocument.FormFields(1).Select
            If strCheckBoxValue = True Then
                Selection.TypeText "0"
            Else
                Selection.TypeText "1"
            End If
        End If
    Next
    
End Sub
Reply With Quote
  #3  
Old 05-05-2019, 05:14 AM
gmayor's Avatar
gmayor gmayor is offline How can I replace FormFields with text Windows 10 How can I replace FormFields with text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

To replace the formfield checkboxes with text 1 or 0 then

Code:
Sub Test()

Dim rngCheckBoxSlct As Range
Dim strCheckBoxName As String
Dim bCheckBoxValue As Boolean
Dim oFld As FormField

    If Not ActiveDocument.ProtectionType = wdNoProtection Then
        ActiveDocument.Unprotect Password:=""
    End If

    For Each oFld In ActiveDocument.FormFields
        If oFld.Type = wdFieldFormCheckBox Then
            strCheckBoxName = oFld.Name
            bCheckBoxValue = oFld.CheckBox.value
            Set rngCheckBoxSlct = oFld.Range
            If bCheckBoxValue = True Then
                rngCheckBoxSlct.Text = "1"
            Else
                rngCheckBoxSlct.Text = "0"
            End If
        End If
    Next oFld
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 05-05-2019, 08:25 AM
gmaxey gmaxey is online now How can I replace FormFields with text Windows 10 How can I replace FormFields with text Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I didn't test those and I suppose they work, but I don't see the point of strCheckBoxName or bCheckBoxValue or rngCheckBoxSlct??


Code:
Sub Test()
Dim lngIndex As Long
Dim oFld As FormField
  If Not ActiveDocument.ProtectionType = wdNoProtection Then
    ActiveDocument.Unprotect Password:=""
  End If
  For lngIndex = ActiveDocument.FormFields.Count To 1 Step -1
    Set oFld = ActiveDocument.FormFields(lngIndex)
      If oFld.Type = wdFieldFormCheckBox Then
        If oFld.CheckBox.Value = True Then
          oFld.Range.Text = "1"
        Else
          oFld.Range.Text = "0"
        End If
      Else
        oFld.Range.Text = oFld.Result
      End If
  Next
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-05-2019, 08:51 PM
gmayor's Avatar
gmayor gmayor is offline How can I replace FormFields with text Windows 10 How can I replace FormFields with text Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by gmaxey View Post
I didn't test those and I suppose they work, but I don't see the point of strCheckBoxName or bCheckBoxValue or rngCheckBoxSlct??
They are unnecessary but included to demonstrate the uses of variables the OP had attempted to use. Sledgehammers to crack a nut.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
checkbox, formfields

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue Using FormFields.Result boCash Word VBA 0 04-04-2017 12:54 PM
Updating/Locking FormFields Maskot Word VBA 4 06-24-2015 04:40 AM
Macro to find coloured text and replace with form-field/formtext containing that text tarktran Word VBA 1 11-26-2014 08:12 AM
How can I replace FormFields with text Mailmerge and Formfields Liosis Mail Merge 4 03-07-2014 12:56 PM
Renaming Word Formfields: string too long error silverspr Word VBA 7 01-22-2013 06:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:06 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft