Would there be someone who could help me with a problem I am having trying to pass an Excel variable over to a Microsoft Word document?
I have an Excel document that contains entered data and I have a Word document that is setup as a mail merge document and VBA code. Currently, I enter data into the Excel spreadsheet and I run the Word document. The VBA in the Word document prompts me to enter a row number and it then pulls data from that row and merges. I have two such Word documents like this I use. One to generate a ticket and the other to generate an email.
In order for this to be a little more automated I would like to do is to change this to use an intersect so that when an X is placed in a cell in a specific column this triggers the code to launch the Word document. I'd like to pass the active row number to Word as a variable so that I can get rid of the message box prompt system I'm using currently.
I've researched this quite a bit today online and the closest I've come so far is this code:
This almost works. However, the "MailMergeDisplayinNewDoc", StrRecordNumber syntax is generating the error message "Run-time error '450' Wrong number of arguments or invalid property assignment" and I'm not entirely sure why. If I remove the attempt to pass variable/data the error goes away and the Word document launches. However, the variable data isn't passed in any way.
I'm sure I'm missing something in my code.
Any help would be greatly appreciated! Thanks much!
Matt
I have an Excel document that contains entered data and I have a Word document that is setup as a mail merge document and VBA code. Currently, I enter data into the Excel spreadsheet and I run the Word document. The VBA in the Word document prompts me to enter a row number and it then pulls data from that row and merges. I have two such Word documents like this I use. One to generate a ticket and the other to generate an email.
In order for this to be a little more automated I would like to do is to change this to use an intersect so that when an X is placed in a cell in a specific column this triggers the code to launch the Word document. I'd like to pass the active row number to Word as a variable so that I can get rid of the message box prompt system I'm using currently.
I've researched this quite a bit today online and the closest I've come so far is this code:
Code:
Public StrRecordNumber As String
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
'When an "X" Checkbox Has Been Placed in a Column V Cell, Add Date/Time Timestamp
Set WatchRange = Range("V" & ActiveCell.Row & ":" & "V" & ActiveCell.Row & "")
Set IntersectRange = Intersect(Target, WatchRange)
If Not (IntersectRange Is Nothing) And (Target = "x") Then
Cells(ActiveCell.Row, 22).Value = "X"
End If
If Not (IntersectRange Is Nothing) And (Target = "X") Then
Cells(ActiveCell.Row, 22).Value = Cells(ActiveCell.Row, 22).Value & vbCrLf & " (" & Now & ")"
'Launch "Template For Computer Repair Ticket in SolarWinds Mail Merge.docm" Mail Merge Document
Dim WordApp
Dim WordDoc
StrRecordNumber = ActiveCell.Row
Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(Filename:="\\srvr\tech\Netbook and Laptop Check-In-Out Log\Outlook 2010 Templates For Items Going To Repair or Ready to Be Picked Up\Sent Into Repair Templates\Template For Computer Repair Ticket in SolarWinds Mail Merge Auto.docm", ReadOnly:=True)
WordApp.Visible = True
WordApp.Application.Run "MailMergeDisplayinNewDoc", StrRecordNumber
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End If
This almost works. However, the "MailMergeDisplayinNewDoc", StrRecordNumber syntax is generating the error message "Run-time error '450' Wrong number of arguments or invalid property assignment" and I'm not entirely sure why. If I remove the attempt to pass variable/data the error goes away and the Word document launches. However, the variable data isn't passed in any way.
I'm sure I'm missing something in my code.
Any help would be greatly appreciated! Thanks much!
Matt
Last edited by a moderator: