Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Code snippet (for Add-In) to fix .vbw (workspace) issue

$
0
0
It didn't seem worthwhile creating an Add-In just for this (it seems more appropriate that it be incorporated into other addins), hence posting as a snippet...

Don't know about anybody else, but the IDE's insistence, upon opening a project, of randomly restoring windows, is one VB6 development annoyance that I've lived with for a long time.

Anyway, whilst in the process of developing a related Add-in, I applied my new-found familiarity with the IDE object model, to come up with this:

Somewhere in your Add-In, you'll need a reference to VBIDE.VBProjectsEvents

e.g.
Code:

Private WithEvents mProjectEvents As VBIDE.VBProjectsEvents

...

Private Sub mProjectEvents_ItemRemoved(ByVal VBProject As VBIDE.VBProject)
  SaveVBWFile VBProject
End Sub

...and the following code, which you can put in a module (or wherever)


Code:

Option Explicit
Private Declare Function DeleteFileW Lib "kernel32" (ByVal lpFileName As Long) As Long

Public Sub SaveVBWFile(ByRef VBProj As VBProject)
Dim FileNo As Integer
Dim FilePath As String
Dim s() As String
Dim ComponentName As String
Dim W As VBIDE.Window
 
  FilePath = Replace(VBProj.FileName, ".vbp", ".vbw")
  DeleteFileW (StrPtr(FilePath))
 
  FileNo = FreeFile
  Open FilePath For Output As #FileNo
     
      For Each W In gVBInstance.Windows
        'Window captions take the form 'Project Name - Component Name (type suffix)', where Project name
        'can be absent if the Component belongs to the Active Project

        If W.Type = vbext_wt_CodeWindow Then
            'strip the suffix - we're not intersted in that
            s = Split(W.Caption, " (")
          'If Project name is missing, add it in
            If InStr(s(0), " - ") = 0 Then s(0) = gVBInstance.ActiveVBProject.Name & " - " & s(0)
          'Now attempt to replace the Project Name...
            ComponentName = Replace(s(0), VBProj.Name & " - ", vbNullString)
          '...if that worked, we are interested in this Window
            If InStr(ComponentName, " - ") = 0 Then
              Print #FileNo, ComponentName & " = " & W.Left & "," & W.Top & "," & W.Width & "," & W.Height & ",0,0,0,0,C"
            End If
        End If
      Next W
 
  Close #FileNo
 
End Sub

It's not perfect, as it won't allow you to save vbw data for Designer Windows. Unfortunately, by the time mProjectEvents_ItemRemoved fires, all such windows have already been destroyed. Still, I can live with that versus the chaos that the IDE sometimes inflicts on me.

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>