• Variables 2010

    Author
    Topic
    #481609

    Can anyone tell me if variables work differently from 2003 to 2010.

    I am having problems with 1 issue where I have developed a macro for moving specific pages backwards and forwards. Staff create reports following assessments at clients and if there aere issues noticed raise corrective action reports (CARs). They insert the CARs by clicking a button under Add Ins, which locates the template and inserts at a specific point in the report. The CARs are numbered using the SEQ field. If multiple Cars are inserted, the macro always inserts the CARs after the last one.
    Occasionally staff wish to reorder the CARs to enable this I have created another macro where they are asked which CAR (number) they wish to move and after which CAR they wish to move it to. 2 variables are created, CARToMove and MoveTo.
    In Word 2003 this has worked fine for some 7 or so years. Since the introduction of Win7 and Office 2010 to CARToMove works fine but the MoveTo variable doesn’t appear to be recognised anymore!

    Code

    Code:
    Private Sub cmdOK_Click()
    ‘Modified 23 November 2011 by Phil Carter
    ‘Modified to prevent opening in multipage view and remove extra line feed
    ‘when cutting page to move
        Application.ScreenUpdating = False
        
        strGoToCARNum = txtCARNum
        strMoveAfterCARNum = txtMoveTo
        
        intGoToCARNum = Val(txtCARNum)
        intMoveAfterCARNum = Val(txtMoveTo)
        
            ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
        
        Selection.HomeKey Unit:=wdStory
        Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=(intGoToCARNum), _
        Name:=”SEQ”
        Selection.MoveUp Unit:=wdLine, Count:=3
    ‘    Selection.Delete Unit:=wdCharacter, Count:=1
        Selection.Find.ClearFormatting
        Selection.Extend
        
        With Selection.Find
            .Text = “^m”
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
            If .Found Then
                Selection.Cut
            End If
        End With
        
    ‘    Selection.InsertBreak Type:=wdPageBreak
        If intMoveAfterCARNum = 0 Then
            Selection.GoTo What:=wdGoToBookmark, Name:=”CAR1″
            Selection.Find.ClearFormatting
            With Selection.Find
                .Text = “^m”
                .Replacement.Text = “”
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.MoveRight Unit:=wdCharacter, Count:=1
        
            Selection.Paste
        Else
            Selection.HomeKey Unit:=wdStory
            Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=(intMoveAfterCARNum), _
            Name:=”SEQ”
            
            Selection.MoveUp Unit:=wdLine, Count:=3
            Selection.HomeKey Unit:=wdLine
            
            Selection.Find.ClearFormatting
            
            With Selection.Find
                .Text = “^m”
                .Forward = True
                .Wrap = wdFindAsk
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute
                If .Found Then
                    Selection.MoveRight Unit:=wdCharacter, Count:=1
                End If
            End With
            
            Selection.Paste
        End If
        
        Update
        
        Unload frmShiftCAR
    End Sub
    Viewing 24 reply threads
    Author
    Replies
    • #1320079

      Hi Phil,

      I’m not aware of any particular change. However, your code seems inefficient. Try something along the lines of:

      Code:
      Private Sub cmdOK_Click()
      Application.ScreenUpdating = False
      Dim RngOld As Range, RngNew As Range
      Application.ScreenUpdating = False
      intGoToCARNum = Val(txtCARNum)
      intMoveAfterCARNum = Val(txtMoveTo)
      With ActiveDocument
        If .Sections.Count  intGoToCARNum Then intGoToCARNum = .Sections.Count
        If intMoveAfterCARNum >= .Sections.Count Then intMoveAfterCARNum = intMoveAfterCARNum - 1
        If intMoveAfterCARNum = intGoToCARNum Then Exit Sub
        If intMoveAfterCARNum < 0 Then intMoveAfterCARNum = 0
        Set RngOld = .Sections(intGoToCARNum).Range
        Set RngNew = .Sections(intMoveAfterCARNum + 1).Range
        RngNew.Collapse wdCollapseStart
        RngOld.Cut
        RngNew.Paste
        .Fields.Update
      End With
      Set RngOld = Nothing: Set RngNew = Nothing
      Application.ScreenUpdating = True
      Unload frmShiftCAR
      End Sub

      Note: In the above I'm guessing a bit as to what values some of your variables pass.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1320453

        Paul hi

        I note in your reply you are working with sections. In my code I search for a value set in the Sequence [SEQ] field, hence the .Fields.Update line to remiber the moved pages. when I amend the macro using your code I get the following error at the Set RngOld = .Sections… line

        30125-MoveMacro

    • #1320460

      Hi Phil,

      I mis-read your code – interpreted ^m as a Section break, whereas it’s actually a manual page break. I post back with some revised code later – unless you want to change your page breaks to Section breaks …

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1320461

        Hi Paul thanks again

        I think I’ll leave it at page breaks as sections make give problems with moving the pages and page numbering etc.

        Look forward to your code

    • #1320476

      Hi Phil,

      Try:

      Code:
      Private Sub cmdOK_Click()
      Application.ScreenUpdating = False
      Dim RngOld As Range, RngNew As Range, i As Long
      Application.ScreenUpdating = False
      intFromCARNum = Val(txtCARNum)
      intToCARNum = Val(txtMoveTo)
      With ActiveDocument
        i = .Range.ComputeStatistics(wdStatisticPages)
        If i = 1 Then Exit Sub
        If intFromCARNum > i Then Exit Sub
        If intFromCARNum  i Then Exit Sub
        If intToCARNum < 1 Then Exit Sub
        If intToCARNum = intFromCARNum Then Exit Sub
        If intToCARNum = i Or intFromCARNum = i Then .Range.InsertAfter Chr(12)
        Set RngNew = .GoTo(What:=wdGoToPage, Name:=intToCARNum)
        Set RngOld = .GoTo(What:=wdGoToPage, Name:=intFromCARNum)
        Set RngOld = RngOld.GoTo(What:=wdGoToBookmark, Name:="page")
        RngNew.Collapse wdCollapseEnd
        RngOld.Cut
        RngNew.Paste
        If intToCARNum = i Or intFromCARNum = i Then
          While .Range.Characters.Last.Previous < Chr(32)
            .Range.Characters.Last.Previous.Delete
          Wend
        End If
        .Fields.Update
      End With
      Set RngOld = Nothing: Set RngNew = Nothing
      Application.ScreenUpdating = True
      Unload frmShiftCAR
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1320638

      Paul thanks for your input

      I can’t seem to get it work as required. Not sure I understand how (why) you areusing the ComputerStatistics object.

      I have created a document, attached, that explains how the macro was designed and previousely worked. I was thinking (sometime thorugh the night, as you do!) maybe it is a Win 7 addition that is causing the problem.

    • #1320745

      Hi Phil,

      The macro works on the premise that each CAR occupies its own page. That’s why it works with page ranges. Is that premise wrong?

      The ComputerStatistics is used to get the number of pages in the document so that, via the destination CAR#, the user can’t specify an invalid destination.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1320955

      Paul thnaks again

      Confused me a bit as you had renamed some variables and not defined them!

      The logic appears confused. Firstly Itried to move CAR #4 to position #2 but the macro moved CAR #1 to be the first page of the report. I then tried to move CAR #! to be #3 and it moved the covering letter to page #3.

      I’ve attached the dummy report report I have been usi nf ofr testin g. It has 4 CARs inserted. Also included is the CAR oage.

      In the past we have had problems with staff deleting or modifying the Normal.dot so we have locked this down by creating our own version, ianznorm.dotm, which is installed as the startup template and contains all the macros required to generate the reports. In Office 2010 this file resides in D:(C:)UsersPublicTemplatesStarup. I tried to upload the dotm file but his type seems to be blocked

    • #1321051

      Hi Phil,

      Confused me a bit as you had renamed some variables and not defined them!

      Au contraire, the only undefined variables were those you already had – I assumed any required definitions for them would have been somewhere else in your existing code.

      The logic appears confused. Firstly Itried to move CAR #4 to position #2 but the macro moved CAR #1 to be the first page of the report. I then tried to move CAR #! to be #3 and it moved the covering letter to page #3.

      The logic was OK, it’s just that it didn’t allow for your front matter or back matter, which I wasn’t aware of. Now that I know about it, it’s easily accomodated and the code can be simplified in some respects. Try the following (the only undefined variables now are for your existing ‘txtCARNum’ and ‘txtMoveTo’):

      Code:
      Application.ScreenUpdating = False
      Dim i As Long, LngFromCAR As Long, LngToCAR As Long
      Dim LngFrnt As Long, LngBk As Long, RngOld As Range, RngNew As Range
      '# Pages for front matter & back matter, respectively
      LngFrnt = 4: LngBk = 1
      LngFromCAR = CLng(txtCARNum)
      LngToCAR = CLng(txtMoveTo)
      With ActiveDocument
        'Validate
        i = .Range.ComputeStatistics(wdStatisticPages) - LngFrnt - LngBk
        If i  i Then Exit Sub
        If LngFromCAR  i Then Exit Sub
        If LngToCAR < 1 Then Exit Sub
        If LngToCAR = LngFromCAR Then Exit Sub
        'Define from & to ranges
        Set RngNew = .GoTo(What:=wdGoToPage, Name:=LngToCAR + LngFrnt - 1)
        Set RngOld = .GoTo(What:=wdGoToPage, Name:=LngFromCAR + LngFrnt - 1)
        Set RngOld = RngOld.GoTo(What:=wdGoToBookmark, Name:="page")
        'Re-order the ranges
        RngOld.Cut
        RngNew.Paste
        .Fields.Update
      End With
      Set RngOld = Nothing: Set RngNew = Nothing
      Application.ScreenUpdating = True

      Note: As I still haven't seen your userform, I still don't know what the user is meant to input for the 'txtMoveTo' variable. I've assumed it's the CAR# before which to relocate the CAR # being moved. If it's supposed to be the CAR# after which to relocate the CAR # being moved, change 'LngToCAR = CLng(txtMoveTo)' to 'LngToCAR = CLng(txtMoveTo) + 1'

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1321081

      Thanks Paul

      Seems to have fixed the problem

      The user form
      30163-MoveCAR

      Now uses the code you created

    • #1321090

      Hi Phil,

      Your userform implies the second CAR# is being replaced. That’s not what the code does. If replacement is required, different code is needed. Alternatively, if the first CAR# is meant to be be moved before/after the 2nd CAR# (which is retained), the prompt should be changed.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1321103

      Paul sorry

      Yes I will change the prompt as it is intended that the first CAR takes the “position” of but does not replace the second CAR. i.e. CAR #2 of 4, move to become CAR #4 of 4

    • #1322072

      Paul hi again

      In your latest code you have specified; LngFrnt = 4: LngBk = 1. I am not sure what these do but seem to indcate the number of pages in the document. The reports can range from a basic 4 pages up to 20 plus.

      I tested on the attached and tried the following;
      move CAR 2 to CAR 3 = no change
      move CAR1 to CAR 3 = moved CAR1 to CAR 2
      move CAR 3 to CAR 1 = OK

      I have attached the sanitised report I tried with 4 CARs inserted for you to play with

    • #1322132

      Hi Phil,

      This would be much easier to test, etc, if you posted a copy with the form & code.

      FWIW, ‘LngFrnt = 4’ tells the macro there are four pages before the first CAR and ‘LngBk = 1’ tells the macro there is one page after the last CAR. I’d have thought that was obvious from the preceding code comment:

      Code:
      '# Pages for front matter & back matter, respectively

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1322195

      Paul hi

      As I stated in the last both LngFrnt and LngBk can be variable depending on the outcome of the evaluation.

      As a bare minimum a report with 2 or 3 CARS could be as follows;

        [*]P1 – Letter
        [*]P2 – Report front page
        [*]P3 – Organisation details
        [*]P4 – 10 Report detail including overview and comment on each clause of the standard being evaluated against
        [*]P11 – 14 Inserted CARs
        [*]P15 – Recommendations page

        The form
        30223-MoveCARForm

        The code is what we are working on

    • #1322268

      As I stated in the last both LngFrnt and LngBk can be variable depending on the outcome of the evaluation.

      As a bare minimum a report with 2 or 3 CARS could be as follows;

      [*]P1 – Letter
      [*]P2 – Report front page
      [*]P3 – Organisation details
      [*]P4 – 10 Report detail including overview and comment on each clause of the standard being evaluated against
      [*]P11 – 14 Inserted CARs
      [*]P15 – Recommendations page

      What you’ve described is a document with four pages of front matter and one page of back matter, between which is a variable number of pages. That’s what I’ve coded for. If the number of front matter or back matter pages changes, please say so. Also, your ‘form’ is just an image. What I need for testing a document containing the userform & code, not a post with an image of the form.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1322275

      Paul hi

      The reports will always have a constant first 3 pages. In my example the CARs would be inserted in a position variable between pages 4 and 11 so not sure how that would fit.

      The back matter always begins with the “Recommendations” page and may vary from 1 to n.

      I have attached the form to a blank document together with the InsertCAR macro if you wish to test this.

    • #1322277

      Hi Phil,

      See attached. I’ve modified the code so that it doesn’t care how many pages the document has (it now does everything using the SEQ fields as the foundation), plus I’ve added some extra error-checking to the input fields to ensure only digits are input. I’ve also added a minor tweak to allow a CAR to be moved to the end, which I think you couldn’t do before.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1322496

      Paul hi

      Thanks for the new code. When I first tested it seemed to work great. However, when I copied the macro to our global template, where it should reside, it didn’t seem to want to work.
      I created a document using one of our report templates from or database, inserted 4 CARs then tried to move them via the user form input. On clicking OK nothing happened, even clicking the Cancel button nothing happened.

    • #1322564

      Hi Phil,

      The useform is now initialized via the UserForm_Initialize sub, which sets the LngCARs variable and populates the note about how you can relocate a CAR after the last existing CAR.

      As the Cancel sub is yours (ie it was already there with your form), that suggests you’ve maybe copied the code into a module other than that attached to the ‘frmShiftCAR’ form. Try deleting the ‘frmShiftCAR’ form from your template, then simply dragging the corresponding one from the document attached to my post into your document.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1322711

      Paul hi

      I deleted the old frmShiftCAR and dragged your new one to the template. However, the macro still will not work.

      I am not sure the UserForm_Initialize sub is functioning correctly. While the form is initialized and the note is populated the numbering of the CARs is not.

      30245-NewMoveCAR

      Cancel function works OK.

    • #1322776

      Hi Phil,

      You appear to be working with a .dot or .dotm template with which the form doesn’t get copied to the documents based on it. In that case, change ‘ThisDocument’ to ‘ActiveDocument’.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1322823

      Paul

      Your right we are using .dotm. Changed to ActiveDocument, recognises the CARs as numbers correctly on the UserForm but still doesn’t move the CARs. UserForm just blinks with the OK button highlighted.

    • #1322831

      hi Phil,

      Did you change both ‘ThisDocument’ references? If you’ve done that and the code still doesn’t work, I’ll need to see the template.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1323205

        Paul hi

        How do I send you the template when the lounge doesn’t seem to allow attaching that type of file?

        PS; Yes I did change both instances of This Document

        • #1323214

          You could zip the file and attach that.

          Cheers,
          Paul Edstein
          [Fmr MS MVP - Word]

    • #1323217

      Paul hi

      I have included a report template, the blank CAR page and the group template that has your FrmShiftCAR macro included.

      We push the group template out to users via GP to the Word startup folder, UsersPublicstartup. The report is generated using data from our CRM SQL database, the CAR form is located on all computers in the UsersPublicTempaltes folder.

      All the computers are partioned to include a D: and we use the D: for all group templates and data.

    • #1323395

      Hi Phil,

      Once I attached your IANZCAR document to your ‘ianznorm0203.dotm’ template, then added some more CAR pages to it, I had no trouble with the macro.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1323508

        Paul hi

        Did you insert the CAR using the Addin from the ribbon?

        This inserts each CAR before the Recommendations page hence the ned to identify the position of each one.

    • #1323531

      Hi Phil,

      No, I didn’t do that. I simply opened the IANZCAR document, attached the ‘ianznorm0203.dotm’ template then ran the MoveCAR macro via Alt-F8. I can’t see why that should make any difference, however, as the form’s code is completely self-contained.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1323681

        Paul hi

        We have always run the macros from the global template. However, I tried your method and works fine except when moving a CAR after the last one. I am having it tested by one of our power users and have instructed him, if he needs to move a CAR after the last, to move the last one forward.

        I am heading off on a weeks leave now but will be in touch after the 19 March

    Viewing 24 reply threads
    Reply To: Variables 2010

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: