How to get user to easily run some VBA

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

How to get user to easily run some VBA

Post by Sundog »

Word 2010: I have a document with a title page containing a document number, and a revision number, which are entered by the user. Page 2, section 2, onward has a header that needs those two numbers.

I have crafted VBA to grab the numbers and populate the header. What are suggestions for an easy way (untrained users) to cause this macro to run after the user has entered the numbers?
Sundog

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to get user to easily run some VBA

Post by HansV »

I'd use a userform with text boxes and/or combo boxes. In the OK button of the userform, you can set the value of document variables or of custom document properties. In the document (whether in the header/footer or in the body), you can use DocVariable or DocProperty fields to display their values.

See Post 15936 for an example.
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: How to get user to easily run some VBA

Post by macropod »

IMHO, a better approach would be to assign unique Character or Paragraph Styles to those ranges and use StyleRef fields in the header to reference those Styles. No code required.
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: How to get user to easily run some VBA

Post by Sundog »

Hans, can't use a userform in this application.
macropod wrote:...assign unique Character or Paragraph Styles to those ranges and use StyleRef fields in the header to reference those Styles. No code required.
Could you expound on this a bit? Or point me to a link?
Sundog

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to get user to easily run some VBA

Post by HansV »

See for example Useful StyleRef tricks by Suzanne S. Barnhill.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: How to get user to easily run some VBA

Post by Sundog »

Can't get it to work in my situation. I need to grab this type of character string, PM123456, on p.1 section 1 in Arial 44 point, and paste PM123456 in the header on p2. section 2 as unformatted text or Arial 10 point. Tried hidden text without success. As suggested by Suzanne, I did not find it easy.
Sundog

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to get user to easily run some VBA

Post by HansV »

I have attached a small sample document. I created a character style DocNo and used it for the document number in the first paragraph, and another character style RevNo for the revision number in the second paragraph.
STYLEREF fields are used in the header of section 2 (page 2) to display the document number and revision number there.
If you edit the document number or revision number on page 1 (take care not to remove the character style), you should see the change reflected almost instantaneously in the header of page 2.
StyleRefTest.docx
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: How to get user to easily run some VBA

Post by Sundog »

Where is the DocNo character style? See attached.

Edited to add: It works like a champ, but how did you do it?!
DocNo.PNG
You do not have the required permissions to view the files attached to this post.
Last edited by Sundog on 22 Aug 2012, 23:45, edited 1 time in total.
Sundog

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to get user to easily run some VBA

Post by HansV »

I didn't add DocNo and RevNo to the QuickStyles list. They are displayed in the Styles pane though.
x1220.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: How to get user to easily run some VBA

Post by Sundog »

Ah so. I will try that on my sample doc. Don't know why I couldn't get it to work; maybe because I was trying to only grab the numbers in PM123456?
Sundog

User avatar
HansV
Administrator
Posts: 78857
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: How to get user to easily run some VBA

Post by HansV »

Sundog wrote:maybe because I was trying to only grab the numbers in PM123456?
No, that shouldn't matter.
Best wishes,
Hans

User avatar
Sundog
5StarLounger
Posts: 704
Joined: 28 Jan 2010, 22:47
Location: Alien Country (Roswell NM)

Re: How to get user to easily run some VBA

Post by Sundog »

Well, kudos to you, it works on my home-office machine. Will try at work tomorrow and will report. Thanks for the help! (Are kudos edible? Or am I thinking of kangaroos?) Edit: Or maybe kudzu.
Sundog

User avatar
StuartR
Administrator
Posts: 12655
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to get user to easily run some VBA

Post by StuartR »

Sundog wrote:Well, kudos to you...
Kudos to macropod who suggested this approach.
StuartR