Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    First Question:

    I work with three documents open at one time. The first document is a patient log, the second document is used to cut and paste the patient reports with page breaks to have one long continous document for E-mailing later, and the third document is where I type each individual report, saving and then exiting, etc. I have created a macro that copies and pastes the current typed report to document two, places a page break, and then switches to document one to input the patient variable information onto the patient log sheet. This macro runs fine for a while and then I get an error message after about ten uses. I believe that the problem is because I have used ActiveWindow.Previous.Activate and ActiveWindow.Next.Activate codes, as the macro seems to get confused after a while. Is there a way to define exactly where the macro should take me without using these codes? I was thinking that maybe if I created documents named log and copy that it could be written into the code telling the macro specifically where to go and perform the essential functions?

    Second Question:

    I would like to create a macro that automatically saves each report with the defined patient variable information; however, I have no clue as to what the code would be for this. I tried using ActiveDocument.SaveAs FileName:="vPatient", but all this did was actually save with vPatient instead of the stored variable information.

    Thank you for all of your help.
    Atelby

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    1. Are the three documents stored documents that you open each time, or do you start with blank documents?
    2. By putting quotes around "vPatient" you specify a literal string. Where should the variable information come from?

  3. #3
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    1. I essentially start with blank documents (the patient log is a separate macro that is called up in a blank document).

    2. The variable information is global and is stored after invoking the patient report macro. The doctor's signature macro invokes the copy/paste and document switching macro.

    Please let me know if you need more information,

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    1. If you start with blank docs, you could do something like this:

    Dim docLog As Document
    Dim docCopy As Document
    Dim docEntry As Document

    Set docLog = Documents.Add
    Set docCopy = Documents.Add
    Set docEntry = Documents.Add

    You can easily switch to a specific document:

    docCopy.Activate

    2. If vPatient is a global variable, you can save the document as follows:

    ActiveDocument.SaveAs FileName:=vPatient & ".doc"

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    On the first question, you could try using a Static variable in your macro. I've never done this, but I've read about them. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    The idea is that unlike other variables, which are forgotten once you Exit Sub or End Sub, the Static variable will retain its value for the remainder of the time that the project (presumably Normal.dot) remains in memory. Thus, you could have code like this:

    Sub CopyThisAndThat()
    Static strLogDoc as string, strAllReports as string
    if strLogDoc = vbnullstring Then 'first time through
    strAllReports = ActiveWindow.Previous.Document.FullName
    strLogDoc = ActiveWindow.Previous.Previous.Document.FullName
    End If
    ...

    The problem with ActiveWindow.Previous and the index of the Documents collection is that they seem to be based on the alphabetical order of the file names, and thus to change. You would indeed have an easier time looking in the file name for LOG or SUM (summary) than relying on these commands. You could change the above to require user input:

    Sub CopyThisAndThat()
    Static strLogDoc as string, strAllReports as string
    if strLogDoc = vbnullstring Then 'first time through
    strAllReports = ChooseOpenDoc("Which document contains all of the reports?")
    strLogDoc = ChooseOpenDoc("Which document contains the log?")
    End If
    ...

    <pre>Function ChooseOpenDoc(strInstruction As String) As String
    ' Present user an InputBox with a document list to pick from, return the FullName
    Dim strPrompt As String, strValid As String, intCounter As Integer
    strPrompt = strInstruction
    For intCounter = 1 To Documents.Count
    strPrompt = strPrompt & vbCrLf & intCounter & ": " & Documents(intCounter).Name
    strValid = strValid & CStr(intCounter)
    Next
    Do
    ChooseOpenDoc = Trim(InputBox(strPrompt, "Choose document"))
    If ChooseOpenDoc = vbNullString Then Exit Do
    If (Len(ChooseOpenDoc) = 1) And (InStr(1, strValid, ChooseOpenDoc) > 0) Then
    ChooseOpenDoc = Documents(CInt(ChooseOpenDoc)).FullName
    Exit Do
    End If
    MsgBox "Please enter a valid file number, or leave blank to skip"
    Loop
    End Function</pre>

    I just wrote this function and tried it out with five documents open. I don't know how it might behave under greater stress.

    Hope this helps.

  6. #6
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Thanks to the both of you for your input.

    Jefferson,

    You are so advanced in your code writing that I can't possibly comprehend what your macro does. If I ever feel brave enough, I may give it a try, but am a bit frightened because I don't want it affecting any of my other functions.

    Hans,

    Your code is much more on my level (not to say that you aren't a sophisticated code-writer yourself). I am guessing that in order to use your code, I would have to define and save a blank document as "log" and "copy" before the code will work?

    Thanks again to the both of you,

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Oh heck, I'm really not that advanced. But it was lazy of me not to include more comments. The idea of the function is it's completely encapsulated, and so use it you only need something like:

    Sub TestThatThing()
    msgbox chooseopendoc("Which document is the most amusing?)
    End sub

    (I just realized that if there are no documents open, you'll get an error.)

    Try the above plus the function and see how you like it.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Atelby,

    I had assumed that you start with new blank docs every time, so the macro code I posted creates three new blank documents - that is the meaning of Documents.Add. It is up to you as user to save them after working with them.

    If you have some kind of boilerplate documents that you open each time, instead of creating blank new ones, replace the Documents.Add insturcons by

    Set docLog = Documents.Open "C:WordMyLogbook.doc"
    Set docCopy = Documents.Open "C:OtherFolderCopyCat.doc"
    Set docEntry = Documents.Open "C:WordTheThirdDoc.doc"

    Substitute the actual paths and file names. Instead of opening the documents each time, you can also create new documents based on them:

    Set docLog = Documents.Add "C:WordMyLogbook.doc"
    etc.

    The difference between Open and Add is that Open opens an existing document (if you then save it, it will overwrite the original), while Add creates a new document; if you try to save it, Word will prompt you to give it a name.

  9. #9
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Jefferson,

    I will most definitely try out your code sometime in the near future. Even if I find that it doesn't fit what I am looking for, I will definitely learn something new from it that may help with future endeavors! A couple of things that make me question whether or not it will work for my particular situation is because it seems like it may append ALL files in the directory into one long document? I need one file with all of the reports in a running document and THEN I need the same files saved separately to calculate the lines typed later. Also, my report header macro has input boxes which prompt for the patient information and then store that information into global variables for later use. I don't think that I need yet another input box to call up a log macro? Until I try out the code, I won't really know until then. I do like the way your codes loop and question things, etc. These codes have always boggled my mind when I read other posts. The only time I have ever had the use for a code that looped and questioned was when I had a report header that multiple facilities shared. The code had to question which facility address to put at the top. I did not write this code, so I really didn't learn too much about it. Someday in the future....

    Hans,

    If I always keep the patient log in Document1and the page that I copy to in Document2, is there a way to write the code making the macro go specifically to Document1, input the patient information, and then go to Document2 to copy and paste the report? My patient log is a macro, which calls up the log format. The log is an ongoing form, which may be saved periodically throughout the day (I realize that I would have to cut and paste the saved log into Document1, as once it is saved, it is given a new name). At the end of each day, I print off the patient log for my files, which shows all of the patient reports that I typed for that day. The copy page is where I cut & paste all of the patient reports that I type each day, putting a hard page break in between, and I then E-mail the one large file to the facility. When I get ready to E-mail the file to the facility, it is renamed as MRIaug23 (or whatever the exam type & date). This page is also saved periodically throughout the day, as I don't always sit and do my work at one time. I would very much like to be able to specifically tell the macro where to go to perform its functions and save the documents after running each time; however, the log & copy page names change from day to day and once saved, Word changes the document name. In WordPerfect, I did not have a problem with this macro, as the documents ALWAYS remained named "Document "") and did not get renamed after saving. This also allowed me to automatically save my files after adding to them or reopening at a later time. [Side Note: I just realized that WP did rename the document after the file was saved, but using the Save and not SaveAs function automatically saved it with the predefined name.]

    Your response to the save macro brings up another question... If I automatically save the patient files with variable information, will Word question before saving over a previous file with the same name? Sometimes there are patients who have more than one chest x-ray done in the same day, which would give it the same name. Is there a way to make Word question saving a duplicate file?

    I have yet one more question regarding the save macro. I have tried it out and it works GREAT, except for when it gets to the date of service variable. I save each patient file with the patient's name (vPatient), type of exam performed (vExam), and date of service (vDOS). When I manually save, I have to take out all of the "/".
    Example: BEFORE: 08/23/03 AFTER: 082303
    I do remember that I had this same problem in WordPerfect. The solution was to use a StrTransform code. Because WordPerfect and Word are so similar in code writing, I tried playing with rewriting the WP code, but of course was not successful.
    The WP code was...""+vPatient+ vExam + StrTransform (vDOS; "/";"-")+".wpd"
    The StrTransform (vDOS "") took out the "/" and inserted "-" in their place, which WP and Word allow to be used to save with. You may ask why I need all of this info when saving each file? The reason is because when I go to bill each facility, I run a line-counting program (I get paid per line typed). Once my line-counting program is finished running, it automatically creates a log with all of the saved patient information and amount due per report typed. I then send this log to the facility when billing.

    To sum up this long post and simplify what I am asking for help with:
    1. Is there a code that I can write to take me specifically to Document1, then Document2, then back where I started from?
    2. Will Word question before overwriting a previous file with the same name?
    3. Is there a code to take out the slashes ["/"] and insert ["-"] instead?

    Thanks again for all of your help/suggestions.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    A Word macro is different from a WP macro.

    I don't think I understand enough of your explanation to be of real help, but with variables of type Document, as I described in previous replies, you can switch back and forth, and the name under which the document is saved doesn't matter. You can even copy and paste without switching documents:

    Dim rng As Range

    docLog.Content.Copy
    Set rng = docCopy.Content
    rng.Collapse Direction:=wdCollapseEnd
    rng.Paste



    The VBA equivalent of StrTransform is Replace

    vPatient & vExam & Replace(vDOS, "/","-") & ".wpd"

    Note that VBA uses commas instead of semicolons to separate the arguments. You could also use Format(vDos, "mm-dd-yyyy") to get the date in the right format.
    SaveAs will overwite an existing file, but you can present the user with the standard dialog:

    With Application.Dialogs(wdDialogFileSaveAs)
    .Name = vPatient & vExam & Replace(vDOS, "/","-") & ".wpd"
    .Show
    End With

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    > ...it seems like it may append ALL files in the directory into one long document...

    Actually, the only thing it does in the Function is display a list of the currently open documents with prefixing numbers, like this:

    1: Memo re budget.doc
    2: Letter to landlord re pets.doc
    3: New resume.doc
    4: Document6

    and present it in an InputBox. If the user enters a number from 1 to 4, the Function will return the full path and file name of the chosen file back to whatever procedure called it in the first place. The message in parentheses in the Function call will appear just above the numbered list, providing "help" to the user on what he or she is selecting.

    Once this file name is returned, you can activate the specific document using:

    Documents(complete_filename).Activate

    The original idea was that by storing this in two string variables declared as Static, that you would only need to ask the user which were the log and report summary one time, and after that, it would remember automatically.

    Anyway, I hope that helps explain how you could use it and a bit more about what it does.

  12. #12
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    This does seem pretty nifty and may come in handy in the future. For right now, I don't want the user to have to make choices, but rather that the macro know specifically where to go and perform its functions automatically. The ActiveWindow.Next.Activate and ActiveWindow.Previous.Activate codes do work, but only for a limited time, as the page numbers change. I realize that your code will do this, but it seems like an awful lot of work and code-writing to get there. I will most definitely try your code out in the future to see exactly what it does.

    Thank you again for all of your help,

  13. #13
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Hi Hans,

    I think that we're are all getting a bit confused here on what it is that I need. Apparently I am not relaying my information very clearly. Let me try again...

    None of the codes that you have been kind enough to share with me seem to be what it is that I am looking for, as I do not want to add new documents, open existing ones, or create a new document based upon an original. I am looking for a written code that will simply switch me to DOCUMENT1 to allow my already written macro to perform its function. This same macro code should then be usuable to write code to have the macro switch to DOCUMENT2, etc.

    I received information from another forum, where I was told to try
    Dim strWinName1 As String
    strWinName1=ActiveWindow.Caption 'You should be at the window when storing the caption (though you could also cycle through the windows)

    'Then to go to that particular window:
    Windows(strWinName1).Activate

    This seems like what I may be looking for, but I can't quite understand what it is that I am supposed to do here, and of course I can't get the code to work either. I will be going back to post my question to see what it is that I am doing wrong, but wanted to show you, as you will probably know exactly what the purpose of it is?

    The date code did not work for me when I tried the "Replace" and "Format" commands. I typed it out exactly as you had it, but? I am thinking that there is something just a bit off that keeps it from performing properly? Just to make sure that I understood your previous instructions clearly, I tried...
    vPatient & vExam & Replace(vDOS, "/","-") & ".doc" (Exactly as typed)
    AND later tried
    vPatient & vExam & Format(vDOS, "mm-dd-yyyy") (Exactly as typed), but neither of these codes worked. The report saved fine, but without any date inserted in the file name.

    Thanks for all of your help and for sticking with me on this.

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    I think the window-switching problem is confusing indeed. I don't feel I can help with that.

    I had assumed that vDOS had been set by you to a date before trying to use it in the file name. If you haven't, you should.

  15. #15
    Star Lounger
    Join Date
    Jun 2003
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Two macro questionsSwitching Docs/Save Macro: (Word 2000)

    Oops, thanks for pointing out my error. I double-checked and my predefined variable name wasn't DOS (date of service), but DOE for date of exam. After fixing the error, the code and save macro works perfectly! THANK YOU BUNCHES! This will be a HUGE time-saver for me.

    I guess the switch code isn't just confusing the macro? LOL

    Have a wonderful day, and thanks for all of your patience. Hopefully the switch macro will come in time.

Page 1 of 2 12 LastLast

Posting Permissions

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