Consulting

Results 1 to 9 of 9

Thread: Error when trying to initialize Userform on another computer running 365

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    Error when trying to initialize Userform on another computer running 365

    In the initialize of my userform I have this code which I thought was generic to Word 2010 onwards


    ActiveDocument.ApplyQuickStyleSet2 ("Default (Black and White)")

    However when used on 365 by someone else it fails to load


    So, how can I make this work, or at least fail gracefully and continue to load.

    Thanks
    Jon

  2. #2
    The code is still valid, however it is not possible to suggest why there is a problem without knowing more about your code. The obvious question is: Is there a document open when the code is called?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    I guess I should look into catering for both situations being the case -- A document already open, and when it is active and a document is opened. I have not had a problem either way.

    The only other things I tried to do on initialize were as follows. I don't have 365 to test this. I only have 2010. Maybe the other part of the code sets it up to fail. It anyone with 365 can try the code it would be helpful.
    Also a way to bypass the problem code with some sort of popup would be good enough.

    Thanks for replying.
    p.s the document would more than likely be an .rtf file. Again though I have not encountered an issue.

    'Essential to have this selection
    selection.Document.Activate
    
    
    'ActiveDocument.ActiveWindow.View.Type = wdPrintView
    With ActiveDocument.Background.Fill
        .ForeColor.ObjectThemeColor = wdThemeColorAccent1
        .Visible = True
        .ForeColor.TintAndShade = 0.8
        .Solid
    End With
    
    
    'Select default black and white style for document
    ActiveDocument.ApplyQuickStyleSet2 ("Default (Black and White)")
    I added to the code. So more graceful exit if no document present.
    Private Sub UserForm_Initialize()
    If Application.Documents.Count = 0 Then
        MsgBox ("No document open 1")
        End
    End If
    End Sub
    Sub OpenForm2()
    If Application.Documents.Count = 0 Then
    MsgBox ("No document open 3")
    End
    End If
    
    theWordMacros2.Show
    
    End Sub
    Last edited by JPG; 03-14-2020 at 02:21 AM.

  4. #4
    You should name the documents then the code knows which document to process e.g.

    Sub OpenForm()
    Dim oDoc As Document
        If Application.Documents.Count = 0 Then
            MsgBox ("No document open")
            Exit Sub
        End If
    
        Set oDoc = ActiveDocument
    
        theWordMacros2.Show
    
        oDoc.ActiveWindow.View.Type = wdPrintView
        oDoc.ApplyQuickStyleSet2 ("Default (Black and White)")
    
        With oDoc.Background.Fill
            .ForeColor.ObjectThemeColor = wdThemeColorAccent1
            .Visible = msoTrue
            .ForeColor.TintAndShade = 0.8
            .Solid
        End With
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    OK thank you for that clarification. It works fine for me, so I will see what feedback I get for the one that had problem with Off 365.

  6. #6
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    The problem persists on Office 365

    This is the problem line.

    oDoc.ApplyQuickStyleSet2 ("Default (Black and White)")
    How do I find the ...styleset that is ("Default (Black and White)")



    error.png

  7. #7
    You could add the line
    On Error Resume Next
    before the errant line, but frankly rather than mess around with changing styles using styles sets and applying formatting, why not simply configure the template used to create the documents with the required styles and formats, rendering the code redundant?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Quote Originally Posted by gmayor View Post
    You could add the line
    On Error Resume Next
    before the errant line, but frankly rather than mess around with changing styles using styles sets and applying formatting, why not simply configure the template used to create the documents with the required styles and formats, rendering the code redundant?
    Switching to the default black and white style fixed some unwanted background colour in one step, both in the document and footnote area, also the text was just black and white. I don't have control over the template used to make the document. I don't know how to configure the template used to create the documents. Each person or publisher have their own. I am trying to get documents formatted and marked up in a consistent way. Then another app takes the data, splits it on the markup added and puts it into an SQLite database...

    Thanks for your guidance. Word and VBA is not something that is familiar to me at this level....

  9. #9
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Abandoning the idea. It will cause more issues than it solves. Learned from my mistaken assumption.

Tags for this Thread

Posting Permissions

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