Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2015, 11:23 AM
Marrick13 Marrick13 is offline Array to iterate through variables and trap blank variables Windows XP Array to iterate through variables and trap blank variables Office 2010 32bit
Competent Performer
Array to iterate through variables and trap blank variables
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default Array to iterate through variables and trap blank variables

I am trying to write a macro that copies a document and then deletes sections of text in the copy that are bookmarked. The code works fine if I preset the bookmark names in an array statement, but now I am converting the code to a userform that has four checkboxes. Checking one of the three removes one bookmarked section of text and checking the 4th removes all of the sections defined by the first 3 bookmarks. (Therefore, if the checks the 4th box, the others become unchecked and the 4th is unchecked if he checks any of the first 3.)

I’ve set the array as follows, with the variables becoming the bookmark name when their checkbox values are true:

BkmarkName = Array(Cond1, Cond2, Cond3, Cond4)



However, unchecked boxes mean some of the “Cond” variables are blank, and this causes an error in the code that deletes the bookmark text. I tried an error trap. Which works sometimes but not always. I tried a statement that builds the array text to include only those variables with a length of greater than 1 but the macro doesn’t read it as an array. There must be a way to accomplish what I’m trying to do but I’m coming up with blanks. Any help is appreciated – code is attached.
Attached Files
File Type: txt Print_Exclusions.txt (992 Bytes, 11 views)
Reply With Quote
  #2  
Old 07-31-2015, 08:40 PM
gmayor's Avatar
gmayor gmayor is offline Array to iterate through variables and trap blank variables Windows 7 64bit Array to iterate through variables and trap blank variables Office 2010 32bit
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

Are you over-thinking this? Clearly your macro does not give the whole picture, but I would have thought

Code:
    Set myCopy = Documents.Add(ActiveDocument.FullName)
    With frmPrint
        On Error Resume Next
        Select Case True
            Case .chk1.Value
                myCopy.Bookmarks("hire").Range.Delete
            Case .chk2.Value
                myCopy.Bookmarks("void").Range.Delete
            Case .chk3.Value
                myCopy.Bookmarks("bright").Range.Delete
            Case .chk4.Value
                myCopy.Bookmarks("load").Range.Delete
        End Select
    End With
simpler?
__________________
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
  #3  
Old 08-01-2015, 01:34 PM
Marrick13 Marrick13 is offline Array to iterate through variables and trap blank variables Windows XP Array to iterate through variables and trap blank variables Office 2010 32bit
Competent Performer
Array to iterate through variables and trap blank variables
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

Yes, much! I got locked into select case being only for testing conditions of one variable and didn't realize I could use it as a boolean test. I was over-thinking it. Thanks, Graham, for helping me "under-think" it.
Reply With Quote
  #4  
Old 08-03-2015, 06:17 AM
Marrick13 Marrick13 is offline Array to iterate through variables and trap blank variables Windows XP Array to iterate through variables and trap blank variables Office 2010 32bit
Competent Performer
Array to iterate through variables and trap blank variables
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

On further testing, I found that as soon as the code encountered a true condition when checking the checkboxes, it stopped looking at the other tests in the select case statement. I had to change them to individual if statements to make the above code work properly.
Reply With Quote
  #5  
Old 08-04-2015, 03:49 AM
Guessed's Avatar
Guessed Guessed is offline Array to iterate through variables and trap blank variables Windows 7 32bit Array to iterate through variables and trap blank variables Office 2010 32bit
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Since the checkboxes are not mutually exclusive, I would use a function
Code:
Sub aTest()
  Set myCopy = Documents.Add(ActiveDocument.FullName)
  ProcessCheckbox frmPrint.chk1, "hire", myCopy
  ProcessCheckbox frmPrint.chk2, "void", myCopy
  ProcessCheckbox frmPrint.chk3, "bright", myCopy
  ProcessCheckbox frmPrint.chk4, "load", myCopy
End Sub

Function ProcessCheckbox(aCtl As Control, sBkmk As String, myCopy As Document)
  If aCtl.Value = True Then
    If myCopy.Bookmarks.Exists(sBkmk) Then
      myCopy.Bookmarks(sBkmk).Range.Delete
    End If
  End If
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #6  
Old 08-04-2015, 06:19 AM
Marrick13 Marrick13 is offline Array to iterate through variables and trap blank variables Windows XP Array to iterate through variables and trap blank variables Office 2010 32bit
Competent Performer
Array to iterate through variables and trap blank variables
 
Join Date: Jun 2006
Posts: 102
Marrick13 will become famous soon enough
Default

This looks better. Thanks much, Andrew!
Reply With Quote
Reply

Tags
array, bookmark, variable

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
can word: make variables, find appropriate pages, fill out pages with variables, print only those 20GT Word VBA 1 10-15-2014 09:48 PM
Array to iterate through variables and trap blank variables Document Variables vs CustomDocumentProperties Cosmo Word VBA 2 08-11-2014 01:35 PM
Onenote equations and variables debun OneNote 0 04-15-2014 10:43 AM
Find and replace using variables jago25_98 Word 1 01-31-2012 04:30 AM
Array to iterate through variables and trap blank variables Nesting Variables SET, LISTNUM, and REF Wyskers Word 1 11-13-2011 05:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:59 AM.


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