Solved

Writing a Word Macro that will open a document and make it an active document on which I can run some other Macros

Posted on 2008-06-22
5
203 Views
Last Modified: 2010-04-30
I need help writing a Macro that will open a document such as c:\Important Documents\Target.doc and make Target.doc an active document on which I will then be able to run some other Macros (named such as TypingMacro1 and TypingMacro2). These macros (TypingMacro1 and TypingMacro2) will type some sentences in the Target.doc and then Save and Close  Target.doc.
I am able to write TypingMacro1 and TypingMacro2 myself. I only need code for the Macro that will open
c:\Important Documents\Target.doc for such that I can run my other macros on it before saving and closing it. Thanks for the help.
0
Comment
Question by:FaheemAhmadGul
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21841939
You can do that in one line.

Documents.Open "c:\Important Documents\Target.doc"

But I suggest that you capture the document object, so that you can refer to it alter without having to know which is the active document.
Dim Doc as Document

Set Doc = Documents.Open("c:\Important Documents\Target.doc")

Open in new window

0
 

Author Comment

by:FaheemAhmadGul
ID: 21842043
Perhaps I did not express my problem clearly. Please see my code and the problem I explain after the code ends.
The code I have in the click event of one of the VB Forms associated with my Parent Document  Progress Writer  the document which is the active document when the code starts) is as follows.

1.      Private Sub CommandButton2_Click()
2.      Documents.Open "c:\Important Documents\Target.doc"
3.      Selection.EndKey Unit:=wdStory
4.      Selection.Find.ClearFormatting
5.      Selection.TypeParagraph
6.      Application.Run MacroName:="BoldCallibri12"
7.      Selection.TypeText Text:="John K: "
8.      Selection.TypeText Text:=Chr(11)
9.      Selection.TypeText Text:="Date of Visit: "
10.      Unload Me              `this unloads the form on which CommandButton2 was located       DateOfVisit.Show       `this loads a new form which has buttons which on clicking type       
        11.      Unload Me            `this unload the form named DateOfVisit
        12.      ProgressNotes.Show        `this loads a new form ProgressNotes
End Sub
.  
This code opens the c:\Important Documents\Target.doc" for me and Types text given in line 7. (John K) and in line 9. (Date of Visit:) in the Target.doc but after this (when forms DateofVisit and ProgressNotes) load all the typing occurs in the Parent Document (Progress Writer) with which my original form that is ProgressNotes is associated. I want c:\Important Documents\Target.doc to  remain the active document all the time while different forms in my project load and unload. All the forms basically have lots of command buttons which on being clicked type different things in the active document.
Thanks for your help.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 125 total points
ID: 21842282
When you are handling several documents, it is best to have a document object for each document.

If you stick to using a range object, you don't have to worry about which one is active.
Private Sub CommandButton2_Click()

    Dim Doc1 As Document

    Dim rng As Range

    

    Set Doc1 = Documents.Open("c:\Important Documents\Target.doc")

    Set rng = Doc1.Bookmarks("\EndOfDoc").Range

    rng.InsertParagraphAfter

    Application.Run MacroName:="BoldCallibri12"

    rng.InsertAfter "John K: " & Chr(11) & "Date of Visit: "

    Unload Me

End Sub

Open in new window

0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 31469538
This is perfect. Many thanks. Once again I am grateful. Best Wishes - Faheem
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21849379
Thanks. It is always a pleasure to help a quick learner.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now