Log in

View Full Version : [SOLVED:] Error when trying to initialize Userform on another computer running 365



JPG
03-13-2020, 04:13 PM
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

gmayor
03-13-2020, 09:46 PM
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?

JPG
03-14-2020, 01:04 AM
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

gmayor
03-14-2020, 06:55 AM
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

JPG
03-14-2020, 07:33 AM
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.

JPG
03-14-2020, 02:53 PM
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)")



26157

gmayor
03-14-2020, 09:52 PM
You could add the line
On Error Resume Nextbefore 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?

JPG
03-14-2020, 11:54 PM
You could add the line
On Error Resume Nextbefore 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....

JPG
03-22-2020, 12:50 PM
Abandoning the idea. It will cause more issues than it solves. Learned from my mistaken assumption.