Results 1 to 6 of 6
  1. #1
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Paste a Word doc into a form control (Access97)

    I want to get the contents of a word doucument and put it into a text box in a form before the text already there. I have written the following code that works, but I am puzzled by two things:

    <pre> Dim strlabNo As String
    strlabNo = Me!LabNo
    strfilename = fngetLetter(strlabNo)

    Dim wrd As Word.Application
    Dim objDocument As Word.Document
    Set wrd = GetObject(, "Word.Application")

    wrd.Documents.Open fileName:=strfilename
    AppActivate ("Microsoft Word")
    wrd.ActiveDocument.select
    wrd.Selection.Copy
    wrd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    AppActivate ("Skinpath Pathology")
    Me!Report.SetFocus
    Me!Report.SelLength = 0
    Me!Report.SelStart = 0
    SendKeys "{ENTER}", True
    Me!Report.SelStart = 0
    SendKeys "{ENTER}", True

    SendKeys "{x 30}", True
    Me!Report.SelStart = 0
    DoCmd.RunCommand acCmdPaste
    </pre>



    My two questions :

    If I try to paste using : SendKeys "^V", True (instead of DoCmd.RunCommand acCmdPaste) nothing happens. Why not ? Have I got the syntax wrong?

    If I make my control a rich text control instead of a normal Access text box, I get the following error, although if I then press Ctrl V my documnet is pasted in just the way i want it.
    Attached Images Attached Images
    Regards
    John



  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Paste a Word doc into a form control (Access97)

    <P ID="edit" class=small>(Edited by MarkD on 16-Nov-03 07:03. Added additional comment.)</P>I'd recommend ditching the flaky SendKeys stuff and use something like this example to capture document text in a form's textbox:

    Sub TestImportWordDoc(ByRef strDocName As String)
    On Error GoTo Err_Handler

    Dim app As New Word.Application
    Dim doc As Word.Document
    Dim rng As Word.Range
    Dim strMsg As String

    app.Visible = False

    Set doc = app.Documents.Open(strDocName)
    doc.SaveAs doc.Name & ".txt", wdFormatText

    Set rng = doc.Range

    With Me.Text1
    .Value = rng.Text & vbCrLf & vbCrLf & .Value
    End With

    doc.Close
    app.Quit

    Exit_Sub:
    Set app = Nothing
    Set doc = Nothing
    Set rng = Nothing
    Exit Sub

    Err_Handler:
    strMsg = "Error No " & Err.Number & ": " & Err.Description
    Beep
    MsgBox strMsg, vbExclamation, "IMPORT WORD ERROR"

    Resume Exit_Sub
    End Sub

    Note: strDocName is full path & file name of Word doc. I'd also strongly recommend saving copy of Word doc as plain text file using this statement:

    doc.SaveAs doc.Name & ".txt", wdFormatText

    before importing its contents, unless you want your textbox populated with a bunch of little square boxes where the doc's paragraph marks were. Saving doc as text will convert Word paragraph marks, section breaks, page breaks, etc to CR/LF combinations which may be more useful when importing into Access. If doing this repeatedly then only open Word once as a separate procedure and close when done processing all docs to be imported, this is just simple example.

    HTH

  3. #3
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Paste a Word doc into a form control (Access97)

    One additional note, I have no experience using Rich Text controls, so don't know how well this would work. If using Rich Text control, then it may be useful to replace this line:

    doc.SaveAs doc.Name & ".txt", wdFormatText

    with this:

    doc.SaveAs doc.Name & ".rtf", wdFormatRTF

    If you want to preserve formatting, etc. According to MS, saving doc in this format means: "Rich Text Format (RTF): Saves all formatting. Converts formatting to instructions that other programs, including compatible Microsoft programs, can read and interpret."

    You would have to test this with Rich Text control to determine best approach.

    HTH

  4. #4
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Paste a Word doc into a form control (Access97

    Thanks Mark

    My Word docs are already rtf docs, and I would like to preserve the formatting if possible.

    If I copy the document from Word, and paste it into the Rich Text Control, everything works just as I want it to.

    If I use a range object as you suggest, the formatting gets lost. (although I will play around with this some more.)

    So I am still looking for a way to programme a paste. DoCmd.RunCommand acCmdPaste produces an error, and SendKeys "^V", True does nothing.

    Is there any other way to do it?
    Regards
    John



  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Paste a Word doc into a form control (Access97

    Why not provide hyperlinks to the .rtf or Word documents, or open them using ShellExecute? Trying to cram a Word document into Access is going to be problematic.

  6. #6
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts

    Re: Paste a Word doc into a form control (Access97

    I have found a solution to my problem.

    Rich Text Controls have a filename property, so just by setting
    <pre>Me!letter.fileName = strfilename
    </pre>

    the Word doc appears immediately as my word docs are all RTF files.

    I was originally trying to get the doc to appear with but before some other text from another source, but I have now found a way to make the next step work with the two kept separate.
    Regards
    John



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •