Consulting

Results 1 to 5 of 5

Thread: Field/Vba question :S easy i think?

  1. #1

    Field/Vba question :S easy i think?

    so i want to make it so when i load a template i create, the date is displayed as follows:

    Week of Monday Feb 26 - Friday March 2

    The code itself i think i can manage to create but i'm having trouble figuring out how to make anything close to what i'm trying to do. I can do it in a field (date) but i don't think i can calculate what i'm trying to do using just a field. I'm not particularly sure what the functions are with vba, i don't know if it can do what i'm trying to do or if there's another way to do it. Can someone push me in the right direction?

    The Document_Open() event either doesn't seem to be firing on load or it's just not capable of doing what i want. I'm trying for example to put a label on a template word doc and then reload it with vba script that simply changes the caption and i'm having no luck. Any ideas? thanks a bunch.

    ~Froz

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Hi Froz,
    Welcome to VBAX
    Give this a try. You might need to play around with it a little but the principle is there.
    [vba]Private Sub Document_New()
    Dim Diff As Long, dt1 As Date, dt2 As Date, dt As String
    Diff = 7 - Weekday(Now)
    dt1 = Now + Diff - 5
    dt2 = Now + Diff - 1
    dt = "Week of " & Format(dt1, "dddd MMM d") & " - " & Format(dt2, "dddd MMM d")
    ActiveDocument.Bookmarks("Test").Range = dt
    End Sub

    [/vba]
    Last edited by mdmackillop; 02-27-2007 at 12:33 PM. Reason: dt1 corrected
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I believe Malcolm handled your date question, so i will respond to the other.
    The Document_Open() event either doesn't seem to be firing on load or it's just not capable of doing what i want. I'm trying for example to put a label on a template word doc and then reload it with vba script that simply changes the caption and i'm having no luck.
    Please clarify precisely what you are doing with the template. Templates are cloned using File > New. Are you doing this? If you are NOT, then you are not using the template properly.

    If you ARE, then Document_Open will not be the correct event to use. File > New does not open the template. Document_New is what you use when you are properly using a template and you want to execute code when you clone the new document.

    You are going to have to explain what you mean by "label on a template word doc". I do not know what you are talking about. What does "reload" mean?

  4. #4
    um, here's what i want to do, i may completely be doing it wrong. I created a template file because i thought that's what you had to make in order to have vba scripting work properly (if i'm wrong correct me).

    Quote:
    "You are going to have to explain what you mean by "label on a template word doc". I do not know what you are talking about. What does "reload" mean?"

    I put a label on the template file i created, just in the white part of the document, dragged it over and put it there. Then went into the code, put the "Document_Open()" event in there so that whenever the file was opened the even/code would run. I think i'm doing it all wrong though, if you could help that'd be great thanks!

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hmmmm. You need to work on your descriptive text more.

    VBA code will execute whenever, and where ever, it is in scope.

    You still did not explain what you mean by "template". A template is a Word file with a .DOT extension, rather than a .DOC extension.

    ANY Word file (either .DOT or .DOC) can execute VBA code.

    Label. Do you mean an ActiveX label control? If so, you have to state so. We can not read minds.

    "Just in the white part of the document". Hmmmm, does that mean you have blue parts? Red parts? Green parts? What the heck does that mean?

    "Dragged it over and put it there". Oh....yes, now that I am looking over your shoulder...could you shuffle over a little more so I can see the screen better? Ah, thanks...yes, now I can see where "there" is.

    You put Document_Open in a label code? Huh? Document_Open IS a procedure. You do not put an event procedure into something.

    Document_Open is a procedure in the ThisDocument module. It has nothing to do with a label. Putting text into a label is usually done in the ThisDocument module.

    If you want to change the text in a label (assuming you ARE talking about an ActiveX label control), write a procedure in the ThisDocument, and run it.
    [vba]Private Sub Document_Open()
    Label1.Caption = "This is the opening text (Caption) " & _
    "of Label1."
    End Sub


    Sub ChangeLabelText()
    Label1.Caption = "This is CHANGED text (Caption) " & _
    "of Label1."
    End Sub[/vba]When the document opens, the Document_Open event fires, and the label caption is written.

    The procedure ChangeLabelText can be fired by any call to it.

    I will reiterate though, a template (.DOT) will NOT fire Document_Open unless it IS opened. Properly done, a template will NOT be opened except by the person putting it together. Used properly, when the template is cloned into a new document, you should use Document_New to execute initial code.

Posting Permissions

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