{"id":35,"date":"2010-02-24T16:56:33","date_gmt":"2010-02-24T15:56:33","guid":{"rendered":"http:\/\/blog.marekolszewski.pl\/?p=35"},"modified":"2010-02-24T16:56:33","modified_gmt":"2010-02-24T15:56:33","slug":"vba-zmieniajacy-vba","status":"publish","type":"post","link":"http:\/\/blog.marekolszewski.pl\/?p=35","title":{"rendered":"VBA zmieniaj\u0105cy VBA"},"content":{"rendered":"<p>Mia\u0142em problem z b\u0142\u0119dnie dzia\u0142aj\u0105cym kodem VBA. Problem niby nie wielki bo trzeba by\u0142o zlikwidowa\u0107 1 niepotrzebn\u0105 linijk\u0119 kodu. Jednak trzeba to zrobi\u0107 u kilkuset u\u017cytkownik\u00f3w w arkuszu kt\u00f3ry mia\u0142 ju\u017c powpisywane dane. Wys\u0142anie nowego nie bardzo wchodzi\u0142o w gr\u0119. Dodatkowo projekt by\u0142 zabezpieczony has\u0142em.<\/p>\n<p>Jednak przemy\u015blno\u015b\u0107 ludzka nie zna granic i dzi\u0119ki podpowiedziom z <a href=\"http:\/\/www.mrexcel.com\/forum\/showthread.php?t=75009\">tej<\/a>, <a href=\"http:\/\/frederic.sigonneau.free.fr\/code\/VBE\/ProtegerDeprotegerVBAProject.txt\">tej<\/a>  i <a href=\"http:\/\/www.cpearson.com\/excel\/vbe.aspx\">tej<\/a> strony uda\u0142o mi si\u0119 stworzy\u0107 arkusz kt\u00f3ry automatycznie zmienia\u0142 zawarto\u015b\u0107 kodu!<\/p>\n<p>Kod jest do\u015b\u0107 &#8222;brudny&#8221; ale skutecznie dzia\u0142aj\u0105cy.<br \/>\nIstotne jest aby zabezpieczeniach makr zaznaczy\u0107 opcj\u0119<br \/>\n&#8222;Ufaj dost\u0119powi do programu Visual Basic Project&#8221; w zak\u0142adce &#8222;Zaufani wydawcy&#8221; (Excel 2003)<br \/>\ni w &#8222;Tools&#8221; > &#8222;References&#8230;&#8221;<br \/>\nDoda\u0107 &#8222;Microsoft Visual Basic For Applications Extensibility 5.3.&#8221;<\/p>\n<p><code><br \/>\nOption Explicit<br \/>\n'<br \/>\nPrivate Const vbext_ct_ActiveXDesigner = 11<br \/>\nPrivate Const vbext_ct_ClassModule = 2<br \/>\nPrivate Const vbext_ct_Document = 100 '(&H64)<br \/>\nPrivate Const vbext_ct_MSForm = 3<br \/>\nPrivate Const vbext_ct_StdModule = 1<br \/>\n'<br \/>\nSub start()<br \/>\nDim i As Integer 'counter of files<br \/>\nDim GarageFile As String<br \/>\nDim FDir, all As String<br \/>\n'<br \/>\n'wyb\u00f3r pliku<br \/>\nall=\"c:\\jakis katalog\\arkusz.xls\"<br \/>\nWorkbooks.Open Filename:=all<br \/>\nGarageFile = StripPathString(all, False)<br \/>\n'Ten wpis jest istotny je\u015bli wybieramy plik formatk\u0105<br \/>\nUnprotectVBProject Workbooks(GarageFile), \"test123\"<br \/>\nDoEvents<br \/>\n  With Workbooks(GarageFile)<br \/>\n    .VBProject.VBComponents.Add 1<br \/>\n  End With<br \/>\n' Tu jest wywo\u0142anie zmiany linii<br \/>\nCall ModuleFindAndReplace(\"wynik = zapis(6, \"\"\"\")\", \"' zmiana lini !!!!\")<br \/>\n'I po problemie<br \/>\nActiveWorkbook.Close SaveChanges:=True<br \/>\n' tadam!<br \/>\nEnd Sub<br \/>\n'<br \/>\n' funkcja odblokowanie projektu nie trzeba go p\u00f3\u017aniej zabezpiecza\u0107<br \/>\nSub UnprotectVBProject(WB As Workbook, ByVal Password As String)<br \/>\nDim vbProj As Object<br \/>\n'<br \/>\n  Set vbProj = WB.VBProject<br \/>\n'<br \/>\n  'can't do it if already unlocked!<br \/>\n  If vbProj.Protection <> 1 Then Exit Sub<br \/>\n'<br \/>\n  Set Application.VBE.ActiveVBProject = vbProj<br \/>\n'<br \/>\n  ' now use lovely SendKeys to quote the project password<br \/>\n  SendKeys Password & \"~~\"<br \/>\n  Application.VBE.CommandBars(1).FindControl(ID:=2578, recursive:=True).Execute<br \/>\nEnd Sub<br \/>\n'<br \/>\n'<br \/>\n'<br \/>\nPublic Function ModuleFindAndReplace(FindText As String, ReplaceText As String, Optional DeclarationsText As String) As Boolean<br \/>\n    Dim objComponent As Object<br \/>\n    Dim strTemp As String<br \/>\n    Dim lngStartRow As Long, lngStartCol As Long, lngEndRow As Long, lngEndCol As Long<br \/>\n    Dim i As Integer<br \/>\n    'tu jest istotna zmiana od przyk\u0142ad\u00f3w ze stron<br \/>\n    'wybieram drugi projekt (lub kolejny) by nie szuka\u0107 w samym sobie!!!!<br \/>\n    With Application.VBE.VBProjects.Item(2)<br \/>\n        For Each objComponent In .VBComponents<br \/>\n        With objComponent.CodeModule<br \/>\n          ' ilo\u015b\u0107 linii w kodzie i nazwa modu\u0142u lub formatki!<br \/>\n            If .CountOfLines > 0 And .Name = \"podsumowanie\" Then<br \/>\n                lngStartRow = 1: lngStartCol = 1<br \/>\n                'Setting to -1 signifies last row and column<br \/>\n                lngEndRow = -1: lngEndCol = -1<br \/>\n                Do While .Find(FindText, lngStartRow, lngStartCol, -lngEndRow, -lngEndCol, False, False)<br \/>\n                        strTemp = .Lines(lngStartRow, 1)<br \/>\n                        strTemp = Replace(strTemp, FindText, ReplaceText)<br \/>\n                        Debug.Print objComponent.Name & \" \" & objComponent.Type & \" start: \" & lngStartRow & \",\" & lngStartCol & \" End: \" & lngEndRow & \",\" & lngEndCol & vbCrLf & \" Before: \" & .Lines(lngStartRow, 1) & vbCrLf & \" After: \" & strTemp<br \/>\n                        .ReplaceLine lngStartRow, strTemp<br \/>\n                    lngStartRow = lngStartRow + 1<br \/>\n                    lngStartCol = 1<br \/>\n                Loop<br \/>\n            End If<br \/>\n        End With<br \/>\n        Next objComponent<br \/>\n    End With<br \/>\n    Set objComponent = Nothing<br \/>\nEnd Function<br \/>\n'<br \/>\n' Bonusowa funkcja kt\u00f3ra zwraca ze \u015bcie\u017cki sam\u0105 nazw\u0119 pliku<br \/>\n' Znaleziona gdzie\u015b w internecie<br \/>\n'<br \/>\nFunction StripPathString(ByVal Path As String, ByVal StripFileName As Boolean) As String<br \/>\nDim X As Integer, Ct As Integer, Y As Integer<br \/>\nStripPathString = Path<br \/>\nX = InStr(Path, \"\\\")<br \/>\nDo While X<br \/>\nCt = X<br \/>\nX = InStr(Ct + 1, Path, \"\\\")<br \/>\nLoop<br \/>\n' tu wprowadzi\u0142em zmiany<br \/>\nIf Ct > 0 Then<br \/>\nSelect Case StripFileName<br \/>\nCase False<br \/>\nStripPathString = Mid(Path, Ct + 1)<br \/>\nCase True<br \/>\nY = InStr(Path, Mid(Path, Ct + 1))<br \/>\nStripPathString = Mid(Path, 1, Y - 1)<br \/>\nEnd Select<br \/>\nElse<br \/>\nStripPathString = \"\"<br \/>\nEnd If<br \/>\nEnd Function<br \/>\n<\/code> <\/p>\n<p>Ca\u0142a zabawa zaoszcz\u0119dzi\u0142a mi mn\u00f3stwo problem\u00f3w.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mia\u0142em problem z b\u0142\u0119dnie dzia\u0142aj\u0105cym kodem VBA. Problem niby nie wielki bo trzeba by\u0142o zlikwidowa\u0107 1 niepotrzebn\u0105 linijk\u0119 kodu. Jednak trzeba to zrobi\u0107 u kilkuset u\u017cytkownik\u00f3w w arkuszu kt\u00f3ry mia\u0142 ju\u017c powpisywane dane. Wys\u0142anie nowego nie bardzo wchodzi\u0142o w gr\u0119. Dodatkowo projekt by\u0142 zabezpieczony has\u0142em. Jednak przemy\u015blno\u015b\u0107 ludzka nie zna granic i dzi\u0119ki podpowiedziom z &hellip; <a href=\"http:\/\/blog.marekolszewski.pl\/?p=35\" class=\"more-link\">Czytaj dalej <span class=\"screen-reader-text\">VBA zmieniaj\u0105cy VBA<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/posts\/35"}],"collection":[{"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":3,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":38,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions\/38"}],"wp:attachment":[{"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.marekolszewski.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}