[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS word 2007 protected form with a bookmark and macro

Posted on 2009-12-28
19
Medium Priority
?
563 Views
Last Modified: 2013-11-10
I need to build a protected from ms 2007 with a bookmark with a macro that capable to convert figure number to text inside the bookmark.
0
Comment
Question by:HKH1967
  • 10
  • 9
19 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 2000 total points
ID: 26136646
It isn't clear which part of that idea you are having difficulty with, but you could do it without a macro if the numeric text is also displayed.

Bookmark the number and then use a Ref field.If the bookmark name is bmkNumber, The field code should look like this:

{ REF bmkNumber \* CardText }

You can use Alt+F9 to toggle between displaying field codes and field results in the document.
 
0
 

Author Comment

by:HKH1967
ID: 26140156
Thanks
I have a macro to convert Arabic number figure to arabic text due to that i have to use the bookmark. what i would like is to have a macro that isnert the result in the bookmark.
I have the file if that contain the macro if any one interset?
Best regards,
Hussain
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26140508
If you already have the macro to do the conversion, then all you need is to call it, reading the text from one bookmark and plugging the result into the other bookmark.

This code assumes that your macro is a function which returns the number in words and accepts the numeric text as an argument.
Sub CallConvertMacro()
    Dim strNumber as String
    Dim strText as string

    strNumber = ActiveDocument.BookMarks("bmkNumber").Range.Text
    strText = MyCodeConverterFunction(strNumber)
    ActiveDocument.BookMarks("bmkNumberText").Range.Text = strText
End Sub
    

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:HKH1967
ID: 26157968
Dear GrahamSkan
I have tried that but I could not succeed, working with ms word 2007 is differing. Can you help me? I need to input the number in one bookmark and get the result in another bookmark.
Many thanks,
NB attached ms word 2003.
 

AraAddIn1.dot
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26158410
I now see that the field solution wasn't suitable because you need Arabic text. However, I don't think that the Word version makes much difference

I had trouble with the Macro security and couldn't get used to the right-to-left text, so I have created a new document and copied the macro code into it.
25006524.docx
0
 

Author Comment

by:HKH1967
ID: 26158539
Dear GrahamSkan
what I have understand from yr last comments that the file contain two bookmarks and macro. I found only 123456 and macro code..
Can you send them again thanks,
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26158640
Are you sure that there are no bookmarks? There is an option to show bookmark delimiters, or you can select a bookmark by name from the dialogue for inserting bookmarks.
0
 

Author Comment

by:HKH1967
ID: 26158780
GrahamSkan:
sorry, the bookmark i found them but what about the marco, I could not found any?
thansk
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26159106
Sorry, too.
I saved the document as .docx (no macros) instead of .docm and EE won't accept that, so here is a Word 2003 version
25006524.doc
0
 

Author Comment

by:HKH1967
ID: 26160790
Dear GrahamSkan
Thanks, in ms word 2007 inviorment protected document macro is working. While runing the macro i have added code to unprotect the dox. but I need to make a loop if I change the figure I need to check if the bookmark named 'strText' is exist or not, if not i need to creat it.

I have run the macro but it is deleting the bookmark 'strText', what I need is to insert the text inside

Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
    strText = sDec2Text(strNumber)
    ActiveDocument.Unprotect
    ActiveDocument.Bookmarks("bmkNumberText").Range.Text = strText
   
    ActiveDocument.Protect wdAllowOnlyFormFields

End Sub

Thanks
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26160880
I usually capture the bookmark range into a variable and use it to set the new text and to add the bookmark again.
Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    Dim rng As Range
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         LoadArrays
         strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
         strText = sDec2Text(strNumber)
         ActiveDocument.Unprotect
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
         ActiveDocument.Protect wdAllowOnlyFormFields
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
End Sub

Open in new window

0
 

Author Comment

by:HKH1967
ID: 26162515
Dear GrahamSkan

I have input the macro in the strnumber bookmark that result in the getting the text in the 'bmkNumberText' and strNumber show no figure (disappear).

What I need if you can help is that input the figure in 'bmkNumberText' bookmark (figure must show & not disapper) and get the reult in 'bmkNumberText' bookmark.


Can the following code help with getting right-to-left text:

   'Save the cursor movement settings (either logical or visual)
    lCursorMovement = Options.CursorMovement
    'Force the cursor movement to be logical if it is not
    If Options.CursorMovement = wdCursorMovementVisual Then Options.CursorMovement = wdCursorMovementLogical
    LoadArrays
   
    lRange = Selection.MoveWhile(cset:="0123456789.,¡", Count:=wdBackward)
    'Save the alignment of the paragraph representing the digits
    lParaAlignment = Selection.ParagraphFormat.Alignment
    'Force the reading order to be RTL
    Selection.ParagraphFormat.ReadingOrder = RtlPara
    'Reset the alignment to what it originally was
    Selection.ParagraphFormat.Alignment = lParaAlignment
thanks
Hussain
 
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26162612
I suggest that you set the numeric data into the bookmark range in the same way that we set the text into its bookmark.


    Dim strNumber As String
    Dim rng As Range
    strNumber = "123456" 'for example
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber 
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If

Open in new window

0
 

Author Comment

by:HKH1967
ID: 26162673
GrahamSkan:

I could not understand the last point, Can you guide me how to merge both code together?
Thanks
Hussain
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26162812
Sorry, Hussain.

I can't interpret what your snippet is trying to do. This code will insert the number, and then read it out again before converting it to text.
Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = "012345" 'for example
    If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect 'password
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
         strText = sDec2Text(strNumber)
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
    ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub

Open in new window

0
 

Author Comment

by:HKH1967
ID: 26167100

Dear GrahamSkan

I have done slit change to the macro, I have replace sDec2Text with sNum2Text. but stil I have a problem of:
1. number is being rounded up e.i. 14.50 (fifteen)
2. i need to the fraction in the following format xx/100
I appreciate your great assiatance, thanks,
Hussain


Sub CallConvertMacro()
Dim strNumber As Long
Dim strText As String
LoadArrays
strNumber = "012345" 'for example
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect 'password
End If
If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
' rng.Text = strNumber
'Add bookmark again to ensure that it exists and the new text is contained within it.
ActiveDocument.Bookmarks.Add "bmkNumber", rng
Else
MsgBox "Bookmark 'bmkNumber' is missing"
End If
If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text

strText = sNum2Text(strNumber) & " " ' *** Decimal to be add & "/100" *******************


Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
rng.Text = strText
'Add bookmark again to ensure that it exists and the new text is contained within it.
ActiveDocument.Bookmarks.Add "bmkNumberText", rng
Else
MsgBox "Bookmark 'bmkNumberText' is missing"
End If
ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26167197
I can't read the output, but I think that sDec2Text is designed to take a number with a decimal point and to pass it in parts to the sNum2Text function, so if you want to have a number like 12345.67, you would have to use sNum2Text.

If the number that has been supplied as a percentage, you can divide by 100 in the procedure.

This snippet does that, but I can't tell if the result is correct.

Sub CallConvertMacroPercent()
    Dim strNumber As String
    Dim strText As String
    LoadArrays
    strNumber = "012345" 'for example
    If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect 'password
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
         rng.Text = strNumber
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If
    If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         strNumber = CStr(Val(ActiveDocument.Bookmarks("bmkNumber").Range.Text / 100)) '<-- New
         strText = sDec2Text(strNumber)
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
    Else
        MsgBox "Bookmark 'bmkNumberText' is missing"
    End If
    ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
End Sub

Open in new window

0
 

Accepted Solution

by:
HKH1967 earned 0 total points
ID: 26182946
Dear GrahamSkan
I think I have succeed in get what I need with thanks to you, but runing the macro is taking too much time. Can you help by checking if there is loop not close?

Sub CallConvertMacro()
    Dim strNumber As String
    Dim strText As String
    Dim sWhole As String, sDecimal As String
    Dim lPosDecimal As Long
    Dim lCursorMovement As Long
    Dim lParaAlignment As Long
   
    LoadArrays
    strNumber = "012345" 'for example
 
    If ActiveDocument.ProtectionType <> wdNoProtection Then
         ActiveDocument.Unprotect 'password
    End If
   
    If ActiveDocument.Bookmarks.Exists("bmkNumber") Then
         Set rng = ActiveDocument.Bookmarks("bmkNumber").Range
   
        ' rng.Text = strNumber
         'Add bookmark again to ensure that it exists and the new text is contained within it.
         ActiveDocument.Bookmarks.Add "bmkNumber", rng
 
    Else
        MsgBox "Bookmark 'bmkNumber' is missing"
    End If
 
 If strNumber <> 0 Then
        ActiveDocument.Bookmarks("bmkNumber").Select
        Selection.MoveRight Unit:=wdCharacter, Count:=-strNumber, Extend:=wdExtend
        'ActiveDocument.Bookmarks("bmkNumber").Select
       
        lPosDecimal = InStr(1, ActiveDocument.Bookmarks("bmkNumber").Range, ".", vbTextCompare)
  Debug.Print lPosDecimal
  '   If lPosDecimal > 0 Then
           
            sWhole = Mid(ActiveDocument.Bookmarks("bmkNumber").Range, 1, lPosDecimal - 1)
               Debug.Print sWhole
            sDecimal = Mid(ActiveDocument.Bookmarks("bmkNumber").Range, lPosDecimal + 1)
        Debug.Print sDecimal
            'Remove zeros to the left of Whole and to the right of Decimal
            sWhole = sLeftRemove(sWhole, "0")
            'sDecimal = sRightRemove(sDecimal, "0")
         
           
            If InStr(sWhole, ",") Then sWhole = sFindReplace(sWhole, ",", "")
            If InStr(sWhole, "¡") Then sWhole = sFindReplace(sWhole, "¡", "")
           
            If InStr(sDecimal, ".") Then sDecimal = sFindReplace(sDecimal, ".", "")
            If InStr(sDecimal, ",") Then sDecimal = sFindReplace(sDecimal, ",", "")
            If InStr(sDecimal, "¡") Then sDecimal = sFindReplace(sDecimal, "¡", "")
           
                           
   If ActiveDocument.Bookmarks.Exists("bmkNumberText") Then
         strNumber = ActiveDocument.Bookmarks("bmkNumber").Range.Text
       Debug.Print strNumber
    If sDecimal > 0 Then
         
         strText = sNum2Text(CLng(sWhole)) & " " & sDecimal & "/100"
         Else
         
         strText = sNum2Text(CLng(sWhole))  ''''& " " & sDecimal & "/100"
       End If
          Debug.Print strText
         Set rng = ActiveDocument.Bookmarks("bmkNumberText").Range
         rng.Text = strText
           
         ActiveDocument.Bookmarks.Add "bmkNumberText", rng
     Else
         MsgBox "Bookmark 'bmkNumberText' is missing"
      End If
             ActiveDocument.Protect wdAllowOnlyFormFields, True ',password
   
           
        'Else            'Only whole number
           
        '    If InStr(sWhole, ",") Then sWhole = sFindReplace(sWhole, ",", "")
        '    If InStr(sWhole, "¡") Then sWhole = sFindReplace(sWhole, "¡", "")
           
        '    sWhole = ActiveDocument.Bookmarks("bmkNumber").Range
           
            'Remove zeros to the left of whole number
        '    sWhole = sLeftRemove(sWhole, "0")
           
           
   '         If Len(sWhole) > 9 Then
                ';MsgBox "Sorry:This addin does not support more than 9 digits for " & _
                ';"whole portion of the number", vbOKOnly, "Number to Text"
               
   '             Exit Sub
   '         End If
           
        '    strText = sNum2Text(CLng(sWhole))
       
  '      End If
   End If
     
End Sub

25006524-05-01-2010.doc
0
 

Author Comment

by:HKH1967
ID: 26609804
it work fine
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Six Sigma Control Plans
Progress

834 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