Results 1 to 9 of 9

Thread: Excel VBA NOT Contacting Word Correctly (Machine Specific)

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Excel VBA NOT Contacting Word Correctly (Machine Specific)

    I have a macro that uses data from an Excel sheet and to fill out a form in a Word document. Everything seems to be working fine except for one user. The macro is supposed to open a specific document and paste data into the correct spaces. One user's computer opens the Word form but then also opens another blank Word document and pasts variables into the blank document. Again, this is just one computer that is doing this...

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Probably a configuration setting on opening Word. I think I remember that you can request a blank doc on opening word. Perhaps this is happening, but after the document you wanted to load so the Selection object is pointing to that.

    Do you use Object variables or Selection object? If a selection object, then update your code to use object variables and it shouldnt matter if the blank doc appears.

    Alternatively, re-install office on that one machine.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Quote Originally Posted by Ecniv View Post
    Probably a configuration setting on opening Word. I think I remember that you can request a blank doc on opening word. Perhaps this is happening, but after the document you wanted to load so the Selection object is pointing to that.

    Do you use Object variables or Selection object? If a selection object, then update your code to use object variables and it shouldnt matter if the blank doc appears.

    Alternatively, re-install office on that one machine.
    I don't think it's a settings issue. Today my machine started doing this also (no change to settings and was working properly before). I am using object variables. What would cause it change?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    What would cause it change?
    maybe windows updates
    I am using object variables.
    post the code you are using, someone may be able to suggest a solution
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Quote Originally Posted by westconn1 View Post
    maybe windows updates
    post the code you are using, someone may be able to suggest a solution
    Here is my code, sorry if it has mistakes. I am self-taught and generally march to the beat of my own drum.

    Code:
    Sub PE140Generate()
    Dim lrow As Integer
    Dim pprnum As Integer
    Dim prnum As String
    Dim Rng As Range
    Dim protonum As String
    Dim requser As String
    Dim sdescription As String
    Dim dsubmitted As String
    Dim pleft As Integer
    Dim solvedira As String
    Dim solvedirb As String
    Dim fpath As String
    Dim wDoc As String
    Dim solvedir As String
    
    lrow = Cells(Rows.Count, 2).End(xlUp).Row
    pprnum = Cells(lrow, 1).Value
    
    prnum = InputBox("Generate Request for request number: ", "PE-140", pprnum)
    If prnum = "" Then Exit Sub
    
    'finds row number if a PE-140 is required for a row that is not the last, redefines lrow and pprnum if true
    On Error GoTo Ending
    If prnum <> " " And prnum <> pprnum Then
    With Sheets("Sheet1")
    Set Rng = .Range("A:A").Find(What:=prnum, LookIn:=xlValues)
    End With
    lrow = Rng.Row
    pprnum = Cells(lrow, 1).Value
    End If
    On Error GoTo 0
    
    'define variables for variables being transfered & save location
    protonum = Cells(lrow, 1).Value
    requser = Cells(lrow, 3).Value
    sdescription = Cells(lrow, 2).Value
    dsubmitted = Cells(lrow, 4).Value
    fpath = "\\[REAL NETWORK PATH\\"
    
    
    If sdescription = "" Or requser = "" Then
    MsgBox "You have not filled out all required information for that request number!" & Chr(13) & Chr(13) & "Please try again."
    GoTo Ending
    End If
    
    pleft = Left(protonum, 2)
    If Right(protonum, 2) = 0 Then
    solvedira = ((pleft - 1) * 100 + 1)
    solvedirb = pleft * 100
    solvedir = solvedira & "-" & solvedirb & "\"
    End If
    
    If Right(protonum, 2) <> 0 Then
    solvedira = pleft * 100 + 1
    solvedirb = (pleft + 1) * 100
    solvedir = solvedira & "-" & solvedirb & "\"
    End If
    
    'defines, opens, and sets focus on word docuement
    wDoc = "\\[REAL FILE LOCATION]\Rename_Me_PR-XXXX.dotx"
    
    Application.EnableEvents = False
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    
    'Set objDoc = objWord.documents.Open(wDoc)
    Set objDoc = objWord.documents.Add(wDoc)
    'objWord.documents.Add (wDoc)
    
    'objWord.Documents.Open wDoc
    'objWord.Activate
    objWord.Visible = True
    'objDoc.Application.Activate
    Application.ActivateMicrosoftApp xlMicrosoftWord
    objWord.ActiveWindow.View.ReadingLayout = False
    
    'set save file name
    If FileThere(fpath & solvedir & protonum & "\PR# " & protonum & " " & sdescription & ".docx") Then
    Application.Visible = True
    MsgBox "A request already exists for PR#" & protonum & "." & Chr(13) & Chr(13) & "The form you generated is minimized and will not be saved."
    GoTo SkipSave
    End If
    
    If sdescription <> "" Or requser <> "" Then
    objDoc.SaveAs (fpath & solvedir & protonum & "\PR# " & protonum & " " & sdescription & ".docx")
    'objWord.documents.SaveAs (fpath & solvedir & protonum & "\PR# " & protonum & " " & sdescription & ".docx")
    End If
    
    SkipSave:
    'find text and insert values into word document
    objWord.Selection.WholeStory
    objWord.Selection.Find.ClearFormatting
    
    With objWord.Selection.Find
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute findtext:="Proto No."
    End With
    
    objWord.Selection.MoveRight Count:=3
    objWord.Selection.TypeText Text:=protonum
    
    objWord.Selection.WholeStory
    objWord.Selection.Find.ClearFormatting
    
    With objWord.Selection.Find
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute findtext:="Requested By:"
    End With
    
    objWord.Selection.MoveRight Count:=2
    objWord.Selection.TypeText Text:=requser
    
    objWord.Selection.WholeStory
    objWord.Selection.Find.ClearFormatting
    
    With objWord.Selection.Find
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute findtext:="Sample Description:"
    End With
    
    objWord.Selection.MoveRight Count:=4
    objWord.Selection.TypeText Text:=sdescription
    
    objWord.Selection.WholeStory
    objWord.Selection.Find.ClearFormatting
    
    With objWord.Selection.Find
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute findtext:="Date Submitted:"
    End With
    
    objWord.Selection.MoveRight Count:=3
    objWord.Selection.TypeText Text:=dsubmitted
    
    objWord.Selection.WholeStory
    objWord.Selection.Find.ClearFormatting
    
    With objWord.Selection.Find
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchAllWordForms = False
        .Execute findtext:="Proto No."
    End With
    
    objWord.Selection.MoveLeft Count:=1
    Application.EnableEvents = True
    
    Ending:
    End Sub

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")

    'Set objDoc = objWord.documents.Open(wDoc)
    Set objDoc = objWord.documents.Add(wDoc)
    here you initiate a new instance of Word, the commented line should have opened a specific document, whereas you are actually adding a new document based on a specific template, which appears to be correct, but as the template appears to be on a network share, if may be basing the new document on the default template

    at least for testing purposes, first, i would check the existence /availability of the template
    Code:
    if len(dir(wdoc)) = 0 then msgbox "template not found"
    and /or try creating a new document in word based on the template

    of course you may have already tried some of the above
    also for testing, i would make objword visible earlier, just so i could see what is going on
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Quote Originally Posted by westconn1 View Post
    here you initiate a new instance of Word, the commented line should have opened a specific document, whereas you are actually adding a new document based on a specific template, which appears to be correct, but as the template appears to be on a network share, if may be basing the new document on the default template

    at least for testing purposes, first, i would check the existence /availability of the template
    Code:
    if len(dir(wdoc)) = 0 then msgbox "template not found"
    and /or try creating a new document in word based on the template

    of course you may have already tried some of the above
    also for testing, i would make objword visible earlier, just so i could see what is going on
    I can confirm the template exists and I am able to create a new document by clicking on the template. Is it possible that it is a network issue? Maybe communication is taking too long and Word opens a blank document rather than the template when the time comes to paste information?

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    I can confirm the template exists and I am able to create a new document by clicking on the template.
    that is why i suggested the code to make sure that vba could also see the file there, before trying to use

    Maybe communication is taking too long
    for testing copy the template to the local machine
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    10

    Re: Excel VBA NOT Contacting Word Correctly (Machine Specific)

    Quote Originally Posted by westconn1 View Post
    that is why i suggested the code to make sure that vba could also see the file there, before trying to use

    for testing copy the template to the local machine
    I added the line of code you suggested. When this happened again (pulling template from local drive) the message box was not triggered.
    Last edited by Wobbs91; Apr 5th, 2018 at 08:36 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width