Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Fields updating via VBA

  1. #1
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location

    Fields updating via VBA

    Can anyone point me to a book/tutorial that explains how to implement field updating through vba. Hopefully, the following explanation of what I am trying to achieve is clear. If it is not I will try to articulate it better.

    I want to have fields in word. These fields should show a name (i.e., First Name) and linked by code to a named cell in Excel (i.e., FirstName). I cannot use the built-in linked fields, because rows and columns in the Excel sheet are constantly being added and linked fields as you know are referenced by row and column numbers.

    I am thinking that I will need to use DOCVARIABLEs. However, I am stuck. When I manually insert a DOCVARIABLE (Ctrl-F9), I can name it {DOCVARIABLE "VariableName"}, but when I click ALT-F9, I cannot see the field, because there is no name assigned to it.

    Searched google but cannot find an answer.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by DeenaH View Post
    I cannot use the built-in linked fields, because rows and columns in the Excel sheet are constantly being added and linked fields as you know are referenced by row and column numbers.

    That not really so, as you can use range names in the link field. If you move, expand or contract the named range, the LINK field will pick up those changes.

    Quote Originally Posted by DeenaH View Post
    I am thinking that I will need to use DOCVARIABLEs. However, I am stuck. When I manually insert a DOCVARIABLE (Ctrl-F9), I can name it {DOCVARIABLE "VariableName"}, but when I click ALT-F9, I cannot see the field, because there is no name assigned to it.
    A DOCVARIABLE field won't do any better, as it still needs a DOCVARIABLE that contains the actual content - not an external workbook reference.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Great, as long as I define a name for a cell or a field, it links it with the name.

  4. #4
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Macropod, you were correct. However, the word document I am trying to replicate has the option of double clicking on the linked field (DOCVARIABLE) and a window pops up and changes are communicated back to the Excel file. That is what I am trying to learn how to do.

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Anything that's going to transfer content from the document back to an Excel workbook will require VBA programming, not just field coding. Since the document you're working from is already doing that, that suggests the VBA code is already in that document (e.g. if it's a docm file) or its template.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Exactly my point, that's why I believe that I need to use DOCVARIABLEs, which will be populated by code. But I am trying to figure out how the DOCVARIABLE is showing a name before being updated. I looked for a tutorial or book but cannot find any. Any suggestions where I can find one that addresses the above, would be greatly appreciated.

    I am not working from such a document, but I saw this features in a software package and would like to implement it into my word/excel files.

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by DeenaH View Post
    Exactly my point, that's why I believe that I need to use DOCVARIABLEs, which will be populated by code. But I am trying to figure out how the DOCVARIABLE is showing a name before being updated. I looked for a tutorial or book but cannot find any. Any suggestions where I can find one that addresses the above, would be greatly appreciated.
    Because that's now how DOCVARIABLE fields either work or can be made to work.
    Quote Originally Posted by DeenaH View Post
    I am not working from such a document, but I saw this features in a software package and would like to implement it into my word/excel files.
    That's not what you said before!
    Quote Originally Posted by DeenaH View Post
    the word document I am trying to replicate has the option of double clicking on the linked field (DOCVARIABLE) and a window pops up and changes are communicated back to the Excel file.
    A "word document I am trying to replicate" is not the same as "I saw this features in a software package"...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Now that you understand what I am looking for, can you suggest further reading/tutorial/video.

    Thanks.

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Since I haven't seen this 'software package' you're referring to, I don't really know what it's doing or what you're looking for. I suggest you look into the use of userforms and Excel automation. As an alternative to a userform, formfields, contentcontrols or ActiveX controls might be used.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    It's not being done with a userform. See screenshot.

    In word there are DOCVARIABLEs with a placeholder name then a button is clicked and all the fields are updated. In addition, you can double click on any field and a window opens where you can edit the field, which reflects the change in Excel.



  11. #11
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Most likely, what you're referring to as "DOCVARIABLEs with a placeholder name" are actually named building blocks, which employ a form of content control (see my previous post). And, if there is a button, then that button (most likely an ActiveX command button, but possibly a picture content control) calls some VBA code to do the updating. As I said before, that code will be in that document (e.g. if it's a docm file) or its template.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    When you click alt-f9 it shows DOCVARIABLE.

    See both images (before and after alt-f9). There is no code attached or Macros, but I believe this is because this is just the doc file and the vba is embedded in a dll. I also do not have access to the software only to the word file. I'm just trying to figure out how it was done so I can add it to my documents. When I click F9 to update the fields, some fields update, while others don't. So I would also like to know how to find out where the references are to the fields.




  13. #13
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    So how about attaching the actual Word file to a post...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  14. #14
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location

    Attached File

    The document was way too big, so I attached the first page, which should show how things are done. If you click F9 on file name (13-03201) it updates to 13-03200.
    Attached Files Attached Files

  15. #15
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by DeenaH View Post
    If you click F9 on file name (13-03201) it updates to 13-03200.
    That's only because the stored value for the "AppraisalFile" variable is already 13-03200. There is nothing you can do by editing the field to change the variable's value to anything else - any time you press F9 again it will revert to 13-03200.
    You can confirm that the "AppraisalFile" variable is already 13-03200 by opening the document and running the following macro before updating the field:
    Sub Test()
    MsgBox ActiveDocument.Variables("AppraisalFile").Value
    End Sub
    Conversely, if you run the following macro, then update the field, you'll get 13-03999:
    Sub Demo()
    ActiveDocument.Variables("AppraisalFile").Value = "13-03999"
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  16. #16
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Ok, great, but is there any way to see all the values of the docvariables? Is there a repository for all of the variables?

    Is it correct to assume that the placeholder names for the docvariables had to be set in code and then the updating of them causes them to get the value from the Excel file?

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    DOCVARIABLE fields only reflect what the document variables in the document's metadata contain; they cannot update that data or transfer anything between there and another application (or even another document). The placeholder names would have been the initial values stored in the corresponding document variables. Updating what is displayed has no effect on what is stored; only on what is displayed. The only way a document variable can be updated is through code, whether VBA, XML coding or in some other application. Your previous reference to a button that is clicked on suggests the use of VBA code. Your attachment didn't include that button or any code, however.

    Since document variables are user-defined, there is no standard set. You can find out all of their names & current values for a given document with a macro like:
    Sub Demo()
    Dim i As Long, StrVar As String
    With ActiveDocument
      For i = 1 To .Variables.Count
        With .Variables(i)
          StrVar = StrVar & vbCr & "Name: " & .Name & ", Value: " & .Value
        End With
      Next
    End With
    MsgBox "Variables in this document:" & StrVar
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    VBAX Regular DeenaH's Avatar
    Joined
    Aug 2018
    Posts
    11
    Location
    Okay. I already saw the variables, but I had to use selection.typetext because a msgbox was not big enough.

    So, how can I have docvariables point their respective named cells in excel. Basically the excel and the word documents will reside in the the same folder and have the same name.

    Thanks.

  19. #19
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by DeenaH View Post
    So, how can I have docvariables point their respective named cells in excel.
    You can't. I've already told you that. Repeatedly.

    The only way of updating the variables from Excel or vice-versa (after you've updated them by code in the document via VBA, etc.) is by code and, to do that, one would need to know what the workbook name is and which particular Excel cell relates to a given document variable.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  20. #20
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 8/18/2018
    Dim oXL As Object
    Dim oWB As Object
    Dim oWS As Object
    
      Set oXL = CreateObject("Excel.Application")
      Set oWB = oXL.WorkBooks.Open("D:\Book1.xlsx") 'Or whatever you file name is.
      Set oWS = oWB.Sheets("Sheet1")
      ActiveDocument.Variables("FirstName").Value = oWS.Range("FirstName").Value 'or whatever you document variable and excel named range are.
      ActiveDocument.Variables("LastName").Value = oWS.Range("LastName").Value 'or whatever you document variable and excel named range are.
      ActiveDocument.Fields.Update
      oWB.Close SaveChanges:=False
      oXL.Quit
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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