Creating auto running macros

Started by slinden, April 11, 2014, 11:45:01 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

slinden

I am trying to record an auto running macro, but so far I haven't been successful, probably because I do not have any experience of VBA or programming whatsoever. I have been searching for an answer but my problem remains unsolved.

I am able to record a macro that does the basic default settings for my template.

Sub Macro1()
' Keyboard Shortcut: Ctrl+Shift+D
'

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Summary Info")
    Application.ActiveDocument.Title = "P/N"
    Application.ActiveDocument.Subject = "Rev."
    Application.ActiveDocument.Manager = "Approved"
    Application.ActiveDocument.Company = "Company"
    Application.ActiveDocument.Category = "Doc type"
    Application.ActiveDocument.Keywords = "ECN"
    Application.ActiveDocument.Description = "Description"
    Application.ActiveDocument.HyperlinkBase = "Initials"
    Application.EndUndoScope UndoScopeID1, True

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Font Size")
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "10 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDblUnderline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterOverline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterStrikethru).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, 11).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDoubleStrikethrough).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterRTLText).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterUseVertical).FormulaU = "FALSE"
    Application.EndUndoScope UndoScopeID2, True

    Dim UndoScopeID3 As Long
    UndoScopeID3 = Application.BeginUndoScope("Text Properties")
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkLeftMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkRightMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkTopMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkBottomMargin).FormulaU = "0 pt"
    Application.EndUndoScope UndoScopeID3, True

    Application.ActivePage.Name = "CIRCUIT DIAGRAM"

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub


I have saved the above macro in the template I want to be using. If I double click the template, the macro works also in "Drawing1" file that is created automatically. The problem is that in order to run the macro, I always have to press ctrl+shift+d. For me it would be easy to remember such a combination, but a bunch of people who are using Visio very rarely wouldn't remember the combination.

Is there a way to change this macro that it runs always in "Drawing1" when one starts a new drawing by double clicking the template?

Jumpy

In VBE Editor of template go to Modul "ThisDocument".
In Comboboxes select event document_created.

In the document_created function place a call to your macro, or directly place your macro code inside the body of that event function.

document created always fires when a new document is created based on the template.

document opened always fires when a (already saved) document (=no longer the template) is opened, but not when a new document is created from the template.

slinden

Quote from: Jumpy on April 11, 2014, 11:56:23 AM
In VBE Editor of template go to Modul "ThisDocument".
In Comboboxes select event document_created.

In the document_created function place a call to your macro, or directly place your macro code inside the body of that event function.

document created always fires when a new document is created based on the template.

document opened always fires when a (already saved) document (=no longer the template) is opened, but not when a new document is created from the template.
Thank you for the reply. I still seem to have some problems with this. I found that "ThisDocument" module and I set "DocumentCreated" in the combobox, but the macro still does not work.

I tried the code first as it is written above then I removed the first parts of the script, but it still didn't work.

In the end I had this in the "ThisDocument" module:
Private Sub Document_DocumentCreated(ByVal doc As IVDocument)

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Summary Info")
    Application.ActiveDocument.Title = "P/N"
    Application.ActiveDocument.Subject = "Rev."
    Application.ActiveDocument.Manager = "Approved"
    Application.ActiveDocument.Company = "Company"
    Application.ActiveDocument.Category = "Doc type"
    Application.ActiveDocument.Keywords = "ECN"
    Application.ActiveDocument.Description = "Description"
    Application.ActiveDocument.HyperlinkBase = "Initials"
    Application.EndUndoScope UndoScopeID1, True

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Font Size")
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "10 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDblUnderline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterOverline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterStrikethru).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, 11).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDoubleStrikethrough).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterRTLText).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterUseVertical).FormulaU = "FALSE"
    Application.EndUndoScope UndoScopeID2, True

    Dim UndoScopeID3 As Long
    UndoScopeID3 = Application.BeginUndoScope("Text Properties")
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkLeftMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkRightMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkTopMargin).FormulaU = "0 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionObject, visRowText, visTxtBlkBottomMargin).FormulaU = "0 pt"
    Application.EndUndoScope UndoScopeID3, True

    Application.ActivePage.Name = "CIRCUIT DIAGRAM"


End Sub


Should I try calling the macro? Do I have to remove something from the original macro before calling it (keyboard shortcut etc).

slinden

I tried to insert a single part of my macro to the DocumentOpened function, but the macro didn't run when I tried to open the document.

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Font Size")
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "10 pt"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDblUnderline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterOverline).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterStrikethru).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, 11).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterDoubleStrikethrough).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterRTLText).FormulaU = "FALSE"
    Application.ActiveDocument.GestureFormatSheet.CellsSRC(visSectionCharacter, 0, visCharacterUseVertical).FormulaU = "FALSE"
    Application.EndUndoScope UndoScopeID1, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub


Is there a some kind of setting which I have to enable before using these macros? That simple macro above is only for changing the default font size from 12 to 10.

JohnGoldsmith

Hi,

I'm not clear on what you're trying to achieve here.  Can you go into a little more detail about what you want to happen?

As it stands it looks like you've recorded your macro with nothing selected in the drawing window and so your changes in the UI (to the text formatting) have been applied to a special shape called the GestureFormatSheet.  This is a special shape that stores default formatting for drawing new primitive shapes etc, which is perhaps what you intended?

If you were expecting to see some visible change then you won't unless you draw a new shape.

Here's some slightly adapted code that includes a msgbox so you know the event is firing:

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
MsgBox "DocumentOpened event was fired"

