Solved

run subroutine in a different document

Posted on 2014-01-06
8
270 Views
Last Modified: 2014-01-13
In Consultation Report.dot, I have a subroutine:
    Public Sub FaxCopyList2(Optional strFileName As String = "")
    ...
I would like to call FaxCopyList2 from another document, Visit Summary.doc:
    FaxCopyList2 ActiveDocument.FullName

I tried:
    Application.Run "FaxCopyList2", ActiveDocument.FullName
but I get: "Object doesn't support this property or method."
The subroutine name FaxCopyList2 is unique to Consultation Report.dot and the document opened using Consultation Report.dot will have different names.

MS Word 2003

How do I do this? TIA
0
Comment
Question by:thenelson
  • 4
  • 2
  • 2
8 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39759249
Hi,

I suppose your code is in Module1

Application.Run "'Consultation Report.dot'!Module1.FaxCopyList2" , ActiveDocument.FullName

Open in new window

Regards
0
 
LVL 39

Author Comment

by:thenelson
ID: 39759298
I tried:
   Application.Run "'Consultation Report.dot'!Module1.FaxCopyList2" , ActiveDocument.FullName

The code is in "NewMacros" so I tried:
   Application.Run "'Consultation Report.dot'!NewMacros.FaxCopyList2", ActiveDocument.FullName

The name of the document created by Consultation Report.dot is '20140106 Progress Report John Doe.doc' so I also tried:
   Application.Run "'20140106 Progress Report John Doe.doc'!NewMacros.FaxCopyList2", ActiveDocument.FullName

All resulted in  "Object doesn't support this property or method."
0
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39759793
I think application.run expects Variants as arguments rather than strings  - though it doesn't seem to care if the proc/function is local.

If that doesn't help - is there a reason why you can't put the target procedure in the template for visit summary.doc as well?

Reg
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39759955
Hi,

I was able to run a similar code with only the Name of the Sub because it is unique, but only when the dot file was the active document.

Could it be a solution to your problem?

Regards
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 39

Author Comment

by:thenelson
ID: 39762656
In vba help for Application.Run is the statement "If you specify the document name, your code can only run macros in documents related to the current context— not just any macro in any document."

So I tried setting a reference to Consultation Report.dot in the other document and then running:
   Application.Run "'Consultation Report.dot'!NewMacros.FaxCopyList2", ActiveDocument.FullName
I still got "Object doesn't support this property or method."

 Rgonzo1971,
>I was able to run a similar code with only the Name of the Sub because it is unique, but only when the dot file was the active document.
I tried that but it didn't work possibly because the dot file is not active document. I suppose I could have code in the second document to make Consultation Report.dot active but then I would have to pass the name of the file to the second document. If I am going to pass data to the second document, I might as well do what I am planning below.

Reg,

>If that doesn't help - is there a reason why you can't put the target procedure in the template for visit summary.doc as well?

The target procedure (FaxCopyList2) gets data from the document so to place a copy of FaxCopyList2 in the second document, I will need to pass that data from Consultation Report.dot to the second document. I have previously discovered I can pass data between two open documents using
   ActiveDocument.Variables("VariableName")
So I guess I will do that: pass the data (a string of names and fax numbers) to the second document and run a version of FaxCopyList2 in the second document.  It would be easier to just call FaxCopyList2 in Consultation Report.dot from the second document so I'll leave this question open in case someone else knows how to do it while I work the passing data method.
0
 
LVL 39

Author Comment

by:thenelson
ID: 39762769
I tried the suggestions at:
   http://windowssecrets.com/forums/showthread.php/72715-Application-run-with-variables-(Word-XP)
but still no help.  If I try the code:
   Application.Run "'Consultation Report.dot'!Module1.FaxCopyList2"
without the argument, I get "Unable to run the specified macro" instead of "Object doesn't support this property or method." So I think your comment about "Variants as arguments rather than strings" is probably accurate if I can get Application.Run to work.
0
 
LVL 39

Author Closing Comment

by:thenelson
ID: 39764824
>If that doesn't help - is there a reason why you can't put the target procedure in the template for visit summary.doc as well?

That's what I ended up doing.

Thanks!
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39776196
thanks for following up - sorry I wasn't around to help but glad you got it sorted
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

Suggested Solutions

Title # Comments Views Activity
MS Office 2013 for purchase 3 56
Moving SharePoint 3.0 role to differen server 7 47
Word 2016 - Comments on one page contracted 2 45
Help with Word VBA class module 4 38
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

920 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

15 Experts available now in Live!

Get 1:1 Help Now