+ Reply to Thread
Results 1 to 10 of 10

Thread: Control Word from Excel in VBA

  1. #1
    Registered User
    Join Date
    05-26-2011
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    6

    Control Word from Excel in VBA

    Hello, i need help with 2 problems

    1)
    im coping excel ranges and inserting them on bookmars in a word document in a iterative way, the problem arises
    when i try to format the pasted tables, only the first get changed and not all the chages are made by the code.
    I really dont understand why it doesnt work

    wordDoc.Bookmarks("tabInstADSLAITop").Range.PasteExcelTable False, False, False
            Application.CutCopyMode = False
            Set tbl = wordDoc.Tables(numTab)
             With tbl
            .Rows.HeightRule = wdRowHeightExactly
            .Rows.Height = 15.6 ' +- 0.55 cm
            .AutoFitBehavior (wdAutoContent)
            .AutoFitBehavior (wdAutoFitWindow)
             End With
            wordDoc.Bookmarks("instADSLAITop").Range.InsertAfter numInst



    2)
    for time to time i keep getting the error 462 The remote server machine does not exist or is unavailable at this line:
      Set tbl = wordDoc.Tables(numTab)
    or this line:
            wordDoc.Bookmarks("instADSLAITop").Range.InsertAfter numInst


    Thanks

  2. #2
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Control Word from Excel in VBA

    You'll have tlo show more of your code:

    It's not clear which method you use to control Word: Getobjec, createobject or Word.application (early binding).
    The origin of the variable 'numtab' isn't clear, neither is numinst's.



  3. #3
    Registered User
    Join Date
    06-29-2011
    Location
    California
    MS-Off Ver
    Excel 2003/2007
    Posts
    58

    Re: Control Word from Excel in VBA

    Edited my first response after re-reading.

    How does early binding work? I always use the create/get object method
    Last edited by docMed; 07-01-2011 at 03:02 PM.

  4. #4
    Registered User
    Join Date
    05-26-2011
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Control Word from Excel in VBA

    This is how i do:

    Private Sub CommandButton3_Click()
    fp_TemplateWord = Application.GetOpenFilename("Wordfiles (*.doc*;*.dot*)*.doc*,*.dot*", , "Abrir um Ficheiro Word...")
    
    If fp_TemplateWord = False Then
    Exit Sub
    Else
    
    LabelTemplateWord.Caption = GetFilenameFromPath(fp_TemplateWord)
    
    
    
       On Error Resume Next
       Set wdApp = GetObject(, "Word.Application")
       If Err.Number <> 0 Then 'Word isn't already running
           Set wdApp = CreateObject("Word.Application")
       End If
       On Error GoTo 0
       Set wdDoc = wdApp.Documents.Open(fp_TemplateWord)
       wdApp.Visible = True
       wdDoc.Activate
    
    
    
    End If
    End Sub

  5. #5
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Control Word from Excel in VBA

    To achieve the same result:

    Sub snb()
      c00 = Application.GetOpenFilename("Wordfiles (*.doc*;*.dot*)*.doc*,*.dot*", , "Abrir um Ficheiro Word...")
    
      If c00 <> False Then
        With GetObject(c00)
          .Activate
                   '  ====  your code
          .Close 0
        End With
      End If
    End Sub
    But that leaves the questions about the variables unanswered.

    @docMed

    Eaarly binding: activate the reference to a certain application in the VBEdior/menu Bar/ extra/references.
    Having done that you can use this code:

    Sub snb_early_binding()
      c00 = Application.GetOpenFilename("Wordfiles (*.doc*;*.dot*)*.doc*,*.dot*", , "Abrir um Ficheiro Word...")
    
      If c00 <> False Then
         word.documents.add c00
    
      end if
    End Sub
    Last edited by snb; 07-04-2011 at 05:06 AM.



  6. #6
    Registered User
    Join Date
    05-26-2011
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Control Word from Excel in VBA

    The variable numTab is passed as argumente to the SUB, and it comes from a FOR loop.
    it represents the position of the table at the word document.
    I dont know any other way of selecting the table...

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Control Word from Excel in VBA

    With latebinding you can't use Word constants like 'wdAutoContent'.



  8. #8
    Registered User
    Join Date
    05-26-2011
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Control Word from Excel in VBA

    I didnt quite understand the meaning o late/early binding so i went here http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx but it didnt tell me how to do the early bind that i need for the code to work.
    Its even possible? And if it is, how can i do this?
    Thanks.

  9. #9
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Control Word from Excel in VBA

    I explained that in post #5



  10. #10
    Registered User
    Join Date
    05-26-2011
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Control Word from Excel in VBA

    i´ve changed the variables to
    Dim wdDoc As word.Document
    Dim wdApp As word.Application
    i wented to Tools->references and the microsoft word/excrel object library 12.0 was already set, so i think that the following already is early binding

    Private Sub CommandButton3_Click()
    
    fp_TemplateWord = Application.GetOpenFilename("Wordfiles (*.doc*;*.dot*)*.doc*,*.dot*", , "Abrir um Ficheiro Word...")
    '
    If fp_TemplateWord = False Then
    Exit Sub
    Else
    LabelTemplateWord.Caption = GetFilenameFromPath(fp_TemplateWord)
    
       On Error Resume Next
       Set wdApp = GetObject(, "Word.Application")
       If Err.Number <> 0 Then 'Word isn't already running
           Set wdApp = New word.Application
       End If
       On Error GoTo 0
       Set wdDoc = wdApp.Documents.Open(fp_TemplateWord)
       wdApp.Visible = True
       wdDoc.Activate
    End If
    But in the form that im making when i summon the procedure that handles word
    Sub inserttable(wordDoc As Object, nomeServico As String, tipotabela As String, numInst As Integer, numTab As Integer, wdApp As Variant, tabela As Range)
    i use wordDoc as Object because it gives error if i try to use word.Document !?
    Last edited by kronozord; 07-04-2011 at 12:45 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0