+ Reply to Thread
Results 1 to 5 of 5

Save a public variable after document closes

  1. #1
    Registered User
    Join Date
    03-19-2015
    Location
    SC, USA
    MS-Off Ver
    2010
    Posts
    4

    Save a public variable after document closes

    Hello,

    I don't know if this is the appropriate area of the forum to post this. I know there is a VBA section for excel, but I didn't know if this would go there as it's a VBA Word question.

    I have to print out a sequentially increasing number code on some labels at work. I currently have a form with the correct layout that is coded to prompt me for the starting number and the number of pages of labels I want. I've got all that working. If I tell it to start at 1, ten it will do 1 through 30 labels on that one sheet, print, and the do 31-60. It'll repeat till it hits the number of sheets I told it to print.

    I have it set that if I don't enter a number of sheets to print, it defaults to 1 sheet. I'd like it to do the same thing if I don't input a starting number. I'd like it to default to the next number in the sequence and run the program to make the labels and print them out. The problem I have is that I don't know how to store that number after the program is shut down. The number is being pulled from a variable I have saved as a public variable called IntLastNum. I'd like to be able to have the program store the last number used in this variable, which it does, then save it and upon reopening the document, it should pull the last number and proceed from there.

    So in the instance I mentioned above, where I printed out labels 1-30 on one sheet and then did 31-60. I should be able to shut the document down, come back in a couple of weeks, leave the starting number blank and have it print labels 61-90. I read that it should save it when the program is saved and I was previously just closing the program and telling it not to save, so that the cells would be empty when I opened it back up. Instead, I have a piece of code that when the document closes, it clears the cells, sets the cursor back to the first cell, and saves the document. Thanks for the help.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Save a public variable after document closes

    Custom document property?

    https://msdn.microsoft.com/en-us/lib.../ff862806.aspx
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    03-19-2015
    Location
    SC, USA
    MS-Off Ver
    2010
    Posts
    4

    Re: Save a public variable after document closes

    Maybe, but I don't know if I'm using it right. I followed the link, copied and pasted the example code, and deleted all but the one called customer number. I changed where it says 1000 and made it equal to an integer variable in the same subroutine. It broke and gave me a run time error. It said automation error and

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Save a public variable after document closes

    I don't know if you're using it right either. Post your code.
    Last edited by shg; 06-17-2016 at 02:03 PM.

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Save a public variable after document closes

    If you use a SEQ field for the label numbering, you could use a macro like:
    Please Login or Register  to view this content.
    where your starting SEQ field is coded as:
    {SEQ Nr \r 1}
    and the SEQ fields for all the other labels are coded as:
    {SEQ Nr \n}
    As coded, the macro assumes the SEQ fields are the only fields on the labels and only the labels to be numbered have them. As each page is printed, the number in the first SEQ field is updated for the printing of the next page. When finished, the last update is saved so the document is ready for the next iteration next time it's opened.
    Last edited by macropod; 06-17-2016 at 07:38 PM.
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Public Variable and Static Variable Difference
    By zanshin777 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-18-2016, 01:58 AM
  2. [SOLVED] VBA save document to PDF with variable filename
    By wesleyu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2016, 10:56 AM
  3. Before Save Force closes
    By sgdva in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-24-2014, 12:36 PM
  4. How do I offer a 'CANCEL' when user closes the document?
    By PosseJohn in forum Word Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2012, 08:48 AM
  5. Excel closes down when trying to save a file
    By Rob.P in forum Excel General
    Replies: 0
    Last Post: 07-23-2010, 04:30 AM
  6. [SOLVED] File closes without prompting to save
    By freyabeads in forum Excel General
    Replies: 0
    Last Post: 03-01-2006, 09:10 AM
  7. [SOLVED] calculation - prompting to save when file closes
    By lpj in forum Excel General
    Replies: 0
    Last Post: 02-14-2006, 04:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1