|
-
Sep 24th, 2010, 11:06 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Move screen data to Word
I have a screen that shows some customer data. I need to be able to click a command button and have the name, address, etc put on a MS Word 2007 document. This is my code so far.
Code:
Private Sub cmdWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWord.Click
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim name As String
name = Trim(txtFName.Text) & " "
If Trim(txtMiddle.Text) <> "" Then
name = name & Trim(txtMiddle.Text) & " "
End If
name = name & Trim(txtLName.Text)
Dim citystatezip As String
citystatezip = Trim(txtCity.Text)
citystatezip = citystatezip & ", " & Trim(txtState.Text)
citystatezip = citystatezip & " " & Trim(TxtZip.Text)
oWord = New Word.Application
oDoc = oWord.Documents.Open("Z:\CRM\CRMLetter.doc")
With oDoc
.Bookmarks("Name").Range.Text = name
.Bookmarks("Address").Range.Text = txtStreet1
.Bookmarks("citystatezip").Range.Text = citystatezip
End With
oWord.Visible = True
oDoc = Nothing
oWord = Nothing
End Sub
It does not like the Dims at the top.
FYI... it works in VB6
===================================================
If your question has been answered, mark the thread as [RESOLVED]
-
Sep 24th, 2010, 12:16 PM
#2
Thread Starter
PowerPoster
Re: Move screen data to Word
Ok. It has changed. Now I have this:
Code:
Dim oWord As Object
Dim oDoc As Object
Dim name As String
name = Trim(txtFName.Text) & " "
If Trim(txtMiddle.Text) <> "" Then
name = name & Trim(txtMiddle.Text) & " "
End If
name = name & Trim(txtLName.Text)
Dim citystatezip As String
citystatezip = Trim(txtCity.Text)
citystatezip = citystatezip & ", " & Trim(txtState.Text)
citystatezip = citystatezip & " " & Trim(TxtZip.Text)
oWord = CreateObject("Word.Application")
oDoc = CreateObject("Word.Document")
oWord.Visible = True
With oDoc.Bookmarks
.item("Name").Range.Text = name
.item("Address").Range.Text = txtStreet1
.item("citystatezip").Range.Text = citystatezip
End With
oDoc = Nothing
oWord = Nothing
It does not like this code
Code:
With oDoc.Bookmarks
.item("Name").Range.Text = name
.item("Address").Range.Text = txtStreet1
.item("citystatezip").Range.Text = citystatezip
End With
===================================================
If your question has been answered, mark the thread as [RESOLVED]
-
Sep 24th, 2010, 12:30 PM
#3
Thread Starter
PowerPoster
Re: Move screen data to Word
Got it to work
Code:
Private Sub cmdWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWord.Click
Dim oWord As Object
Dim oDoc As Object
Dim name As String
name = Trim(txtFName.Text) & " "
If Trim(txtMiddle.Text) <> "" Then
name = name & Trim(txtMiddle.Text) & " "
End If
name = name & Trim(txtLName.Text)
Dim street As String
street = Trim(txtStreet1.Text)
Dim citystatezip As String
citystatezip = Trim(txtCity.Text)
citystatezip = citystatezip & ", " & Trim(txtState.Text)
citystatezip = citystatezip & " " & Trim(TxtZip.Text)
oWord = CreateObject("Word.Application")
oDoc = CreateObject("Word.Document")
oDoc = oWord.Documents.Add("Z:\CRMLetter.doc")
oWord.Visible = True
With oDoc.Bookmarks
.item("Name").Range.Text = name
.item("Address").Range.Text = street
.item("citystatezip").Range.Text = citystatezip
End With
oDoc = Nothing
oWord = Nothing
End Sub
===================================================
If your question has been answered, mark the thread as [RESOLVED]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|