PDA

View Full Version : Pass String to Word macro



mdmackillop
03-25-2009, 12:27 PM
I'm getting error 448, Named argument not found
when trying to pass a string to a Word macro from Excel using


With oWord
.Application.Visible = True
Set oDoc = .Documents.Add(Template:="S:\Templates\Qs\CatoBill.dot")
.Selection.Paste
.Run MacroName:="Convert", Proj:=Project
End With



'Word macro
Sub Convert(Proj As String)

ActiveDocument.BuiltInDocumentProperties(1) = Proj

End Sub

Kenneth Hobs
03-25-2009, 02:01 PM
Without seeing all of the code, I am not sure. Are you trying to paste to MSWord? Does this help any?
.ActiveDocument.Range.Paste

mdmackillop
03-25-2009, 02:07 PM
Hi Kenneth,
This is a snippet from much bigger code. All of it works and the "icing" is to set the document properties before I save. This will form part of the Convert macro in the Word Template

Kenneth Hobs
03-25-2009, 02:12 PM
Is it the Application.Run causing a problem? Try:
Convert Project
though I don't know what the value of Project was.

In the Convert routine, you may need to pass the MSWord object and prefix it to your ActiveDocument.
e.g.
'Similar to Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
'Requires Word Reference
Sub TextInBName(ByRef WDoc As Word.Document, ByVal BName As String, ByVal TextIn As String)
With WDoc
If .Bookmarks.Exists(BName) Then
Dim r As Word.Range
Set r = WDoc.Bookmarks(BName).Range
r.Text = TextIn
WDoc.Bookmarks.Add BName, r
Else
Debug.Print "Bookmark not found: " & BName
End If
End With
End Sub

mdmackillop
03-25-2009, 02:51 PM
Hi Kenneth
I've zipped up two files to show the problem.

mdmackillop
03-25-2009, 04:35 PM
It seems to be the command in the macro, rather than the parameter itself

Sub DocTest()
Dim Project As String
Dim appWd As Object
Set appWd = CreateObject("Word.Application")
appWd.Visible = True
appWd.Documents.Open Filename:="C:\AAA\Test.doc"
'This works
appWd.Run "PrintTable", "var1", "var2", "var3"
'This fails
appWd.Run "Convert", "PQ 123"

End Sub

'Word Subs
Sub PrintTable(arg1 As String, arg2 As String, arg3 As String)
Selection.MoveUp Unit:=wdLine, Count:=3
Selection.TypeText Text:=arg1
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:=arg2
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.TypeText Text:=arg3
End Sub

Sub Convert(Proj As String)
ActiveDocument.BuiltinDocumentProperties(1) = Proj
End Sub

GTO
03-26-2009, 12:36 AM
Greetings Malcom,

Other than the passing a named arg, which you already caught, I did not have a problem with either the string or variable passed by position.

Sub NewDoc()
Dim oWord As Object
Dim oDoc As Object, bolWDCreated As Boolean
Dim Project As String
Project = Sheets(1).Range("A1")
On Error Resume Next
Set oWord = GetObject(Class:="Word.Application")
If Err.Number > 0 Then
Err.Clear
Set oWord = CreateObject(Class:="Word.Application")
bolWDCreated = True
End If
On Error GoTo 0
With oWord
.Application.Visible = True

'//disregard this of course...//
'Set oDoc = .Documents.Add(Template:="C:\AAA\Test.dot")
Set oDoc = .Documents.Add(Template:=ThisWorkbook.Path & "\Test.dot")
.Run MacroName:="Macro1"

'// Both of these worked fine for me; confirmed both stepping thru and
'// fullspeed//
.Run "Convert", Project
.Run "Convert", "PQ 123"
End With
Set oWord = Nothing
Set oDoc = Nothing
End Sub

Have a great morning:thumb

Mark