With doc.GestureFormatSheet
    .CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "10 pt"
    .CellsSRC(visSectionCharacter, 0, visCharacterDblUnderline).FormulaU = "TRUE"
    .CellsSRC(visSectionCharacter, 0, visCharacterOverline).FormulaU = "FALSE"
    .CellsSRC(visSectionCharacter, 0, visCharacterStrikethru).FormulaU = "FALSE"
    .CellsSRC(visSectionCharacter, 0, 11).FormulaU = "FALSE"
    .CellsSRC(visSectionCharacter, 0, visCharacterDoubleStrikethrough).FormulaU = "FALSE"
    .CellsSRC(visSectionCharacter, 0, visCharacterRTLText).FormulaU = "FALSE"
    .CellsSRC(visSectionCharacter, 0, visCharacterUseVertical).FormulaU = "FALSE"
End With

End Sub


If you open draw a new shape after this code has been run you'll notice that the text is double underlined as described in the code.

Best regards

John
John Goldsmith - Visio MVP
http://visualsignals.typepad.co.uk/

Yacine

#5
From what I read, slinden tries to set up certain properties of a document on opening it.
Unless there are serious reasons for doing so, this is usually done in Visio via templates.

Templates:
Create a new blank drawing.
Modify whatever you need.
Save the file as WhatEverYouWantToCallIt.VST
New drawings shall then be created from this template.

This is even for a highly dynamic system the better way to go. Better move the actual drawing to a new file created from the current template, than trying to modify an existing document.
Yacine

slinden

#6
I try to explain again.

I use a Visio template for electrical schematics. It is not possible to save default settings in the template and every time I create a new document based on the template, I have to do the same settings all over again.

When I create a new drawing (which is automatically named Drawing1 by Visio) based on the template, the settings are always wrong. In the following list you can find the settings which I would like to change:

Font size: 12 pt => 10 pt
Sheet name: page-1 => CIRCUIT DIAGRAM
Text block margins: 4 pt => 0 pt (all margins)

Also in the documents advanced properties I would like to set automatically text in all of the fields.


I have done the macro by recoding it manually. If I use it with a key combination CRTL+SHIFT+D, the macro fires normally in the template and in Drawing1. What I am not able to do is make it automatically running every time a new document is created based on the template.

I tried Johns hint to use a MsgBox function, but I didn't see anything visible when creating a new document.

Can you see something wrong in my code?



Sorry about the big image.  :-\




Yacine

The font settings are modified by editing the "normal" style. The changes are permanent.
Only the file properties need to be set by macro.
I include a template which does what you want.
Yacine

slinden

Quote from: Yacine on April 17, 2014, 01:59:54 PM
The font settings are modified by editing the "normal" style. The changes are permanent.
Only the file properties need to be set by macro.
I include a template which does what you want.
Thank you very much, Yacine.

I was able to translate these changes also to our official template. I just went to the drawing explorer and looked for the Normal style as you probably had done. I just didn't find the sheet name there. Where did you change that?

How about the document properties? If I created the macro as I have shown before, should the macro fill the properties fields as I wanted? The problem is that the macro does not fire and I don't know how to fix it.

I have also a lot of questions about styles, but I guess that's totally another topic and should not be mixed with this one.

Jumpy

#9
Why don't you start with a simple test code, to see if the events fire:


Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
  MsgBox "DocumentOpened event was fired"
  InstallSettings
End Sub

Private Sub Document_DocumentCreated(ByVal doc As IVDocument)
  MsgBox "DocumentCreated event was fired"
  InstallSettings
End Sub

Private Sub InstallSettings()
  'Do nothing at first
End Sub


If those simple events don't fire you may have to look after your macro security settings.

If they fire, you can start to add your code to change the settings in the procedure "InstallSettings" line for line.

-------------

But aside from the above. I don't understand why your settings don't remain after you save the template again (you do save it as a template again, don't you?)?

Yacine

#10
Quote from: slinden on April 22, 2014, 12:41:58 PM
I was able to translate these changes also to our official template. I just went to the drawing explorer and looked for the Normal style as you probably had done. I just didn't find the sheet name there. Where did you change that?
Yacine

slinden

Quote from: Jumpy on April 22, 2014, 02:27:14 PM
Why don't you start with a simple test code, to see if the events fire:


Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
  MsgBox "DocumentOpened event was fired"
  InstallSettings
End Sub

Private Sub Document_DocumentCreated(ByVal doc As IVDocument)
  MsgBox "DocumentCreated event was fired"
  InstallSettings
End Sub

Private Sub InstallSettings()
  'Do nothing at first
End Sub


If those simple events don't fire you may have to look after your macro security settings.

If they fire, you can start to add your code to change the settings in the procedure "InstallSettings" line for line.

-------------

Still doesn't work... I don't know why and I can't find any more settings to enable VBA or macros. I attach an image once again to make sure that I have done everything correctly.



Quote from: Jumpy on April 22, 2014, 02:27:14 PMBut aside from the above. I don't understand why your settings don't remain after you save the template again (you do save it as a template again, don't you?)?

The settings must be done every time I open again a Visio file. Of course, if I have created a text box and edited its settings, there settings stay active as they should, but if I tried to create a new text box, I would have to do all settings again.

Same goes for any setting in Visio. Editing old drawing tends to frustrating when half of the shapes and text boxes have the default settings, which do not work for my purpose :(

Jumpy

Can you make a screenshoot of your security settings? If that simple macros don't fire, maybe VBA is disabled.

slinden

Quote from: Jumpy on April 25, 2014, 10:43:49 AM
Can you make a screenshoot of your security settings? If that simple macros don't fire, maybe VBA is disabled.
Here you go. Is there any other settings available?



P.S. There should be a possibility to add spoiler tags on this forum. :)