Advertisement

05.25.2008 at 12:31PM PDT, ID: 23431527
[x]
Attachment Details

Excel VBA - check if a Word document is open

Asked by murbro in Microsoft Visual Basic.Net, Microsoft Excel Spreadsheet Software, Microsoft Word

Hi

I am using the following procedure to paste something to a Word document.
I need to run the procedure several times. How do I update the code so that
it checks if the Word document is already open and doesn't reopen it.


Sub PasteToWord2(ByVal strFilePath As String)

 On Error GoTo EH
   Dim wdApp As Object
   Dim i As Integer
    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    With wdApp
        .Visible = True
        Set wdDoc = .Documents.Open( _
                              Filename:=strFilePath, ReadOnly:=False)
    End With
 
   'Range(strRange).CopyPicture xlScreen, xlPicture 'use if you need to copy Excel range is a pic in this procedure
        wdDoc.Range.past
        wdDoc.Save
        wdDoc.Close
  wdApp.Quit
   
    'clean up
    Set wdDoc = Nothing
    Set wdApp = Nothing
        Exit Sub
EH:
    MsgBox "There was a problem pasting to a word document!"
End SubStart Free Trial
[+][-]05.25.2008 at 12:46PM PDT, ID: 21643513

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Microsoft Excel Spreadsheet Software, Microsoft Word
Sign Up Now!
Solution Provided By: raja_ind82
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628