0

I have been looking all over for an answer to this issue. It seems like it should be a relatively simple fix but I'm not sure what I'm missing here.

The outcome should be the user selects a file from a folder (PickFile is a function that does that). Then from that selected file it will open that word doc, copy all of it's contents and paste it into DocText field in the form.

Any help or push in the right direction would be really appreciated. I've been looking all over but nothing I've found has worked.

Private Sub Updatebtn_Click()
Dim FName As String
Dim WordApp As Object
Dim WordDoc As Object
Dim WordRng As Object
Dim s As String

    'Add new SOP Doc
    s = PickFile("\\page\data\NFInventory\groups\CID\SOPs\", False, True)
    If s <> "" Then FileName = s
    
    'Update SOP Doc Text
    StatusBox = ""
    doctxt = ""
    FName = "\\page\data\NFInventory\groups\CID\SOPs\" & FileName
    
    'Status "Loading SOP..."
    Set WordApp = CreateObject("Word.Application")
    WordApp.Visible = True
    
    'Status "Opening SOP..."
    Set WordDoc = WordApp.Documents.Open(FName)
    
    'Status "Copying Text..."
    Set WordRng = WordDoc.Range(1)
    WordRng.WholeStory
    WordRng.Copy
    
    'Status "Pasting Text..."
    DocText.SetFocus
    DoCmd.RunCommand acCmdPaste ' <----error
    Nametxt.SetFocus
    If IsNull(Nametxt) Then Nametxt = Left(doctxt, 10)
    
    'Status "Closing Word..."
    WordDoc.Close False
    WordApp.Quit False
    
    'Status "Done"
       
Set WordApp = Nothing
Set WordDoc = Nothing
Set WordRng = Nothing

End Sub
braX
  • 11,506
  • 5
  • 20
  • 33
Deke
  • 425
  • 5
  • 20
  • 1
    If DocText is plain text, you could do instead something like `Me.DocText.Value = WordRng.Text`. – Andre Oct 27 '22 at 22:38
  • If you must use Paste, try putting a few milliseconds `Sleep` before it. https://stackoverflow.com/a/1544582/3820271 – Andre Oct 27 '22 at 22:39
  • @Andre Thank you! that did the trick! Appreciate the help! – Deke Oct 28 '22 at 13:43

0 Answers0