Re: Word Document VBA Question by G
G
Tue Aug 31 09:52:24 CDT 2004
Mike and Stephanie,
I had the same problem, as I created a an Access Report, and found that all
the lines and nice formatting were lost with the "send to mail recipient as
RTF attachment". I went with the word document as the formatting is
preserved.
The trick is that you need to create the desired word document, the use the
Insert>Bookmark to create locations in the document where the program will
place info, then open the document and place info at each named bookmark.
My problem was a bit trickier, as I had to open word, then open the desired
word document, then insert the info. It is pretty easy though.
Here is a code snippet of opening a document and writing to boomarks. Hope
this helps:
Dim objWord As Object
Dim objDoc As Object
Dim strFullPathAndFileName As String
'build the path to the rma form
strRMA = "RMA Request Form rev 2004-08-27"
strFullPathAndFileName = "l:\database\customer support\" & strRMA &
".doc"
'Opens word
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'opens the document
Set objDoc = objWord.Documents.Open(strFullPathAndFileName)
'write to the document at the bookmarks
With objWord.ActiveDocument.Bookmarks
.Item("MN").Range.InsertAfter (((UCase(Forms("FrmRMARequest")("MN")))))
.Item("SN").Range.InsertAfter (((Forms("FrmRMARequest")("SN"))))
.Item("PN").Range.InsertAfter (((Forms("FrmRMARequest")("PN"))))
.Item("RmaDate").Range.InsertAfter
(((Forms("FrmRMARequest")("RequestDate"))))
"Stephanie Krieger" <MODD_2003NO@SPAMmsn.com> wrote in message
news:033e01c48c84$ef7524e0$a401280a@phx.gbl...
> Hi, Mike,
>
> Okay -- so Word VBA will be really simple for you!
>
> You don't need to use forms or mail merge for this. A
> regular Word document will work fine for what you want to
> do... then the code does the content merging. You have a
> few choices of how the code will read the content into
> that form. If you use a table for the document, you can
> just have text dropped into specified table cells for
> each field (I do that frequently when building templates -
> - it's a clean, easy way to go both in Word and in terms
> of keeping the code as brief as possible... also is very
> stable in terms of easily retaining whatever document
> formatting you set up in the master, and it doesn't put
> limits on the amount of content you can place in any
> table cell.)
>
> If you send me an email address, I'll be happy to send
> you a quick sample using a Word table for the form with
> VBA code to generate a new document and add content to
> it. I don't visit the newsgroups every day (and not on
> the weekends at all, so using my email below will be best
> for following-up.)
>
> Best,
> Stephanie
>
> e-mail: MODD_2003 at msn.com
>
> >-----Original Message-----
> >Thanks for the post. I guess I am leaning toward using
> a Word template form
> >and creating the document on the fly. Basically, I
> would create a xml or
> >ini file that would have all the info for the word
> document. Then create a
> >macro via the word template file that would read the
> file (xml or ini) and
> >fill in the data in the document. I do have alot of
> experience writing VB
> >code so the transition to VBA will not be hard, I just
> have no experience
> >working with Word. The real issue is how do I create
> somewhat a static
> >form/template in Word that I can push data into. Is
> this called a user form
> >in Word or is this mail merging? Am I on the right
> track?
> >
> >Thanks
> >
> >"Stephanie Krieger" <MODD_2003NO@SPAMmsn.com> wrote in
> message
> >news:17de01c48c5e$0dcf4b40$a501280a@phx.gbl...
> >> Hi, Mike,
> >>
> >> In terms of adding borders... Depending on how the form
> >> exports (i.e., does it place the content in a table or
> >> paragraph text? etc.) will depend on what a formatting
> >> macro should consist of ... but if you have elements
> that
> >> are always consistent, you can certainly create a macro
> >> to do that. I'd need more information to direct you on
> >> specific code.
> >>
> >> Or, if you're going to create your own form instead, a
> >> table is going to be much simpler and look a lot better
> >> ultimately than trying to do this with text boxes (will
> >> also take a fraction of the time to both create and
> fill-
> >> out). You can use cell borders for the underlines of
> your
> >> line items -- they'll remain stable and table cells
> will
> >> expand with content, but won't move or distort the
> form.
> >>
> >> You'll only need VBA if you want to automate completion
> >> of the form. VBA is quite easy to learn, but it will be
> >> easiest if you get a solid foundation of end-user
> skills
> >> with Word before going there -- and sounds to me like
> you
> >> might not need to go there at all for this project.
> >>
> >> The easiest thing for this form might just be to create
> >> the table in a document (Table, Insert, Table), format
> it
> >> and then save it as a template.
> >>
> >> To save it as a template, go to File, Save and select
> >> Document Template as the save as type. The save
> location
> >> will default to the user template folder.
> >>
> >> Then, to start a new form, select the template from the
> >> templates dialog box (it will appear on the General tab
> >> of that dialog box) and it will create a new document
> >> with the content you saved in the template. How you
> >> access that dialog box depends on your Word version --
> in
> >> 2002 or 2003, it's accessible from the New Document
> task
> >> pane (General Templates in 2002 and On My Computer in
> >> 2003) -- or through File, New in 2000 or earlier.
> >>
> >> I hope that's helpful!
> >>
> >> Stephanie Krieger
> >> author of Microsoft Office Document Designer
> >> (from Microsoft Press)
> >> e-mail: MODD_2003 NOSPAM @msn.com
> >> blog: arouet.net
> >>
> >> >-----Original Message-----
> >> >Hi,
> >> >
> >> >I have an application that creates a report using a
> >> third party tool which
> >> >has the ability to export to a RTF (Word) document.
> The
> >> problem I am
> >> >running into is the report has lines to seperate
> certain
> >> sections however
> >> >the export does not support lines. I am looking for a
> >> solution in where I
> >> >create a Word Macro and create the lines in Word. Or
> I
> >> was thinking of
> >> >creating a word document and use it as a template.
> The
> >> report is basically
> >> >a quote form generated for sales and sales needs to
> have
> >> the ability to edit
> >> >it. Basically, the lines are used to hold the line
> >> items and give it a
> >> >professional look. I was thinking that the Word
> >> document could have a
> >> >textbox for the line items and I just pass the info in
> >> and would shrink or
> >> >grow as the data grows. I do not have alot of
> >> experience with VBA and Word
> >> >so I am just looking for some suggestions or comments.
> >> >
> >> >Thanks
> >> >
> >> >
> >> >.
> >> >
> >
> >
> >.
> >